Gesture - Long Press

@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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值