2011-05-30 81 views
69

我想模仿長按按鈕,我該怎麼做?我認爲需要一個計時器。 我看到UILongPressGestureRecognizer但我該如何利用這種類型?UIButton長按活動

回答

144

您可以通過創建並將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"); 
    } 
} 

現在,這將是基本方法的方法。您還可以設置印刷機的最短持續時間和可承受的誤差。另外請注意,如果您在識別該手勢後幾次調用該方法,那麼如果您想在其末尾執行某些操作,則必須檢查其狀態並處理它。

+0

超級!謝謝! btw:if(gesture.state == UIGestureRecognizerStateEnded)非常重要,否則你會在你的longPress中得到很多事件void – RecycleRobot 2013-05-24 12:25:22

+21

你可能想使用'if(gesture.state == UIGestureRecognizerStateBegan)',因爲用戶當他們還在按下時(狀態開始),而不是當他們釋放時(結束),他們期望發生什麼事情。 – shengbinmeng 2014-10-23 13:40:16

8

試試這個:

在視圖中添加按鈕做負載這樣

-(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 
} 
+0

'recognizer.view.tag'給我錯誤標記的UIButton點擊。任何解決方案 – 2013-03-20 11:32:40

15

手勢的方法來接受的答案的替代,這可使用Interface Builder可以非常容易地在Xcode中完成。

剛從對象庫拖動長按手勢識別器拖放到你想要的長按操作按鈕的頂部。

接下來,連接從長按手勢識別器只是增加了一個行動,你的視圖控制器,選擇發件人是UILongPressGestureRecognizer類型。在這種IBAction這種使用,這是非常相似的代碼的代碼在接受的答案建議:

Objective-C的

if (sender.state == UIGestureRecognizerStateEnded) { 
    // Do your stuff here 
} 

或者在斯威夫特

if sender.state == .Ended { 
    // Do your stuff here 
} 

但我不得不承認,在嘗試之後,我更喜歡@shengbinmeng提出的建議作爲對已接受答案的評論,即使用:

Objective-C的

if (sender.state == UIGestureRecognizerStateBegan) { 
    // Do your stuff here 
} 

或者在斯威夫特

if sender.state == .Began { 
    // Do your stuff here 
} 

不同的是,與Ended,你看到長按的效果,當你擡起手指。使用Began時,即使在將手指從屏幕上擡起之前,只要系統抓住長按,就會看到長按的效果。

2

我認爲你需要我的解決方案。

你應該有一個按

- (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; 
    } 
} 
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:)]; 

該行爲應該像所有的IBActions(沒有IBAction)一樣定義。

- (void)longPressAction:(id)sender { 
    // sender is the button 
} 
10

接受的答案

的斯威夫特版本我做了使用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") 
    } 
} 
0

無工作,因此我試着在IBAction或單擊按鈕寫長按代碼storyboardController,而不是寫在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]; 

} 
0

對於斯威夫特4,「FUNC長按」需要改變,以讓它工作:

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") 
    } 
}