開發(fā)中遇到個類似腦圖的一個東西润脸,里面需要支持縮放,下面介紹一下我是怎么處理的
需要的類
UIPinchGestureRecognizer 這個就是縮放的手勢
過程
1.將視圖添加縮放手勢
UIView *customView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
[customView addGestureRecognizer:pinchGestureRecognizer];
相應(yīng)方法
#define MaxSCale 2.0 //最大縮放比例
#define MinScale 0.5 //最小縮放比例
@property (nonatomic,assign) CGFloat totalScale; //<? 用于記錄視圖即時的縮放比例
// 處理縮放手勢
- (void)pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
UIView *view = pinchGestureRecognizer.view;
CGFloat scale = pinchGestureRecognizer.scale;
//放大情況
if(scale > 1.0){
if(self.totalScale > MaxSCale) return;
}
//縮小情況
if (scale < 1.0) {
if (self.totalScale < MinScale) return;
}
if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
pinchGestureRecognizer.scale = 1;
self.totalScale *=scale;
}
}
總結(jié)
最開始倔矾,并沒有想好怎么限制妄均,后來參考別人的辦法,就是在處理手勢的時候 適當?shù)臅r候return哪自。
值得注意的是self.totalScale一定要初始化為 1丰包;