首页 > 代码库 > iOS开发UI篇—手势识别器(长按+轻扫)

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 @end15 16 @implementation YYViewController17 18 - (void)viewDidLoad19 {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.添加手势识别器到view33     [self.iconView addGestureRecognizer:longpress];34     35     //4.监听手势识别器36     [longpress addTarget:self action:@selector(longpressView)];37 }38 39 -(void)longpressView40 {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 @end15 16 @implementation YYViewController17 18 - (void)viewDidLoad19 {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)swipeView41 {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 @end15 16 @implementation YYViewController17 18 - (void)viewDidLoad19 {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)abc57 {58 }59 -(void)swipeleftView60 {61     NSLog(@"手指向左轻扫");62 }63 64 -(void)swiperightView65 {66     NSLog(@"手指向右轻扫");67 }68 -(void)swipedownView69 {70     NSLog(@"手指向下轻扫");71 }72 -(void)swipeupView73 {74     NSLog(@"手指向上轻扫");75 }76 @end