Making use of velocityInView with UIPanGestureRecognizer

二次信任 提交于 2019-11-30 18:31:05

问题


I have a custom slider-type object, that I wish to make more usable. Currently I use UIPanGestureRecognizer and translationInView to make it work. It works pretty well but I'd like some sort of velocity in there to make it feel a bit more useful. I've tried a few things but cant quite figure out how to properly implement velocity changedLevel equation.

- (void)panDetected:(UIPanGestureRecognizer *)gesture {

    CGPoint swipeLocation = [gesture locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
    LevelCounterTableCell *swipedCell = (LevelCounterTableCell *)[self.tableView cellForRowAtIndexPath:indexPath];

    if([gesture state] == UIGestureRecognizerStateBegan) {
        NSString *originalLevelString = swipedCell.levelNumber.text;
        originalLevel = [originalLevelString intValue]; // int originalLevel
    }

    if ([gesture state] == UIGestureRecognizerStateChanged) {

        CGFloat xTranslation = [gesture translationInView:[[gesture view] superview]].x;
        CGFloat xVelocity = [gesture velocityInView: [[gesture view] superview]].x;

        // Pan threshold is currently set to 8.0. 
        // 8.0 is a decent level for slow panning
        // for fast panning 2.0 is more reasonable
        changedLevel = ceilf((xTranslation / panThreshold) + originalLevel); // int changedLevel

        // Raw velocity seems to go from around 3 (slow)
        // to over 200 (fast)
        NSLog(@"raw velocity = %f", xVelocity);

        if (changedLevel >= 15 && changedLevel <= 100) {
            swipedCell.levelNumber.text = [NSString stringWithFormat:@"%i", changedLevel];
            swipedCell.meter.frame = [self updateMeter: changedLevel];

        }
    }

    if ([gesture state] == UIGestureRecognizerStateEnded || [gesture state] == UIGestureRecognizerStateCancelled) {
        if (changedLevel >= 15 && changedLevel <= 100) {
            //... Save the values...            
        }

    }
}

Any help would be greatly appreciated. Thank you.


回答1:


In my experience, the velocityInView: of a pan gesture recognizer isn't important until the user lifts their finger(s) and the recognizer finishes. At that point, you can use the velocity to calculate an animation duration to move your views.

Just stick with translationInView: until the state is UIGestureRecognizerStateEnded and then use velocityInView: to animate any onscreen view changes.



来源:https://stackoverflow.com/questions/9093544/making-use-of-velocityinview-with-uipangesturerecognizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!