個人iOS 學習 - 學習基本手勢

iOS設計手勢符合人的操做習慣,提供了良好的用戶體驗。dom

UIGestureRecognizer 手勢抽象類,實現類 :學習

  1. UITapGestureRecognizer  輕擊測試

  2. UILongPressGestureRecognizer  長按動畫

  3. UISwipeGestureRecognizer  輕掃ui

  4. UIPanGestureRecognizer  拖動spa

  5. UIPinchGestureRecognizer  捏合縮放設計

  6. UIRotationGestureRecognizer  旋轉rest

 下面是示例,簡單的建立一個view,測試使用手勢code

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     // Do any additional setup after loading the view, typically from a nib.
 4     
 5     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 300, 300) ];
 6     view.backgroundColor = [UIColor greenColor];
 7     
 8     /** tap 輕擊手勢 **/
 9     /**
10     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeBackgroundByTap:)]; //tap 手勢
11     tapGesture.numberOfTapsRequired = 2; // tap觸發次數
12     tapGesture.numberOfTouchesRequired = 2; // tap手指數
13    
14     [view addGestureRecognizer:tapGesture]; //view 增長手勢
15     **/
16     
17     /** longPress 長按手勢 **/
18     /**
19     UILongPressGestureRecognizer *longpressGresture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(changeBackgroundByLongPress:)];
20      
21     [view addGestureRecognizer:longpressGresture];
22     **/
23     
24     /** swipe 親掃手勢 **/
25     /**
26     UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeBackgroundBySwipe:)];
27     swipeGesture.direction = UISwipeGestureRecognizerDirectionDown; // 輕掃方向
28      
29      [view addGestureRecognizer:swipeGesture];
30     **/
31     
32     /** pan 拖動手勢 **/
33     /**
34     UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
35     
36     [view addGestureRecognizer:panGesture];
37     **/
38     
39     /** pinch 縮放手勢 **/
40     /**
41     UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
42     
43     [view addGestureRecognizer:pinchGesture];
44     **/
45     
46     /** rotation 旋轉手勢 **/
47     UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
48     
49     [view addGestureRecognizer:rotationGesture];
50     
51     [self.view addSubview: view];
52 }

 

 手勢對應 action orm

 1 #pragma mark -
 2 #pragma mark  tap 輕擊手勢
 3 - (void)changeBackgroundByTap:(UITapGestureRecognizer *)tapGesture {
 4     UIView *view = tapGesture.view; //取得手勢做用的view視圖
 5     view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 /255.0 green:arc4random() % 256 /255.0 blue:arc4random() % 256 /255.0 alpha:1.0];
 6 }
 7 
 8 #pragma mark -
 9 #pragma mark  longPress 長按手勢
10 - (void) changeBackgroundByLongPress:(UILongPressGestureRecognizer *)longPressGesture {
11     UIView *view = longPressGesture.view;
12     
13     // 這裏判斷狀態,否則會調用兩次
14     if (longPressGesture.state == UIGestureRecognizerStateBegan) {
15         view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 /255.0 green:arc4random() % 256 /255.0 blue:arc4random() % 256 /255.0 alpha:1.0];
16     }
17 }
18 
19 #pragma mark -
20 #pragma mark  swipe 輕掃手勢
21 - (void) changeBackgroundBySwipe:(UISwipeGestureRecognizer *)swipeGesture {
22     UIView *view = swipeGesture.view;
23     view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 /255.0 green:arc4random() % 256 /255.0 blue:arc4random() % 256 /255.0 alpha:1.0];
24 }
25 
26 #pragma mark -
27 #pragma mark  pan 拖動手勢
28 - (void) panAction:(UIPanGestureRecognizer *)panGesture {
29     UIView *view = panGesture.view;
30     CGPoint offset = [panGesture translationInView:view];
31     NSLog(@"pan 位移 = %@", NSStringFromCGPoint(offset));
32     
33     view.transform = CGAffineTransformMakeTranslation(offset.x, offset.y); // 設置transfrom實現手勢拖動,
34 }
35 
36 #pragma mark -
37 #pragma mark  pinch 縮放手勢
38 - (void) pinchAction:(UIPinchGestureRecognizer *)pinchGesture {
39     CGFloat pinchScale = pinchGesture.scale;
40     NSLog(@"pinchSale 縮放比例 = %f", pinchScale);
41     
42     pinchGesture.view.transform = CGAffineTransformMakeScale(pinchScale, pinchScale); //設置transform實現手勢縮放
43 }
44 
45 #pragma mark -
46 #pragma mark  rotation 旋轉手勢
47 - (void) rotationAction:(UIRotationGestureRecognizer *)rotationGesture {
48     CGFloat rotation = rotationGesture.rotation; // 旋轉弧度
49     NSLog(@"rotation 弧度 = %f", rotation);
50     
51     rotationGesture.view.transform = CGAffineTransformMakeRotation(rotation); //設置transform實現手勢旋轉
52 }

 

以上是iOS手勢的基本操做,視圖的transform涉及到動畫的知識,還在學習中。。。

相關文章
相關標籤/搜索