@property (nonatomic, strong) UIButton *dummyButton;
@property (nonatomic, strong) UILongPressGestureRecognizer *longPressGesture;
-(void)initButton {
self.btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.btn.frame = CGRectMake(0.0f,0.0f,72.0f,37.0f);
[self.btn setTitle:@"My button" forState:UIControlStateNormal];
self.btn.center = self.view.center;
[self.view addSubview:self.btn];
}
- (void)addGesture {
self.longPressGesture =[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(handleLongPressGestures:)];
self.longPressGesture.numberOfTouchesRequired = 2;
/* Maximum 100 points of movement allowed before the gesture is recognized,default 10 points */
self.longPressGesture.allowableMovement = 100.0f;
/* The user must press 2 fingers (numberOfTouchesRequired) for at least 1 second for the gesture to be recognized */
self.longPressGesture.minimumPressDuration = 1.0;
[self.view addGestureRecognizer:self.longPressGesture];
}
- (void) handleLongPressGestures:(UILongPressGestureRecognizer *)paramSender{
/* 确保与其它的长按手势区分开来 */
if ([paramSender isEqual:self.longPressGesture]){
if (paramSender.numberOfTouchesRequired == 2){
CGPoint touchPoint1 = [paramSenderlocationOfTouch:0 inView:paramSender.view];
CGPoint touchPoint2 = [paramSender locationOfTouch:1 inView:paramSender.view];
CGFloat midPointX = (touchPoint1.x + touchPoint2.x) / 2.0f;
CGFloat midPointY = (touchPoint1.y + touchPoint2.y) / 2.0f;
CGPoint midPoint = CGPointMake(midPointX, midPointY);
self.btn.center = midPoint;
}
}
}
postscript:若在长按过程中接收到来电,则此手势状态改为UIGestureRecognizerStateCancelled
本文介绍了一个iOS应用中实现特定长按手势的过程。通过设置UILongPressGestureRecognizer,该手势识别器能够响应两个手指同时长按屏幕至少一秒的动作,并将按钮移动到两指中间位置。文章详细说明了如何初始化按钮、添加手势识别器以及处理长按事件。
952

被折叠的 条评论
为什么被折叠?



