IOS development UI-gesture reader (drag/drop + rotation + scaling)

Source: Internet
Author: User

1. Drag the sample code: Copy code 1 // 2 // YYViewController. m 3 // 06-drag event 4 // 5 // Created by apple on 14-6-19. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import "YYViewController. h "10 11 @ interface YYViewController () 12 @ property (strong, nonatomic) IBOutlet UIView * iconView; 13 14 @ end15 16 @ implementation YYViewController17 18-(void) viewDidLoad19 {20 [super viewDidLoad]; 21 22 // drag event 23 UIP AnGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] init]; 24 [self. iconView addGestureRecognizer: pan]; 25 [pan addTarget: self action: @ selector (panView :)]; 26} 27 28-(void) panView :( UIPanGestureRecognizer *) pan29 {30 // use the upper left corner of the view on the Controller as the coordinate origin 31 CGPoint point = [pan locationInView: pan. view]; 32 NSLog (@ "drag event"); 33 NSLog (@ "the position of the obtained touch point is: % @", NSStringFromCGPoint (point )); 34 35 CGPoint point1 = [pan translationInVie W: pan. view]; 36 NSLog (@ "drag Event 1"); 37 NSLog (@ "The retrieved touch point is: % @", NSStringFromCGPoint (point1 )); 38 39 // drag your finger to move the custom view to 40 CGPoint temp = self. iconView. center; 41 temp. x + = point1.x; 42 temp. y + = point1.y; 43 self. iconView. center = temp; 44 45 // clear 46 [pan setTranslation: CGPointZero inView: pan. view]; 47} 48 @ end copy code Note: 1. pay attention to the displacement superposition of drag and drop events. Pay attention to the mathematical increment. You must clear the result after each call. 2. Note that the obtained point is based on the point pressed by the finger. CGPoint point1 = [pan translationInView: pan. view]; // use the upper left corner of the view on the Controller as the coordinate origin CGPoint point = [pan locationInView: pan. view]; 2. Sample Code of rotation: Copy code 1 // 2 // YYViewController. m 3 // 07-rotate 4 // 5 // Created by apple on 14-6-19. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import "YYViewController. h "10 11 @ interface YYViewController () 12 @ property (weak, nonatomic) IBOutlet UIImageView * iconView; 1 3 14 @ end15 16 @ implementation YYViewController17 18-(void) viewDidLoad19 {20 [super viewDidLoad]; 21 22 // rotate 23 // create a gesture reader (rotate) 24 UIRotationGestureRecognizer * gesture = [[UIRotationGestureRecognizer alloc] init]; 25 // Add a gesture reader 26 [self. iconView addGestureRecognizer: gesture]; 27 // listener 28 [gesture addTarget: self action: @ selector (gestureView :)]; 29} 30 31-(void) gestureView :( optional *) gesture3 2 {33 34 // rotating radian: gesture. rotation35 NSLog (@ "rotation event, rotation radians: % 1f", gesture. rotation); 36 37 // Let the image rotate with the finger 38 // start from the initial position 39 // self each time. iconView. transform = CGAffineTransformMakeRotation (gesture. rotation); 40 41 // rotate 42 Based on the input transform // rotate the image along with the previous one (remove the automatic layout) 43 // note the problem: rotate 44 self at the tornado speed. iconView. transform = CGAffineTransformRotate (self. iconView. transform, gesture. rotation); 45 // reset the rotating radians to 46 // (note that the current Finger rotation radians cleared) 47 gesture. rotation = 0; 48} 49 @ end copy code Note: 1. imageview is interactive by default and does not support multi-touch. You need to select these two items in storyboard. 2. The rotation degrees are superimposed. 3. After the rotation is cleared, each call starts from scratch. Iii. Zoom copy code 1 // 2 // YYViewController. m 3 // 07-rotate 4 // 5 // Created by apple on 14-6-19. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import "YYViewController. h "10 11 @ interface YYViewController () <strong> 12 @ property (weak, nonatomic) IBOutlet UIImageView * iconView; 13 14 @ end15 16 @ implementation YYViewController17 18-(void) viewDidLoad19 {20 [super viewDidLoad]; 21 [self pinchTest]; 22 [self gestureTest]; 23} 24 25-(void) pinchTest26 {27 // scale 28 UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] init]; 29 [self. iconView addGestureRecognizer: pinch]; 30 [pinch addTarget: self action: @ selector (pinchView :)]; 31 32 // set proxy 33 pinch. delegate = self; 34} 35 36-(void) pinchView :( UIPinchGestureRecognizer *) pinch37 {38 // zoom ratio pinch. scale; 39 NSLog (@ "ZOOM: % f", pinch. scale); 40 // scale the image to 41 // self. iconView. transform = CGAffineTransformMakeScale (pinch. scale, pinch. scale); 42 // scale the image to 43 self based on the existing one. iconView. transform = CGAffineTransformScale (self. iconView. transform, pinch. scale, pinch. scale); 44 // clears 45 pinch. scale = 1.0; 46} 47-(void) gestureTest48 {49 // rotate 50 // create gesture reader (rotate) 51 UIRotationGestureRecognizer * gesture = [[UIRotationGestureRecognizer alloc] init]; 52 // Add the gesture reader 53 [self. iconView addGestureRecognizer: gesture]; 54 // listen to 55 [gesture addTarget: self action: @ selector (gestureView :)]; 56 57 // set proxy 58 gesture. delegate = self; 59} 60-(void) gestureView :( UIRotationGestureRecognizer *) gesture61 {62 63 // rotating radian: gesture. rotation64 NSLog (@ "rotation event, rotation radians: % 1f", gesture. rotation); 65 66 // rotate the image along the finger 67 // each time starting from the initial position 68 // self. iconView. transform = CGAffineTransformMakeRotation (gesture. rotation); 69 70 // rotate 71 based on the input transform // rotate the image along with the previous one (remove the automatic layout) 72 // note the problem: rotate 73 self at the tornado speed. iconView. transform = CGAffineTransformRotate (self. iconView. transform, gesture. rotation); 74 // reset the rotating radians to 75 // (note that the radians of the image are not cleared, but the radians of the current finger rotation are cleared) 76 gesture. rotation = 0; 77} 78 79 // implement proxy method 80-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer runner :( UIGestureRecognizer *) else {82 // NO by default, set YES83 return YES; 84} 85 @ end

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.