标签:
捏合手势目测可以无限放大图片,但是一般情况下,我们只允许用户在一定的比例范围内进行缩放
方案:用一个全局的值记录总的缩放比例,当超出范围后就不允许继续超范围,只能回到缩放范围
如果嫌麻烦这里打包好的分类,三行代码搞定:https://github.com/iOSSinger/SGZoomImage
具体代码如下:
#import "ViewController.h" #define MaxSCale 2.0 //最大缩放比例 #define MinScale 0.5 //最小缩放比例 #import "UIImageView+Zoom.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @property (nonatomic,assign) CGFloat totalScale; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.totalScale = 1.0; UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]; [self.imageView addGestureRecognizer:pinch]; } - (void)pinch:(UIPinchGestureRecognizer *)recognizer{ CGFloat scale = recognizer.scale; //放大情况 if(scale > 1.0){ if(self.totalScale > MaxSCale) return; } //缩小情况 if (scale < 1.0) { if (self.totalScale < MinScale) return; } self.imageView.transform = CGAffineTransformScale(self.imageView.transform, scale, scale); self.totalScale *=scale; recognizer.scale = 1.0; } @end
标签:
原文地址:http://www.cnblogs.com/yyxios/p/4822236.html