iOS中ViewController自身提供了一些触发手指触摸事件的方法,在这些触发的方法中我们可以实现自己想要的操作.这些方法如下
//开始
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"你触摸了屏幕");
}
//结束
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸结束");
}
//移动
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"你移动了手指");
}
//中断
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸中断");
}
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"开始摇晃");
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"摇晃结束");
}
iOS还提供了一系列的手势来添加到其他空间上实现不同的效果
//点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchMe)];
//设置点击次数
//tap.numberOfTouchesRequired = 3
tap.numberOfTapsRequired = 3;
[view addGestureRecognizer:tap];
-(void)touchMe
{
NSLog(@"touch");
}
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(@"长按");
}
//轻扫手势
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(@"扫啥扫");
}