你的位置:首页 > 操作系统

[操作系统]UI中的七种手势

 1 // 2 // GestureRecognizerViewController.m 10  11 #import "GestureRecognizerViewController.h" 12 #import "UIColor+RandomColor.h" 13 @interface GestureRecognizerViewController () 14 { 15    16   CGRect _frame; // 用来记录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 手势识别器,是所有手势识别类的基类,提供了手势识别器的基本功能,有了手势识别器之后,手势的识别全部由这个类来识别,我们就不再关心手势识别的过程,我们只需要关心手势识别之后应该做哪些操作,它的子类有6个,轻拍手势,捏合手势,长按手势,轻扫手势,旋转手势,平移手势,以及平移手势的子类 屏幕边缘手势 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   //! 轻拍手势 这种手势用的最多, 跟按钮似的 34   35   // UITapGestureRecognizer 36   /* 37   // 1. 创建轻拍手势对象 38   UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; // self指视图控制器的对象 39    40   // 2. 设置轻拍触发方法时,需要的点击次数 // 一定要在添加手势之前设置 41   tapGesture.numberOfTapsRequired = 2; 42   tapGesture.numberOfTouchesRequired = 2; 43    44   // 3. 向视图对象上添加手势 45   [view addGestureRecognizer:tapGesture]; // 多态 父类指针指向子类对象 46 //  [view addGestureRecognizer:<#(UIGestureRecognizer *)#>] 47    48   [tapGesture release]; 49   */ 50    51   /** 52    1. 创建手势识别类的对象(轻拍手势/ 长按手势/ 轻扫手势/ 平移手势/ 捏合手势/ 旋转手势/ 屏幕边缘手势) 53    54    2. 设置手势方法的相关属性(如需要几根手指,多长时间才能触发手势事件 等)  // 根据需要 55    56    3. 向视图对象上添加手势 57    58    4. 释放手势对象 59    60    61   */ 62    63   // 长按手势 UILongPressGestureRecognizer 64  /* 65   UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)]; 66   // 设置长按手势最短的触发事件 秒为单位 67   longPressGesture.minimumPressDuration = 1; 68    69   [view addGestureRecognizer:longPressGesture]; 70    71   [longPressGesture release]; 72   */ 73    74   // 轻扫手势 UISwipeGestureRecognizer 75    76   UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; 77    78   // 设置轻扫手势支持的方法 (默认是向右扫) 79   // UISwipeGestureRecognizerDirectionDown 向下扫 80   swipeGesture.direction = UISwipeGestureRecognizerDirectionDown; // 一定要在添加手势之前设置 81    82   [view addGestureRecognizer:swipeGesture]; 83    84   [swipeGesture release]; 85    86   // 一个视图可以添加多个手势 87   // 想要实现多个方向轻扫手势: 再添加一个轻扫手势 88   UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 89   // UISwipeGestureRecognizerDirectionLeft 向左轻扫 90   swipe.direction = UISwipeGestureRecognizerDirectionLeft; 91    92   [view addGestureRecognizer:swipe]; 93   [swipe release]; 94    95    96   // 平移手势 UIPanGestureRecognizer 97  98   UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesTure:)]; 99   100   [view addGestureRecognizer:panGesture];101   [panGesture release];102   103   104   105   // 捏合手势 UIPinchGestureRecognizer106   107   UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];108   109 //  pinchGesture.scale110   111   [view addGestureRecognizer:pinchGesture];112   [pinchGesture release];113   114   115   116   // 旋转手势117   118 //  UIRotationGestureRecognizer119   UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationGesture:)];120   [view addGestureRecognizer:rotationGesture];121   [rotationGesture release];122   123   124   // 屏幕边缘手势 UIScreenEdgePanGestureRecognizer 是UIPanGestureRecognizer的子类125   126   UIScreenEdgePanGestureRecognizer *screenEdgePanGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScreenEdgeGesture:)];127   128   // 设置屏幕边缘手势支持方法129   screenEdgePanGesture.edges = UIRectEdgeRight;130   // 必须让位置和屏幕边缘重合131   view.frame = CGRectMake(120, 50, 200, 200);132   // 使每次移动后的view回到原始位置,实现在action方法中133   _frame = view.frame;134   135   [view addGestureRecognizer:screenEdgePanGesture];136   137   138   [screenEdgePanGesture release];139   140   141 }142 #pragma mark - 轻拍手势方法143 - (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture {144   145   // 获取轻拍手势所在的视图对象146   tapGesture.view.backgroundColor = [UIColor randomColor];147   148 }149 150 151 152 #pragma mark - 长按手势方法153 - (void)handleLongPressGesture:(UILongPressGestureRecognizer *)longPressGesture {154   155   // UIGestureRecognizerStateBegan 当达到条件((longPressGesture.minimumPressDuration = 1;))界限时,触发方法事件156   // UIGestureRecognizerStateChanged 当达到条件时,滑动,触发157   // UIGestureRecognizerStateEnded 当达到条件,手指离开,触发158   159   // 根据长按手势的状态执行160   if (longPressGesture.state == UIGestureRecognizerStateEnded) {161     162     longPressGesture.view.superview.backgroundColor = [UIColor randomColor];163 164   }165   166 }167 168 #pragma mark - 轻扫手势方法169 - (void)handleSwipeGesture:(UISwipeGestureRecognizer *)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 - 平移手势方法183 - (void)handlePanGesTure:(UIPanGestureRecognizer *)panGesture {184   185   // 1. 获取平移增量186   CGPoint point = [panGesture translationInView:panGesture.view];187   // 2. 让视图的位置发生移动,以上次视图为基准,transform 仿射变换技术 (view上所有点跟随移动) 用了线性代数的知识188   panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);189   //3. 将上次的平移增量置为0190   //CGPointZero 代表一个(0, 0)的结构体 CGPointMake(0, 0)191   [panGesture setTranslation:CGPointZero inView:panGesture.view];192   193   panGesture.view.backgroundColor = [UIColor randomColor];194   195 }196 197 #pragma mark - 捏合手势方法198 - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinchGesture {199   200   // 1.根据比例做放射变换, 也是以上次视图为基准 Scale 比例201   pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale);202   // 2.将上次的缩放比例置为1203   pinchGesture.scale = 1; // 或者[pinchGesture setScale:1];204   205   //pinchGesture.view.backgroundColor = [UIColor randomColor];206   207 }208 209 #pragma mark - 旋转手势方法210 - (void)handleRotationGesture:(UIRotationGestureRecognizer *)rotationGesture {211   // 1. 根据旋转角度做放射变换,也是以上次视图形变量为基准 rotation 旋转,旋度212   rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation);213   // 2. 将上次的旋转角度清零214   rotationGesture.rotation = 0;215   216   217   218 }219 220 #pragma mark - 边缘手势方法221 // 方法和UIPanGestureRecognizer(平移手势)一样222 - (void)handleScreenEdgeGesture:(UIScreenEdgePanGestureRecognizer *)screenEdgeGesture {223   224   // 手指离开的时候 ,view回到原来的位置225   if (screenEdgeGesture.state == UIGestureRecognizerStateEnded) {226     227     screenEdgeGesture.view.frame = _frame;228   }229   230   // 1.获取手指的平移增量231   CGPoint point = [screenEdgeGesture translationInView:screenEdgeGesture.view];232   // 2.根据平移增量做仿射变换233   screenEdgeGesture.view.transform = CGAffineTransformTranslate(screenEdgeGesture.view.transform, point.x, point.y);234   // 3. 把平移增量置为0的结构体235   [screenEdgeGesture setTranslation:CGPointZero inView:screenEdgeGesture.view];236   237   //screenEdgeGesture.view.frame = _frame;238   //NSLog(@"触发了");239   240 }241 242 - (void)didReceiveMemoryWarning {243   [super didReceiveMemoryWarning];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

 

随机颜色

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