iOS开发UI篇—手势识别器(长按+轻扫)

iOS开发UI篇—手势识别器(长按+轻扫)

一、长按事件

 1 //
 2 //  YYViewController.m
 3 //  03-长按
 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 UIView *iconView;
13 
14 @end
15 
16 @implementation YYViewController
17 
18 - (void)viewDidLoad
19 {
20     [super viewDidLoad];
21     
22     //长按事件
23     //1.创建一个手势识别器对象
24     UILongPressGestureRecognizer *longpress=[[UILongPressGestureRecognizer alloc]init];
25 
26     //2.设置长按手势识别器的属性
27     //设置最小停留时间
28 //    longpress.minimumPressDuration=3;
29     //手指按下后事件响应前允许手指移动的偏移量
30     longpress.allowableMovement=50;
31     
32     //3.添加手势识别器到view
33     [self.iconView addGestureRecognizer:longpress];
34     
35     //4.监听手势识别器
36     [longpress addTarget:self action:@selector(longpressView)];
37 }
38 
39 -(void)longpressView
40 {
41     NSLog(@"发生了长按事件");
42 }
43 
44 @end

步骤

(1)创建手势识别器

(2)添加手势识别器

(3)监听

 

 

 

 

二、轻扫事件

 1 //
 2 //  YYViewController.m
 3 //  04-轻扫
 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 UIView *iconView;
13 
14 @end
15 
16 @implementation YYViewController
17 
18 - (void)viewDidLoad
19 {
20     [super viewDidLoad];
21     
22     //1.创建手势识别器(轻扫事件)
23     UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]init];
24     
25     //2.设置手势识别器的属性
26     //默认为向右
27     //设置手指向右轻扫的时候触发
28 //    swipe.direction=UISwipeGestureRecognizerDirectionUp;
29     //设置向下和向上两个方向轻扫
30     swipe.direction=UISwipeGestureRecognizerDirectionDown|UISwipeGestureRecognizerDirectionUp;
31     
32     
33     //3.添加手势识别器
34     [self.iconView addGestureRecognizer:swipe];
35     
36     //4.监听手势
37     [swipe addTarget:self action:@selector(swipeView)];
38 }
39 
40 -(void)swipeView
41 {
42     NSLog(@"手指在屏幕上轻扫");
43 }
44 
45 @end

  提示:swipe.direction=UISwipeGestureRecognizerDirectionDown|UISwipeGestureRecognizerDirectionUp;这种方式最多只支持个方向的清扫。
设置支持四个方向的轻扫事件,代码如下:

 1 //
 2 //  YYViewController.m
 3 //  05-轻扫2
 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 @end
15 
16 @implementation YYViewController
17 
18 - (void)viewDidLoad
19 {
20     [super viewDidLoad];
21     
22     //设置四个方向的轻扫
23     //向左
24     UISwipeGestureRecognizer *swipeleft=[[UISwipeGestureRecognizer alloc]init];
25     //设置轻扫的方向
26     swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
27     [self.iconView addGestureRecognizer:swipeleft];
28     [swipeleft addTarget:self action:@selector(swipeleftView)];
29     
30     //向右
31     UISwipeGestureRecognizer *swiperight=[[UISwipeGestureRecognizer alloc]init];
32     //设置轻扫的方向
33     swiperight.direction=UISwipeGestureRecognizerDirectionRight;
34     [self.iconView addGestureRecognizer:swiperight];
35     [swiperight addTarget:self action:@selector(swiperightView)];
36     
37     //向下
38     UISwipeGestureRecognizer *swipedown=[[UISwipeGestureRecognizer alloc]init];
39        //设置轻扫的方向
40     swipedown.direction=UISwipeGestureRecognizerDirectionDown;
41     [self.iconView addGestureRecognizer:swipedown];
42     [swipedown addTarget:self action:@selector(swipedownView)];
43     
44     //向上
45     UISwipeGestureRecognizer *swipeup=[[UISwipeGestureRecognizer alloc]init];
46        //设置轻扫的方向
47     swipeup.direction=UISwipeGestureRecognizerDirectionUp;
48     [self.iconView addGestureRecognizer:swipeup];
49     [swipeup addTarget:self action:@selector(swipeupView)];
50     
51     //简略写法
52   [ self.iconView addGestureRecognizer: [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(abc)] ];
53     
54 }
55 
56 -(void)abc
57 {
58 }
59 -(void)swipeleftView
60 {
61     NSLog(@"手指向左轻扫");
62 }
63 
64 -(void)swiperightView
65 {
66     NSLog(@"手指向右轻扫");
67 }
68 -(void)swipedownView
69 {
70     NSLog(@"手指向下轻扫");
71 }
72 -(void)swipeupView
73 {
74     NSLog(@"手指向上轻扫");
75 }
76 @end

 

iOS开发UI篇—手势识别器(长按+轻扫),,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。