Seven gestures in the UI and seven gestures in the UI

Source: Internet
Author: User

Seven gestures in the UI and seven gestures in the UI

1 // 2 // GestureRecognizerViewController. m

10 11 # import "GestureRecognizerViewController. h "12 # import" UIColor + RandomColor. h "13 @ interface GestureRecognizerViewController () 14 {15 16 CGRect _ frame; // used to record the original view frame 17 18} 19 @ end 20 21 @ implementation GestureRecognizerViewController 22 23-(void) viewDidLoad {24 [super viewDidLoad]; 25 // Do any additional setup after loading the view. 26 // UIGestureRecognizer is the base class of all Gesture Recognition classes. It provides the basic functions of the gesture recognition tool. With the gesture recognition tool, all Gesture Recognition is identified by this class, so we no longer care about the gesture recognition process, we only need to care about what operations should be done after Gesture Recognition. There are 6 sub-classes of the sub-class, including Pat gesture, kneading gesture, long-pressed gesture, light-scanning gesture, rotation gesture, and translation gesture, and sub-screen edge gesture 27 28 UIView * view = [[UIView alloc] initWithFrame :( CGRectMake (60,184,200,200)]; 29 view. backgroundColor = [UIColor redColor]; 30 [self. view addSubview: view]; 31 [view release]; 32 33 //! This kind of gesture is used most often, like a button 34 35 // UITapGestureRecognizer 36/* 37 // 1. create a pat gesture object 38. Optional * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (handleTapGesture :)]; // self indicates the object of the View Controller 39 40 // 2. set the number of clicks required for the Pat trigger method // you must set 41 tapGesture before adding a gesture. numberOfTapsRequired = 2; 42 tapGesture. numberOfTouchesRequired = 2; 43 44 // 3. add a gesture 45 [view addGestureRecognizer: tapGesture] To the view object; // The multi-state parent class Pointer Points to the subclass object 46 // [view addGestureRecognizer: <# (UIGestureRecognizer *) #>] 47 48 [tapGesture release]; 49 */50 51/** 52 1. create an object for the gesture recognition class (Pat gesture/long-pressed gesture/swipe gesture/PAN gesture/pinch gesture/rotate gesture/screen edge gesture) 53 54 2. set the relevant attributes of the gesture method (for example, how long it takes to trigger a gesture event with a few fingers) // 55 56 3 as needed. add gesture 57 58 to view object 4. release the gesture object 59 60 61 */62 63 // The long-pressed gesture seek 64/* 65 then * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (handleLongPressGesture) 66 // set the second of the trigger event with the shortest of the long-pressed gesture to 67 longPressGesture. minimumPressDuration = 1; 68 69 [view addGestureRecognizer: longPressGesture]; 70 71 [longPressGesture release]; 72 */73 74 // swipe gesture UISwipeGestureRecognizer 75 76 running * swipeGesture = [[using alloc] initWithTarget: self action: @ selector (handleSwipeGesture :)]; 77 78 // set the method supported by the swipe gesture (scan right by default) 79 // uiswipegesturerecognizerdiredown down scan 80 swipeGesture. direction = UISwipeGestureRecognizerDirectionDown; // you must set 81 82 [view addGestureRecognizer: swipeGesture]; 83 84 [swipeGesture release] before adding a gesture; 85 86 // you can add multiple gestures to a view. 87 // you can configure multiple gesture swipe directions: then add a swipe gesture 88 UISwipeGestureRecognizer * swipe = [[Alibaba alloc] initWithTarget: self action: @ selector (handleSwipe :)]; 89 // rotate to sweep 90 swipe left. direction = direction; 91 92 [view addGestureRecognizer: swipe]; 93 [swipe release]; 94 95 96 // translation gesture limit 97 98 running * panGesture = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (handpolicangesture :)]; 99 100 [view addGestureRecognizer: panGesture]; 101 [panGesture release]; 102 103 104 105 // kneading gesture limit 107 UIPinchGestureRecognizer * pinchGesture = [[using alloc] initWithTarget: self action: @ selector (handlePinchGesture :)]; 108 109 // pinchGesture. scale110 111 [view addGestureRecognizer: pinchGesture]; 112 [pinchGesture release]; 113 114 115 116 117 // rotate gesture 118 // rotate role * rotationGesture = [UIRotationGestureRecognizer alloc] initWithTarget: self action: @ selector (handleRotationGesture :)]; 120 [view addGestureRecognizer: rotationGesture]; 121 [rotationGesture release]; 122 123 124 // The screen edge gesture seek is a subclass of UIPanGestureRecognizer 125 126 running * screenEdgePanGesture = [[using alloc] initWithTarget: self action: @ selector (handleScreenEdgeGesture :)]; 127 128 // supports the method of setting the screen edge gesture 129 screenEdgePanGesture. edges = UIRectEdgeRight; 130 // The position must overlap with the screen edge by 131 view. frame = CGRectMake (120, 50,200,200); 132 // enables the view to return to the original position after each move to implement 133 _ frame = view in the action method. frame; 134 135 [view addGestureRecognizer: screenEdgePanGesture]; 136 137 138 139 [screenEdgePanGesture release]; 140 141 142} 143 # pragma mark-pat gesture method-(void) handleTapGesture :( UITapGestureRecognizer *) tapGesture {144 145 // gets the view object 146 tapGesture where the Pat gesture is located. view. backgroundColor = [UIColor randomColor]; 147 148} 149 150 151 152 # pragma mark-Long-pressed gesture method 153-(void) handleLongPressGesture :( UILongPressGestureRecognizer *) longPressGesture {154 155 // UIGestureRecognizerStateBegan when the condition is reached (longPressGesture. minimumPressDuration = 1;) method event 156 // UIGestureRecognizerStateChanged when the condition is reached, slide, trigger 157 // UIGestureRecognizerStateEnded when the condition is reached, finger exit, trigger 158 159 // execute 160 if (longPressGesture. state = UIGestureRecognizerStateEnded) {161 162 longPressGesture. view. superview. backgroundColor = [UIColor randomColor]; 163 164} 165 166} 167 168 # pragma mark-Light Scan gesture method 169-(void) handleSwipeGesture :( optional *) swipeGesture {170 171 swipeGesture. view. backgroundColor = [UIColor randomColor]; 172 173} 174 175-(void) handleSwipe :( UISwipeGestureRecognizer *) swipe {176 177 swipe. view. backgroundColor = [UIColor randomColor]; 178} 179 180 181 182 # pragma mark-translation gesture method 183-(void) handpolicangesture :( UIPanGestureRecognizer *) panGesture {184 185/1. get the translation increment 186 CGPoint point = [panGesture translationInView: panGesture. view]; 187 // 2. moving the position of a view. The preceding view is used as the reference. The transform affine transformation technology (moving all vertices in the view) uses the linear algebra knowledge of 188 panGesture. view. transform = CGAffineTransformTranslate (panGesture. view. transform, point. x, point. y); 189 // 3. set the last translation increment to 0190 // CGPointZero represents a (0, 0) structure CGPointMake (0, 0) 191 [panGesture setTranslation: CGPointZero inView: panGesture. view]; 192 193 panGesture. view. backgroundColor = [UIColor randomColor]; 194 195} 196 197 # pragma mark-kneading gesture method 198-(void) handlePinchGesture :( UIPinchGestureRecognizer *) pinchGesture {199 200 // 1. the Scale ratio is 201. view. transform = CGAffineTransformScale (pinchGesture. view. transform, pinchGesture. scale, pinchGesture. scale); 202 // 2. set the last scaling ratio to 1203 pinchGesture. scale = 1; // or [pinchGesture setScale: 1]; 204 205 // pinchGesture. view. backgroundColor = [UIColor randomColor]; 206 207} 208 209 # pragma mark-rotation gesture method 210-(void) handleRotationGesture :( UIRotationGestureRecognizer *) rotationGesture {211 // 1. the rotation is performed based on the rotation angle. The rotation is also performed based on the preceding image variables. The rotationGesture degree is 212. view. transform = CGAffineTransformRotate (rotationGesture. view. transform, rotationGesture. rotation); 213 // 2. clears the last rotation angle by 214 rotationGesture. rotation = 0; 215 216 217} 218 219 # pragma mark-edge gesture method 220 // The method is the same as UIPanGestureRecognizer (Pan gesture) 221-(void) handleScreenEdgeGesture :( reboot *) screenEdgeGesture {223 224 // when the finger leaves, the view returns to the original position 225 if (screenEdgeGesture. state = UIGestureRecognizerStateEnded) {226 227 screenEdgeGesture. view. frame = _ frame; 228} 229 230 // 1. get the finger translation increment 231 CGPoint point = [screenEdgeGesture translationInView: screenEdgeGesture. view]; 232 // 2. apply the 233 screenEdgeGesture resolution based on the translation increment. view. transform = CGAffineTransformTranslate (screenEdgeGesture. view. transform, point. x, point. y); 234 // 3. 235 [screenEdgeGesture setTranslation: CGPointZero inView: screenEdgeGesture. view]; 236 237 // screenEdgeGesture. view. frame = _ frame; 238 // NSLog (@ "triggered"); 239 240} 241 242-(void) didReceiveMemoryWarning {243 [super didreceivemorywarning]; 244 // Dispose of any resources that can be recreated.245} 246/* 247 # pragma mark-Navigation248 249 // In a storyboard-based application, you will often want to do a little preparation before navigation250-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {251 // Get the new view controller using [segue destinationViewController]. 252 // Pass the selected object to the new view controller.253} 254 */255 256 @ end

 

Random color

1 //2 //  UIColor+RandomColor.h3 //  UILessonTouch-044 5 #import <UIKit/UIKit.h>6 7 @interface UIColor (RandomColor)8 + (UIColor *)randomColor;9 @end
 1 // 2 //  UIColor+RandomColor.m 3 //  UILessonTouch-04 4  5 #import "UIColor+RandomColor.h" 6 #define kColorValue arc4random_uniform(256) / 255.0 7 @implementation UIColor (RandomColor) 8  9 + (UIColor *)randomColor {10     11     return [UIColor colorWithRed:kColorValue green:kColorValue blue:kColorValue alpha:kColorValue];12     13 }14 15 @end

 

Related Article

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.