您的位置:首页 > 移动开发 > IOS开发

iOS之触摸事件和手势

2015-10-27 20:28 405 查看

一.事件

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(@"扫啥扫");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 手势 事件