Max/Min Scale of Pinch Zoom in UIPinchGestureRecognizer - iPhone iOS

后端 未结 10 1097
[愿得一人]
[愿得一人] 2020-11-29 17:29

How would I be able to limit the scale of the UIPinchGestureRecognizer to a min and max level? The scale property below seems to be relative to the last known scale (the de

10条回答
  •  庸人自扰
    2020-11-29 18:06

    Thanks, really useful code snippet above clamping to a minimum and maximum scale.

    I found that when I flipped the view first using:

    CGAffineTransformScale(gestureRecognizer.view.transform, -1.0, 1.0); 
    

    it would cause a flicker when scaling the view.

    Let me know what you think but the solution for me was to update the code sample above, and if the view has been flipped (flag set via property) then invert the scale value:

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer     state] == UIGestureRecognizerStateChanged)
    {
        CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];
    
        if(self.isFlipped) // (inverting)
        {
            currentScale *= -1;
        }
    
        CGFloat newScale = 1 -  (self.lastScale - [gestureRecognizer scale]);
    
        newScale = MIN(newScale, self.maximumScaleFactor / currentScale);
        newScale = MAX(newScale, self.minimumScaleFactor / currentScale);
    
        CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
        gestureRecognizer.view.transform = transform;
    
        self.lastScale = [gestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call
    

提交回复
热议问题