我希望当用户从左向右滑动时,标签计数器从1更新为5。如果我缓慢滑动,则它会非常缓慢地工作,然后会更快地工作,那是行不通的吗?还有另一种方法可以实现吗?

我有这个代码:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    gestureStartPoint = [touch locationInView:self.view];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.view];

    if ((int)(currentPosition.x)%40 == 0 && counter < 5) {
        counter++;
    }

    [label setText:[NSString stringWithFormat:@"%d", counter]];

}

最佳答案

您可以使用UIPanGestureRecognizer ...它具有开始状态,更改状态和结束状态,例如touchEvents...。

- (void)onDraggingLetter:(UIPanGestureRecognizer *)panGR {

    if (panGR.state == UIGestureRecognizerStateBegan) {

    } else if (panGR.state == UIGestureRecognizerStateChanged) {

    } else if (panGR.state == UIGestureRecognizerStateEnded) {

    }
}


它可以帮助您...

10-04 15:58