iOS之触摸事件和手势

一.事件

iOS中ViewController自身提供了一些触发手指触摸事件的方法,在这些触发的方法中我们可以实现自己想要的操作.这些方法如下

1.触摸开始方法

//开始
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"你触摸了屏幕");
}

2.触摸结束方法

//结束
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸结束");
}

3.触摸移动手指方法

//移动
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"你移动了手指");
}

4.触摸中断方法

//中断
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸中断");
}

5.摇晃手机触发的方法

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"开始摇晃");
   
}

6.摇晃结束

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"摇晃结束");
}

二.手势

iOS还提供了一系列的手势来添加到其他空间上实现不同的效果

1.点击手势

//点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchMe)];
    //设置点击次数
    //tap.numberOfTouchesRequired = 3
    tap.numberOfTapsRequired = 3;
    [view addGestureRecognizer:tap];

    -(void)touchMe
    {
        NSLog(@"touch");
    }

2.长按手势

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressMe:)];
    [view addGestureRecognizer:longPress];
    //控制允许滑动的距离
    longPress.allowableMovement = 110;
    //设置长按的时间
    longPress.minimumPressDuration = 2;

-(void)longPressMe:(UILongPressGestureRecognizer *)longress
{
    if (longress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    }else if(longress.state == UIGestureRecognizerStateChanged)
    {
        NSLog(@"滑动");
    }else if(longress.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"滑动结束");
    }
    //NSLog(@"长按");
}

3.轻扫手势

    //轻扫手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeMe:)];
    //设置清扫的方向
    swipe.direction = UISwipeGestureRecognizerDirectionDown;
    [view addGestureRecognizer:swipe];

-(void)swipeMe:(UISwipeGestureRecognizer *)swipec
{
    if (swipec.direction == UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"乡下");
    } else if(swipec.direction ==UISwipeGestureRecognizerDirectionUp){
        NSLog(@"向上");
    }
    NSLog(@"扫啥扫");
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值