我想模仿長按按鈕,我該怎麼做?我認爲需要一個計時器。 我看到UILongPressGestureRecognizer
但我該如何利用這種類型?UIButton長按活動
回答
您可以通過創建並將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");
}
}
現在,這將是基本方法的方法。您還可以設置印刷機的最短持續時間和可承受的誤差。另外請注意,如果您在識別該手勢後幾次調用該方法,那麼如果您想在其末尾執行某些操作,則必須檢查其狀態並處理它。
試試這個:
在視圖中添加按鈕做負載這樣
-(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
}
'recognizer.view.tag'給我錯誤標記的UIButton點擊。任何解決方案 – 2013-03-20 11:32:40
手勢的方法來接受的答案的替代,這可使用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
時,即使在將手指從屏幕上擡起之前,只要系統抓住長按,就會看到長按的效果。
我認爲你需要我的解決方案。
你應該有一個按
- (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;
}
}
使用故事板進行手勢識別要簡單得多。 查看斯坦福大學的視頻以瞭解手勢識別。 https://www.youtube.com/watch?v=85IUfbgp0v8
我有我的應用程序的子類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
}
接受的答案
的斯威夫特版本我做了使用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")
}
}
無工作,因此我試着在IBAction
或單擊按鈕寫長按代碼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];
}
對於斯威夫特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")
}
}
- 1. 長按UIButton
- 2. 長按和短按UIBUTTON
- 3. 將UIButton的長按在iPhone
- 4. UIButton長按手指靜止
- 5. 活動的UIButton問題
- 6. 活動家長
- 7. UIButton上的長按手勢識別器?
- 8. 如何實現UIButton的長按
- 9. 如何處理長按媒體按鈕,以啓動活動?
- 10. 按鈕仍然處於活動狀態長按jQuery移動
- 11. 快速按下單按和長按事件的UIButton
- 12. 按下UIButton時移動
- 13. 主頁按鈕/長按休息時間活動堆棧
- 14. UIButton和UIScrollView - 按下按鈕時滾動
- 15. 與家長溝通活動,提升家長活動
- 16. UIButton上的活動指示器
- 17. UIButton網格激活同時拖動
- 18. 長按鍵盤空間鍵啓動我的活動
- 19. UIButton按住動作並釋放動作
- 20. Android:從長按鍵的QWERTY鍵開始我的活動從其他活動
- 21. 檢測家中孩子被按下的時間家長活動的活動
- 22. 取消激活UIButton
- 23. Creat UIButton激活UISwitch
- 24. Android:長按動作按鈕
- 25. 延長現有活動
- 26. 多個家長活動
- 27. 長按後拖動
- 28. UIButton按住
- 29. 用的UIButton按
- 30. 吳級 - 活動/非活動按鈕
超級!謝謝! btw:if(gesture.state == UIGestureRecognizerStateEnded)非常重要,否則你會在你的longPress中得到很多事件void – RecycleRobot 2013-05-24 12:25:22
你可能想使用'if(gesture.state == UIGestureRecognizerStateBegan)',因爲用戶當他們還在按下時(狀態開始),而不是當他們釋放時(結束),他們期望發生什麼事情。 – shengbinmeng 2014-10-23 13:40:16