How to reduce velocity of pinch-zoom UIGestureRecognizer

落爺英雄遲暮 提交于 2019-12-05 13:15:27

Did you tried to scale the current value to the 80%.

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || 
        [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

    CGFloat maxScale = 2.0;
    CGFloat currentScale = [gestureRecognizer scale];
    currentScale = 0.8 * currentScale;   //80% of scaling

    if(currentScale < 0.8)
        currentScale = 0.8;

    if(currentScale > maxScale)
        currentScale = maxScale;

    [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], currentScale, currentScale);

}

Try this.

- (void)handlePinching:(UIPinchGestureRecognizer *)gestureRecognizer {

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        if (CGRectIsEmpty(self.initalFrame)) {
            self.initalFrame = gestureRecognizer.view.frame;
            // store view's original frame and never change it
        }

        if (self.preScale == 0.f) {
            self.preScale = 1.f; 
        }

        gestureRecognizer.scale = self.preScale;
        // gestureRecognizer and view should share the same scale state at the beginning
    }

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
        [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

        const CGFloat kMaxScale = 2.0;
        const CGFloat kMinScale = 1.0;

        CGFloat newScale = gestureRecognizer.scale;

        newScale = (newScale - self.preScale) * 0.8 + self.preScale;

        newScale = MIN(newScale, kMaxScale);
        newScale = MAX(newScale, kMinScale);


        CGRect newFrame = self.initalFrame;
        newFrame.size.height *= newScale;
        newFrame.size.width *= newScale;
        gestureRecognizer.view.frame = newFrame;
        self.preScale = newScale;
    }

}

The points are

  • use frame to implement scale.
  • do change to the scale variable's change to slow/speed scaling.

Turns out the excessive speed of the zoom was actually a bug in the linked answer's code. Sadly this doesn't actually answer my actual question though (for which I'd still like an answer!), but it does serve to solve my client's problem.

Note the added line at the bottom that resets the gestureRecognizer back to 1:

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {

        ...

        lastScale = [gestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call  

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