@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