2016-08-11 88 views
0

我正在使用捏手勢縮小imageVIew現在我可以設置下限到它的限制,它不會讓用戶縮小。我正在使用下面的代碼。捏手勢的手勢綁定限制IOS

func handlePinch(recognizer : UIPinchGestureRecognizer) 
    { 
     imageView.transform = CGAffineTransformScale(imageView.transform, 
                 recognizer.scale, recognizer.scale) 

     recognizer.scale = 1.0 
} 

回答

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

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) { 
     // Reset the last scale, necessary if there are multiple objects with different scales 
     lastScale = [gestureRecognizer scale]; 
    } 

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

     CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue]; 

     // Constants to adjust the max/min values of zoom 
     const CGFloat kMaxScale = 2.0; 
     const CGFloat kMinScale = 1.0; 

     CGFloat newScale = 1 - (lastScale - [gestureRecognizer scale]); 
     newScale = MIN(newScale, kMaxScale/currentScale); 
     newScale = MAX(newScale, kMinScale/currentScale); 
     CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale); 
     [gestureRecognizer view].transform = transform; 

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