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(@"扫啥扫");
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- jQuery中的常用事件总结
- C#实现WinForm捕获最小化事件的方法
- c#事件使用示例详解
- C#中的事件介绍
- C#实现自定义双击事件
- extjs 为某个事件设置拦截器
- jQuery阻止同类型事件小结
- jQuery bind事件使用详解
- jQuery支持添加事件的日历特效代码分享(3种样式)
- C#中委托和事件的区别实例解析
- SQLSERVER中得到执行计划的两种方式
- C#中事件的动态调用实现方法
- ASP.NET中实现定制自己的委托和事件参数类