我的iOS 学习 - 学习基本手势

iOS设计手势符合人的操作习惯,提供了良好的用户体验。

UIGestureRecognizer 手势抽象类,实现类 :

  1. UITapGestureRecognizer  轻击

  2. UILongPressGestureRecognizer  长按

  3. UISwipeGestureRecognizer  轻扫

  4. UIPanGestureRecognizer  拖动

  5. UIPinchGestureRecognizer  捏合缩放

  6. UIRotationGestureRecognizer  旋转

 下面是示例,简单的创建一个view,测试使用手势

 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 

 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涉及到动画的知识,还在学习中。。。

 

 

 

转载于:https://www.cnblogs.com/hu-liang/p/4637839.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值