Modem IOS () UI-gesture event rotation _ zooming _ Drag, iosui
CAT/CAT sharing, must be excellent
For Original Articles, please reprint them. Reprinted Please note: Yan Nai-yu's blog
Http://blog.csdn.net/u013357243? Viewmode = contents
Source code: http://blog.csdn.net/u013357243/article/details/45560213
Effect
Click, drag, and rotate an image.
Drag-and-drop design ideas:
The first is the simplest drag
// Drag-(void) panTest {UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] init]; [self. iconView addGestureRecognizer: pan]; [pan addTarget: self action: @ selector (panView :)];}-(void) panView :( UIPanGestureRecognizer *) pan {// The returned value is the point at which the finger is pressed as the origin // 1 2 3 4 5 CGPoint = [pan translationInView: pan. view]; NSLog (@ "drag event % @", NSStringFromCGPoint (point); CGPoint temp = self. iconView. center; temp. x + = point. x; temp. y + = point. y; self. iconView. center = temp; // if you cannot understand it, remember OK [pan setTranslation: CGPointZero inView: pan. view];}
Rotation:
-(Void) rotationTest {// rotate UIRotationGestureRecognizer * gesture = [[UIRotationGestureRecognizer alloc] init]; gesture. delegate = self; [self. iconView addGestureRecognizer: gesture]; [gesture addTarget: self action: @ selector (rotationView :)];}-(void) rotationView :( UIRotationGestureRecognizer *) gesture {// NSLog (@ "rotation event %. 1f ", gesture. rotation); // each time starting from the initial position // self. iconView. transform = CGAffineTransformMakeRotation (gesture. rotation); // increments the input transform to a radian self. iconView. transform = CGAffineTransformRotate (self. iconView. transform, gesture. rotation); // clears the rotating radians (note that the radians of the image are not cleared, but the radians of the current finger are cleared) gesture. rotation = 0; // if you cannot understand it, remember it}
Kneading
-(Void) pichTest {// kneading gesture UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] init]; pinch. delegate = self; [self. iconView addGestureRecognizer: pinch]; [pinch addTarget: self action: @ selector (pinchView :)];}-(void) pinchView :( UIPinchGestureRecognizer *) pinch {// NSLog (@ "pinch event %. 1f ", pinch. scale); // self. iconView. transform = CGAffineTransformMakeScale (pinch. scale, pinch. scale); // 1.0*0.9 self. iconView. transform = CGAffineTransformScale (self. iconView. transform, pinch. scale, pinch. scale); pinch. scale = 1.0 ;}
Rotate and zoom simultaneously
In this way, only one gesture can be used, and then in order to allow him to rotate and zoom simultaneously
Add a proxy to determine multiple gestures.
Proxy implementation
@interface NYViewController ()<UIGestureRecognizerDelegate>
// The BOOL value returned by this method determines whether the view can respond to multiple gestures simultaneously-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer combined :( UIGestureRecognizer *) otherGestureRecognizer {NSLog (@ "% @-% @", gestureRecognizer. class, otherGestureRecognizer. class); return YES ;}
Note:
There is something to be aware.
The UIImageView must be checked to enable interaction.
Some algorithms in the middle are not much to be said. There are pictures in the previous blog. If you are interested, please take a look. Cats and cats are generally used directly or guessed by feeling (this is not recommended for everyone to learn ...)