UI中的七種手勢,UI七種手勢

來源:互聯網
上載者:User

UI中的七種手勢,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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.