简单的手势的使用

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIView *gestureView = [[UIView alloc] initWithFrame:CGRectMake(10, 70, 350, 550)];
    gestureView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:gestureView];

    /*----------------------点击手势--------------------*/

    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1Action:)];
    tap1.numberOfTapsRequired = 1;//单击
    tap1.numberOfTouchesRequired = 1;//点击使用的手指个数
    [gestureView addGestureRecognizer:tap1];

    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap2Action:)];
    tap2.numberOfTapsRequired = 2;//双击
    [gestureView addGestureRecognizer:tap2];

    [tap1 requireGestureRecognizerToFail:tap2];

    /*----------------------轻扫手势--------------------*/
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    //轻扫方向
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [gestureView addGestureRecognizer:swipe];

    /*----------------------平移手势--------------------*/
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [gestureView addGestureRecognizer:pan];

    /*----------------------长按手势--------------------*/
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    longPress.minimumPressDuration = 3;//长按的最短时间
    [gestureView addGestureRecognizer:longPress];

    /*----------------------旋转手势--------------------*/
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    [gestureView addGestureRecognizer:rotation];

    /*----------------------捏合手势--------------------*/
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [gestureView addGestureRecognizer:pinch];

}

- (void)tap1Action:(UITapGestureRecognizer *)tap1
{
    NSLog(@"单击");
}
- (void)tap2Action:(UITapGestureRecognizer *)tap2
{
    NSLog(@"双击");
}
//轻扫
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    NSLog(@"轻扫");
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
    {
        NSLog(@"向上轻扫");
    }
}
//平移
- (void)panAction:(UIPanGestureRecognizer *)pan{
//    NSLog(@"平移");
//
//    CGPoint p = [pan locationInView:pan.view];
//    NSLog(@"%@", NSStringFromCGPoint(p));
}
//长按
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"开始长按");
    }else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"结束长按");
    }
}
//旋转
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
    //旋转角度,弧度
    NSLog(@"旋转");
    CGFloat r = rotation.rotation;
    NSLog(@"r is %.2f", 180 / M_PI * r);

}
//捏合
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
    //获得捏合比例
    NSLog(@"捏合");
    CGFloat scale = pinch.scale;
    pinch.view.transform = CGAffineTransformMakeScale(scale, scale);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值