继承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 }