我想模拟长按按钮,该怎么做?我认为需要一个计时器。
我看到了UILongPressGestureRecognizer
,但我该如何使用这种类型?
我想模拟长按按钮,该怎么做?我认为需要一个计时器。
我看到了UILongPressGestureRecognizer
,但我该如何使用这种类型?
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");
}
}
现在这将是基本的方法。您也可以设置按压的最短持续时间以及允许的误差量。并且请注意,如果您想要在识别手势后执行某些操作,则该方法将被调用几次,因此您需要检查它的状态并处理它。
除了被接受的答案,这个问题也可以在Xcode中使用Interface Builder轻松解决。
只需从对象库中拖动一个长按手势识别器(Long Press Gesture Recognizer),并将其放到您想要添加长按操作的按钮上即可。
然后,从刚刚添加的长按手势识别器(Long Press Gesture Recognizer)连接一个操作(Action)到您的视图控制器,选择发送方类型为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
时,即使在您还没有将手指离开屏幕之前,系统捕获到了长按的动作,您也能看到长按的效果。
我做了额外的修改,使用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")
}
}
尝试这样做:
在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
}
recognizer.view.tag
给出了错误的被点击 UIButton 的标签。有任何解决方案吗? - rohan-patel- (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;
}
}
viewWillAppear
生命周期事件期间添加 UIGestureRecognizer
,因为每当视图出现时,都会添加另一个手势识别器。这应该在初始化期间调用的私有方法中完成。 - WikipediaBrownimport 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")
}
}
一行回答,没有手势:
[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。
在Storyboard中: 将您的按钮连接到3个事件,而不仅仅是Storyboard选择的默认事件(Touch Up Inside)。
由于没有一个方法起作用,因此我尝试在Controller
中的IBAction
或按钮单击事件以及storyboard
中编写长按代码,而不是在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];
}
@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];
}
}
[_myLongButton addLongPressTarget:0.75 target:self selector:@selector(longPressAction:)];
这个动作应该像所有的IBAction一样被定义(不需要使用IBAction关键字)。
- (void)longPressAction:(id)sender {
// sender is the button
}
if(gesture.state == UIGestureRecognizerStateBegan)
,因为用户希望在他们仍在按住屏幕时(即状态为 Began)就能看到某些反应,而不是在他们松开手指时(即 Ended)。 - shengbinmeng