关于longPressGesture做一个长按连加的效果

2017-07-25  本文已影响36人  Harry_Coding

添加手势

UILongPressGestureRecognizer *longPressGuesForMinus = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(minusButtonLongPressed:)];
    longPressGuesForMinus.allowableMovement = 50;
    longPressGuesForMinus.delegate = self;
    [self.minusButton addGestureRecognizer:longPressGuesForMinus];

手势处理方法

- (void)minusButtonLongPressed:(UILongPressGestureRecognizer *)guesture {
    BOOL flag = YES;
    if (guesture.state == UIGestureRecognizerStateEnded || guesture.state == UIGestureRecognizerStateFailed) {
        flag = NO;
    }
    if (flag) {
        if (!self.timer) {
            ACWeakSelf(self);
            self.timer = [NSTimer scheduledTimerWithTimeInterval:0.2 repeats:YES block:^(NSTimer * _Nonnull timer) {
                [weakSelf minusValueChange];
            }];
        }
        [self.timer fire];
    }
    if (!flag) {
        if (self.timer.isValid) {
            [self.timer invalidate];
            self.timer = nil;
        }
    }
    
}

递减方法

- (void)minusValueChange {
    NSInteger value = [self.guestLabel.text integerValue]-1;
    if (value>0) {
        self.guestLabel.text = [NSString stringWithFormat:@"%ld", value];
    }
}
上一篇下一篇

猜你喜欢

热点阅读