UIImageView实现图片移动,缩放、旋转的代码片段

继承UIImageView,重写init函数。

复制代码
 1 //旋转手势
 2 UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer allor]initWithTarget:selft action:@selector(rotatePiece:)];
 3 [self addGestureRecognizer:rotationGesture];
 4 [rotationGesture release];
 5 
 6 //放大缩小手势
 7 UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scalePiece:)];
 8 [pinchGesture setDelegate:self];
 9 [self addGestureRecognizer:pinchGesture];
10 [pinchGesture release];
复制代码

 

复制代码
 1 - (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
 2     if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
 3         UIView *piece = gestureRecognizer.view;
 4         CGPoint locationInView = [gestureRecognizer locationInView:piece];
 5         CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];
 6         
 7         piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
 8         piece.center = locationInSuperview;
 9     }
10 }
11 
12 - (void)rotatePiece:(UIRotationGestureRecognizer *)gestureRecognizer
13 {
14     [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
15     
16     if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
17         [gestureRecognizer view].transform = CGAffineTransformRotate([[gestureRecognizer view] transform], [gestureRecognizer rotation]);
18         rotate = [gestureRecognizer rotation];
19         isMoveState = NO;
20         [gestureRecognizer setRotation:0];
21     }
22 }
23 
24 - (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer
25 {
26     [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
27     
28     if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
29         [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
30         scale = [gestureRecognizer scale];
31         isMoveState = NO;
32         [gestureRecognizer setScale:1];
33     }
34 }
35 
36 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
37 {
38     if (gestureRecognizer.view != self.view)
39         return NO;
40     
41     if (gestureRecognizer.view != otherGestureRecognizer.view)
42         return NO;
43     
44     if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] || [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
45         return NO;
46     
47     return YES;
48 }
复制代码


//移动方法,仍是继承UIImageView重写Touch
initialPoint为全局CGPoint

复制代码
 1 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
 2 
 3     UITouch *touch = [touches anyObject];
 4 
 5     if ([touch tapCount] == 1)
 6     {
 7         CGPoint currentPoint = [touch locationInView:self];
 8 
 9         if (isOne)
10         {
11             initialPoint = currentPoint;
12             isOne = NO;
13         }
14         CGFloat offsetX = currentPoint.x + self.frame.origin.x - initialPoint.x;
15         CGFloat offsetY = currentPoint.y + self.frame.origin.y - initialPoint.y;
16         self.frame = CGRectMake(offsetX, offsetY, self.frame.size.width, self.frame.size.height);
17     }
18 }
19 
20 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
21 
22     initialPoint = CGPointMake(0, 0);
23     isOne = YES;
24 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值