And*_*rea 83 objective-c uibutton ios long-press
我想模仿一个长按按钮,我该怎么做?我认为需要一个计时器.我明白UILongPressGestureRecognizer但是我怎么能利用这种类型呢?
Dee*_*olu 157
您可以通过创建UILongPressGestureRecognizer实例并将其附加到按钮来开始.
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.button addGestureRecognizer:longPress];
[longPress release];
Run Code Online (Sandbox Code Playgroud)
然后实现处理手势的方法
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
NSLog(@"Long Press");
}
}
Run Code Online (Sandbox Code Playgroud)
现在这将是基本方法.您还可以设置印刷机的最短持续时间以及可容忍的误差范围.并且还要注意,如果您在识别出手势之后几次调用该方法,那么如果您想在其末尾执行某些操作,则必须检查其状态并进行处理.
alo*_*ono 26
作为已接受答案的替代方案,可以使用Interface Builder在Xcode中轻松完成.
只需从对象库拖动长按手势识别器,然后将其放在您想要长按操作的按钮顶部.
接下来,将刚添加的长按手势识别器中的操作连接到视图控制器,选择发送者类型UILongPressGestureRecognizer.在IBAction使用它的代码中,这与接受的答案中建议的代码非常相似:
在Objective-C中:
if ( sender.state == UIGestureRecognizerStateEnded ) {
// Do your stuff here
}
Run Code Online (Sandbox Code Playgroud)
或者在Swift中:
if sender.state == .Ended {
// Do your stuff here
}
Run Code Online (Sandbox Code Playgroud)
但我必须承认,在尝试之后,我更喜欢@shengbinmeng提出的建议作为对已接受答案的评论,该答案将使用:
在Objective-C中:
if ( sender.state == UIGestureRecognizerStateBegan ) {
// Do your stuff here
}
Run Code Online (Sandbox Code Playgroud)
或者在Swift中:
if sender.state == .Began {
// Do your stuff here
}
Run Code Online (Sandbox Code Playgroud)
不同之处在于Ended,当您抬起手指时,您会看到长按的效果.使用时Began,即使在将手指从屏幕上抬起之前,只要系统卡住长按,就会看到长按的效果.
Sur*_*gch 16
我对使用进行了额外的修改,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")
}
}
Run Code Online (Sandbox Code Playgroud)
试试这个:
在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];
}
Run Code Online (Sandbox Code Playgroud)
现在调用这样的手势方法
-(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
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我想你需要我的解决方案。
你应该有这个代码用于单击
- (IBAction)buttonDidPress:(id)sender {
NSLog("buttonDidPress");
}
Run Code Online (Sandbox Code Playgroud)
首先,为按钮添加长按手势
- (void)viewWillAppear:(BOOL)animated
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonDidLongPress:)];
[self.button addGestureRecognizer:longPress];
}
Run Code Online (Sandbox Code Playgroud)
如果识别到长按手势,则重复调用单按事件。
- (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;
}
}
Run Code Online (Sandbox Code Playgroud)
对于 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")
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64858 次 |
| 最近记录: |