轻扫手势/平移、长按手势

(1)创建故事板

(2)在ViewController.h中设置全局_myView

(3)ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _myView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 320, 400)];
    _myView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_myView];
    
    /*__________________________________手势的使用_____________________________________*/
    //_________________________点击手势__________________________
    //单击
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1Action:)];
    //设置点击的次数,默认是1
    tap1.numberOfTapsRequired = 1;
    //设置点击的手指个数
    tap1.numberOfTouchesRequired = 1;
    //将点击的手势添加到视图上
    [_myView addGestureRecognizer:tap1];
    
    //双击
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap2Action:)];
    //设置点击的次数
    tap2.numberOfTapsRequired = 2;
    //设置手指个数
    tap2.numberOfTouchesRequired = 1;
    //添加到视图上
    [_myView addGestureRecognizer:tap2];
    
    //如果我们双击则取消单击相应事件(注意:需要取消的手势放在前面)
    [tap1 requireGestureRecognizerToFail:tap2];
    
    //_________________________轻扫手势__________________________
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    //设置轻扫的方向
    /*
     UISwipeGestureRecognizerDirectionRight  右(默认)
     UISwipeGestureRecognizerDirectionLeft      左
     UISwipeGestureRecognizerDirectionUp    上
     UISwipeGestureRecognizerDirectionDown  下
     */
    swipe.direction = UISwipeGestureRecognizerDirectionUp;
    [_myView addGestureRecognizer:swipe];
    
    //_________________________平移手势__________________________
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [_myView addGestureRecognizer:pan];

    //_________________________长按手势__________________________
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    //设置长按的最短时间
    longPress.minimumPressDuration = 2;
    [_myView addGestureRecognizer:longPress];
    
    //_________________________旋转手势__________________________
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [_myView addGestureRecognizer:rotation];
    
    //_________________________捏合手势__________________________
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [_myView addGestureRecognizer:pinch];
    
}

#pragma mark - 手势响应事件

//单击相应的事件
- (void)tap1Action:(UITapGestureRecognizer *)tap {

    //取得点击手指的个数
//    NSInteger count = tap.numberOfTouches;
    
    CGPoint point = [tap locationInView:_myView];
    NSString *str = NSStringFromCGPoint(point);
    
    NSLog(@"单击了,坐标是:%@",str);
    
}

- (void)tap2Action:(UITapGestureRecognizer *)tap {
    NSLog(@"双击了");
}

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {

    if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
        NSLog(@"轻扫了");
    }
    
}

//手指移动的时候会不停的调用这个方法:注意此时轻扫不能使用了
- (void)panAction:(UIPanGestureRecognizer *)pan {

    
    
    //可以通过手势取得手势所在的视图
    CGPoint point = [pan locationInView:pan.view];
//    NSString *str = NSStringFromCGPoint(point);
    
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(point.x, point.y, 5, 5)];
//    view.backgroundColor = [UIColor redColor];
//    [pan.view addSubview:view];
    
//    NSLog(@"平移坐标:%@",str);
    
}

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {

    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始了");
    }else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"长按结束了");
    }
}

//旋转的时候一直调用
- (void)rotationAction:(UIRotationGestureRecognizer *)rt {

//    NSLog(@"旋转了");
    
    /*
     180/M_PI = 角度/r;
     
     角度 = 180*r/M_PI;
     */
    
    CGFloat r = rt.rotation;
    
//    角度
    float jd = 180/M_PI*r;
    
    NSLog(@"jd:%.2f",jd);
    
}

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {

//    NSLog(@"捏合");
    //取得缩放的倍数
    CGFloat scale = pinch.scale;
    
//    NSLog(@"scale:%.2f",scale);
    
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, scale, scale);
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值