我想模仿一个长按按钮,我该怎么做?我认为需要一个计时器。
我看到UILongPressGestureRecognizer
但我怎样才能使用这种类型?
答案 0 :(得分:154)
您可以通过创建UILongPressGestureRecognizer
实例并将其附加到按钮来开始。
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.button addGestureRecognizer:longPress];
[longPress release];
然后实现处理手势的方法
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
NSLog(@"Long Press");
}
}
现在这将是基本方法。您还可以设置印刷机的最短持续时间以及可容忍的误差范围。并且还要注意,如果你在识别出手势之后几次调用该方法,那么如果你想在它结束时做一些事情,你将必须检查它的状态并处理它。
答案 1 :(得分:25)
作为已接受答案的替代方案,可以使用Interface Builder在Xcode中轻松完成。
只需从对象库拖动长按手势识别器,然后将其放在您想要长按操作的按钮顶部。
接下来,将刚添加的长按手势识别器中的操作连接到视图控制器,选择发件人为UILongPressGestureRecognizer
类型。在IBAction
的代码中使用此代码,这与接受的答案中建议的代码非常相似:
在 Objective-C :
中if ( sender.state == UIGestureRecognizerStateEnded ) {
// Do your stuff here
}
或 Swift :
if sender.state == .Ended {
// Do your stuff here
}
但是我必须承认,在尝试之后,我更喜欢@shengbinmeng提出的建议作为对已接受答案的评论,该答案将被使用:
在 Objective-C :
中if ( sender.state == UIGestureRecognizerStateBegan ) {
// Do your stuff here
}
或 Swift :
if sender.state == .Began {
// Do your stuff here
}
与Ended
不同的是,当您抬起手指时,您会看到长按的效果。使用Began
,即使在您将手指从屏幕上抬起之前,只要长按被系统捕获,您就会看到长按的效果。
答案 2 :(得分:15)
我使用UIGestureRecognizerState.Began
而不是.Ended
进行了额外的修改,因为这可能是大多数用户自然期望的。试试这两个并亲自看看。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// add gesture recognizer
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
self.button.addGestureRecognizer(longPress)
}
func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began {
print("Long Press")
}
}
@IBAction func normalButtonTap(sender: UIButton) {
print("Button tapped")
}
}
答案 3 :(得分:8)
试试这个:
在viewDidLoad:
添加按钮,如下所示
-(void)viewDidLoad {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTag:1]; //you can set any integer value as tag number
btn.title = @"Press Me";
[btn setFrame:CGRectMake(50.0, 50.0, 60.0, 60.0)];
// now create a long press gesture
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressTap:)];
[btn addGestureRecognizer:longPress];
}
现在调用这样的手势方法
-(void)longPressTap:(id)sender {
UIGestureRecognizer *recognizer = (UIGestureRecognizer*) sender
// Recogniser have all property of button on which you have clicked
// Now you can compare button's tag with recogniser's view.tag
// View frame for getting the info on which button the click event happened
// Then compare tag like this
if(recognizer.view.tag == 1) {
// Put your button's click code here
}
// And you can also compare the frame of your button with recogniser's view
CGRect btnRect = CGRectMake(50.0, 50.0, 60.0, 60.0);
if(recogniser.view.frame == btnRect) {
//put your button's click code here
}
// Remember frame comparing is alternative method you don't need to write frame comparing code if you are matching the tag number of button
}
答案 4 :(得分:3)
我认为你需要我的解决方案。
你应该有这个代码单按
- (IBAction)buttonDidPress:(id)sender {
NSLog("buttonDidPress");
}
首先,将长按手势添加到按钮
- (void)viewWillAppear:(BOOL)animated
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonDidLongPress:)];
[self.button addGestureRecognizer:longPress];
}
如果识别出长按手势,则反复调用单击事件。
- (void)buttonDidLongPress:(UILongPressGestureRecognizer*)gesture
{
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
{
self.timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(buttonDidPress:) userInfo:nil repeats:YES];
NSRunLoop * theRunLoop = [NSRunLoop currentRunLoop];
[theRunLoop addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
break;
case UIGestureRecognizerStateEnded:
{
[self.timer invalidate];
self.timer = nil;
}
break;
default:
break;
}
}
答案 5 :(得分:3)
对于Swift 4," func longPress"需要改变以使其有效:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// add guesture recognizer
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
self.button.addGestureRecognizer(longPress)
}
@objc func longPress(_ guesture: UILongPressGestureRecognizer) {
if guesture.state == UIGestureRecognizerState.began {
print("Long Press")
}
}
@IBAction func normalButtonTap(sender: UIButton) {
print("Button tapped")
}
}
答案 6 :(得分:1)
单行答案,无手势:
[btn addTarget:self action:@selector(handleTouch:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
详细信息:
这将在三个事件上触发您的目标:
1-一旦手指按下按钮UIControlEventTouchDown
。这将捕获长按开始。
2&3-当用户抬起手指时:UIControlEventTouchUpOutside
和UIControlEventTouchUpInside
。这将捕获用户按下的内容。
注意:如果您不关心手势识别器提供的额外信息(例如触摸的位置等),此方法就很好用
如果需要,您可以添加更多的中间事件,请在https://developer.apple.com/documentation/uikit/uicontrolevents?language=objc中查看它们。
在情节提要中: 将您的按钮连接到3个事件,而不仅仅是Storyboard选择的默认事件(“ Touch Up Inside”)。
答案 7 :(得分:0)
我的应用程序有一个子类UIButton,所以我已经完成了我的实现。您可以将其添加到子类中,也可以将其重新编码为UIButton类别。
我的目标是将长按添加到我的按钮,而不会使我的视图控制器与所有代码混乱。我已经决定在手势识别器状态开始时应该调用该动作。
有一个警告,表明我从未打扰过要解决。说这是一个可能的泄漏,我认为我已经测试了代码并且它没有泄漏。
@interface MYLongButton ()
@property (nonatomic, strong) UILongPressGestureRecognizer *gestureRecognizer;
@property (nonatomic, strong) id gestureRecognizerTarget;
@property (nonatomic, assign) SEL gestureRecognizerSelector;
@end
@implementation MYLongButton
- (void)addLongPressTarget:(CGFloat)interval target:(id)target action:(SEL)selector
{
_gestureRecognizerTarget = target;
_gestureRecognizerSelector = selector;
_gestureRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGestureRecognizer:)];
_gestureRecognizer.minimumPressDuration = interval;
[self addGestureRecognizer:_gestureRecognizer];
}
- (void)handleLongPressGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
NSAssert([_gestureRecognizerTarget respondsToSelector:_gestureRecognizerSelector], @"target does not respond to selector");
self.highlighted = NO;
// warning on possible leak -- can anybody fix it?
[_gestureRecognizerTarget performSelector:_gestureRecognizerSelector withObject:self];
}
}
要指定操作,请将此行添加到viewDidLoad方法。
[_myLongButton addLongPressTarget:0.75 target:self selector:@selector(longPressAction:)];
应该像所有IBAction一样定义动作(没有IBAction)。
- (void)longPressAction:(id)sender {
// sender is the button
}
答案 8 :(得分:0)
没有效果,因此我尝试在IBAction
中编写longpress代码或在storyboard
中的Controller
点击按钮,而不是写入viewDidLoad
- (IBAction)btnClick:(id)sender {
tag = (int)((UIButton *)sender).tag;
// Long press here instead of in viewDidLoad
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
longPress.cancelsTouchesInView = NO;
[sender addGestureRecognizer:longPress];
}