[英]Two method for UIButton?
我們需要用兩種方法設置一個UIButton
,第一個是當您觸摸它(上下)時,您會執行一個操作,但是當您長按它時,您將獲得另一個方法。
例如,要獲得有關它的數據(長按和常規單擊時),另一件事。
如何使用UIButton實現此目的?
UIButton *CAT = [UIButton buttonWithType:UIButtonTypeCustom];
CAT.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
CAT.backgroundColor=[UIColor clearColor];
[CAT addTarget:self action:@selector(newcat:)forControlEvents:UIControlEventTouchUpInside];
我已經開始添加一個手勢
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[CAT addGestureRecognizer:longPress];
但這僅在您松開手指時觸發。 我希望在1-2秒后我的手指仍在時觸發它。 我可以那樣做嗎? 我可以調整觸發時間嗎?
您可以使用事件UIControlEventTouchDown
,使用延遲時間長按,並且必須處理UIControlEventTouchUpInside
和UIControlEventTouchUpOutside
。 祝好運!
這是正常的方法
UIButton *CAT = [UIButton buttonWithType:UIButtonTypeCustom];
CAT.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
CAT.backgroundColor=[UIColor clearColor];
[CAT addTarget:self action:@selector(newcat:)forControlEvents:UIControlEventTouchUpInside];
UILongPressGestureRecognizer *longpressGesture =
[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(longPressHandler:)];
longpressGesture.minimumPressDuration = 5; // set the time interval
[longpressGesture setDelegate:self];
[CAT addGestureRecognizer:longpressGesture];
[self.view addsubview: CAT];
長按動作法
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
NSLog(@"longPressHandler");
// do long press action
}
正常動作方法
-(void) newcat:(id)sender
{
// do normal action here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.