UIButton长按活动
我想模仿长按button,我该怎么做? 我觉得需要一个计时器。 我看到UILongPressGestureRecognizer
但我怎么能利用这种types?
您可以通过创build并将UILongPressGestureRecognizer
实例附加到button来开始。
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"); } }
现在这将是基本的方法。 您也可以设置新闻的最小持续时间和可以承受的误差。 另外请注意,如果您在识别该手势之后再调用该方法的次数很less,那么如果您想在其末尾执行某些操作,则必须检查其状态并进行处理。
作为被接受的答案的替代,这可以通过使用Interface Builder在Xcode中很容易地完成。
只需从对象库中拖出一个长按手势识别器 ,然后将其放在您想要长按操作的button的顶部。
接下来,将刚添加的长按手势识别器的动作连接到您的视图控制器,select发送者为UILongPressGestureRecognizer
types。 在IBAction
的代码中,使用这个代码与接受的答案中提出的代码非常相似:
在Objective-C中 :
if ( sender.state == UIGestureRecognizerStateEnded ) { // Do your stuff here }
或者在Swift中 :
if sender.state == .Ended { // Do your stuff here }
但是我不得不承认,在尝试之后,我更喜欢@shengbinmeng提出的build议,作为对接受答案的评论,即:
在Objective-C中 :
if ( sender.state == UIGestureRecognizerStateBegan ) { // Do your stuff here }
或者在Swift中 :
if sender.state == .Began { // Do your stuff here }
不同的是,用Ended
,你会看到长按时你的手指的效果。 Began
,即使在将手指从屏幕上抬起之前,只要长时间按下系统,就会看到长按的效果。
Swift版本的接受答案
我做了额外的修改,使用UIGestureRecognizerState.Began
而不是.Ended
因为这可能是大多数用户自然期望的。 不过,试试他们两个,然后看看自己。
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) } func longPress(guesture: UILongPressGestureRecognizer) { if guesture.state == UIGestureRecognizerState.Began { print("Long Press") } } @IBAction func normalButtonTap(sender: UIButton) { print("Button tapped") } }
尝试这个:
在视图中添加button的负载是这样的
-(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 tag with recogniser tag //view frame for getting the info on which button the click event has been 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 dont need //to write frame comparing code if you are matching the tag number of button }
我想你需要我的解决scheme。
你应该有这个代码单击
- (IBAction)buttonDidPress:(id)sender { NSLog("buttonDidPress"); }
首先,将长按手势添加到button
- (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; } }
使用故事板进行手势识别要简单得多。 查看斯坦福大学的video,了解手势识别。 https://www.youtube.com/watch?v=85IUfbgp0v8
我有我的应用程序的子类UIButton,所以我已经拿出我的实现。 你可以把它添加到你的子类中,或者像UIButton类一样容易被重新编码。
我的目标是将长按button添加到我的button中,而不会将视图控制器与所有代码混淆在一起。 我已经决定,当手势识别器状态开始时应该调用该动作。
有一个警告,我从来没有打算解决。 说这是一个可能的泄漏,认为我testing了代码,并没有泄漏。
@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 }