码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发小功能之八:手势的简单使用(6种)以及代理方法

时间:2016-05-01 21:45:50      阅读:458      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

代码:

  1 #import "ViewController.h"
  2 @interface ViewController () <UIGestureRecognizerDelegate>
  4 @property (weak, nonatomic) IBOutlet UIImageView *imageView;
  5 @end
  7 @implementation ViewController
  8 - (void)viewDidLoad {
  9     [super viewDidLoad];
 10     //只要是手势,默认都是相对于原来位置,且默认控制只支持一个手势!!若想支持多个手势,详见下面的代理方法
 11 //    [self setUpRotation];
 12 //    [self setUpPinch];
 13     [self setUpPan];
 14 }
 15 
 16 #pragma mark - 点按手势
 17 - (void)setUpTap
 18 {
 19     //创建点按手势
 20     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
 21     //需要连续点3次才能触发
 22     //    tap.numberOfTapsRequired = 3;
 23     //往imageView上添加点按手势
 24     [self.imageView addGestureRecognizer:tap];
 25     //设置点按手势代理
 26     tap.delegate = self;
 27 }
 28 
 29 - (void)tap:(UITapGestureRecognizer *)tap
 30 {
 31     NSLog(@"%s",__func__);
 32 }
 33 
 34 #pragma mark - 长按手势
 35 //默认触发两次(开始触摸半秒后一次,结束触摸一次)
 36 - (void)setUpLongPress
 37 {
 38     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
 39     [self.imageView addGestureRecognizer:longPress];
 40     longPress.delegate = self;
 41 }
 42 
 43 - (void)longPress:(UILongPressGestureRecognizer *)longPress
 44 {
 45     开始的时候触摸
 46     if (longPress.state == UIGestureRecognizerStateBegan) {
 47        NSLog(@"%s",__func__);
 48     }
 49     结束的时候触摸
 50     if (longPress.state == UIGestureRecognizerStateEnded) {
 51         NSLog(@"%s",__func__);
 52     }
 53 }
 54 
 55 #pragma mark - 轻扫手势
 56 - (void)setUpSwipe
 57 {
 58     //默认轻扫手势往右划屏幕
 59     UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
 60     //向上划
 61     swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
 62     [_imageView addGestureRecognizer:swipeUp];
 63     //一个清扫手势只支持一个方向,如果想支持多个方向,必须创建多个
 64     UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
 65     //向下划
 66     swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
 67     [_imageView addGestureRecognizer:swipeDown];
 68 }
 69 - (void)swipe:(UISwipeGestureRecognizer *)swipe
 70 {
 71     NSLog(@"%s",__func__);
 72 }
 73 
 74 #pragma mark - 旋转手势
 75 //默认传递的角度都相对于最开始的位置
 76 - (void)setUpRotation
 77 {
 78     UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
 79     rotation.delegate = self;
 80     [self.imageView addGestureRecognizer:rotation];
 81 }
 82 - (void)rotation:(UIRotationGestureRecognizer *)rotation
 83 {
 84     //让图片随着手势旋转而旋转,旋转的角度相当于初始位置的值,配合rotation.rotation = 0;无法转动
 85 //    self.imageView.transform = CGAffineTransformMakeRotation(rotation.rotation);
 86     //手势旋转一下能让图片高速旋转N圈,并且不受控制,方向不容易确定。配合rotation.rotation = 0;后角度一直为0
 87     self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);   
 88     //复位
 89     rotation.rotation = 0;
 90     //获取手势旋转的角度
 91     NSLog(@"%f",rotation.rotation);
 92 }
 93 
 94 #pragma mark - 捏合手势
 95 //两根手指放大缩小图片
 96 - (void)setUpPinch
 97 {
 98     UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
 99     pinch.delegate = self;
100     [self.imageView addGestureRecognizer:pinch];
101 }
102 - (void)pinch:(UIPinchGestureRecognizer *)pinch
103 {
104     //放大的比例相对于原始点,注意:这个不能配合pinch.scale = 1使用,否则不能放大
105 //    self.imageView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
106     //随着两手指的距离增大,图片会无规则的迅速的放大或者缩小(此处无法控制放大还是缩小),注意:配合pinch.scale = 1使用会正常的放大和缩小
107     self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
108     //复位
109     pinch.scale = 1;
110     NSLog(@"%f",pinch.scale);
111 }
112 
113 #pragma mark - 拖拽
114 //移动视图
115 - (void)setUpPan
116 {
117     UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
118     [self.imageView addGestureRecognizer:pan];
119 }
120 - (void)pan:(UIPanGestureRecognizer *)pan
121 {
122     //获取手势的移动,相对于原始点
123     CGPoint transP = [pan translationInView:self.imageView];
124     self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
125     //复位
126     [pan setTranslation:CGPointZero inView:self.imageView];
127     NSLog(@"%@",NSStringFromCGPoint(transP));
128 }
129 @end

代理方法:

 1 是否允许同时支持多个手势,默认只支持一个手势,要调用此方法注意设置代理
 2 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
 3 {
 4     return YES;
 5 }
 6 
 7 是否允许开始触发手势
 8 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
 9 {
10     return NO;
11 }
12 
13 是否允许接收手机的触摸(可以控制触摸的范围)
14 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
15 {
16     //获取当前的触摸点
17     CGPoint currentP = [touch locationInView:self.imageView];
18       在图片的左半区域可以接受触摸
19     if (currentP.x < self.imageView.bounds.size.width * 0.5) {
20         return YES;
21     }else {
22         return NO;
23     }
24 }

 

iOS开发小功能之八:手势的简单使用(6种)以及代理方法

标签:

原文地址:http://www.cnblogs.com/hissia/p/5451216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!