iOS中對圖片的縮放、旋轉、拖拽的手勢整理


iOS中對圖片的縮放、旋轉、拖拽的手勢整理

 1 //首先把手勢添加到圖片中去,同時確保圖片支持點擊和多指
 2   //圖片放大縮小手勢
 3   UIPinchGestureRecognizer *pinGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(changeScale:)];
 4   [self.imageView addGestureRecognizer:pinGesture];
 5   self.imageView.userInteractionEnabled = YES;
 6   self.imageView.multipleTouchEnabled = YES;
 7   
 8   //圖片拖拉手勢
 9   UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(changePoint:)];
10   [self.imageView addGestureRecognizer:panGesture];
11   
12   //圖片旋轉手勢
13   UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateImage:)];
14   [self.imageView addGestureRecognizer:rotationGesture];
 1 //對應上面的三種手勢
 2 - (void)changeScale:(UIPinchGestureRecognizer *)sender {
 3   UIView *view = sender.view;
 4   if (sender.state == UIGestureRecognizerStateBegan || sender.state == UIGestureRecognizerStateChanged) {
 5     view.transform = CGAffineTransformScale(view.transform, sender.scale, sender.scale);
 6     sender.scale = 1.0;
 7   }
 8 }
 9 
10 - (void)changePoint:(UIPanGestureRecognizer *)sender {
11   UIView *view = sender.view;
12   if (sender.state == UIGestureRecognizerStateBegan || sender.state == UIGestureRecognizerStateChanged) {
13     CGPoint translation = [sender translationInView:view.superview];
14     [view setCenter:CGPointMake(view.centerX+translation.x, view.centerY+translation.y)];
15     [sender setTranslation:CGPointZero inView:view.superview];
16   }
17 }
18 
19 - (void)rotateImage:(UIRotationGestureRecognizer *)sender {
20   UIView *view = sender.view;
21   if (sender.state == UIGestureRecognizerStateBegan || sender.state == UIGestureRecognizerStateChanged) {
22     view.transform = CGAffineTransformRotate(view.transform, sender.rotation);
23     [sender setRotation:0];
24   }
25 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM