Ios中手势的利用
来源:互联网 发布:php 创建远程文件夹 编辑:程序博客网 时间:2024/05/17 00:50
1. 触摸
1.1. 触摸术语
- 事件:在手指与设备的屏幕发生交互时生成,事件记录手势的相关信息
- 轻击:当手指触摸屏幕然后立即离开屏幕不发生任何方向的移动叫做一次轻击
- 手势:如按下、拖动、离开、取消
- 响应者链:屏幕的一切事件的传递流程
1.2. 如何拦截按下事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event {
UITouch*touch = [touches anyObject];
NSUInteger numTaps = [touch tapCount];
//获取按下时的屏幕参数
}
1.3. 如何拦截拖动事件
-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event{
//获取实时的坐标
}
1.4. 如何拦截离开事件
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event {
UITouch*touch = [touches anyObject];
NSUInteger numTaps = [touch tapCount];
}
1.5. 如何拦截取消事件
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent*)event {
UITouch*touch = [touches anyObject];
NSUInteger numTaps = [touch tapCount];
}
1.6. 坐标及大小
- CGPoint:空间的位置{x,y}
- CGSize:尺寸{width,height}
- CGRect:矩形范围
1.7. 练习一:如何判断是否点到了某个控件
- 获取到相对于此控件的CGPoint对象:
- 获取此对象的CGRect :bounds方法
- 判断这个rect是否包含此point
1.8. 练习二:实现拖拽效果
效果图:
实现流程:
- 添加两张图片到界面当中并在屏幕底部添加两个View背景为蓝色和黄色
- 添加touch事件
- 在begin里面获取图片信息
- 通过图片信息再创建一份UIImageView
- 将新UIImageView添加到屏幕当中
- 在move里面获取实时的坐标,将此坐标设置给图片的显示参数
- 在end里面将拖动的图片删除并判断松手的区域是否在区域a或b
- 如果在其中某个区域在此区域里面添加图片
2. 多点触控
2.1. 如何判断多点触控
- 判断touchesBegan的touches参数的count属性即可:
-(void)touchesBegan:(NSSet *)touches
withEvent:(UIEvent*)event{
[touchescount];//获取点击到屏幕的点的数量
}
2.2. 如何获取每个点的坐标
- 判断touchesBegan的touches参数的count属性即可:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if([touchescount]==2) {//识别两点触摸,并记录两点间距离
NSArray*twoTouches=[touches allObjects];
CGPoint p1 = [[twoTouches objectAtIndex:0];
CGPointp2 = [[twoTouches objectAtIndex:1];
}}
2.3. 实现两点的图片缩放流程
- 添加图片到界面显示
- 添加touch相关的事件
- 按下的时候获取两点间的距离
- 在move里面获取实时两点间的距离
- 通过实时两点间的距离和按下时两点间的距离的比值决定图片应该缩放的倍数
- 通过算出的缩放倍数设置给图片
2.4. 两点间距离公式
-(CGFloat)spaceToPoint:(CGPoint)first FromPoint:(CGPoint)two{
//计算两点之间的距离
float x = first.x – two.x;
float y = first.y – two.y;
returnsqrt(x * x + y * y);
}
2.5. 通过算出的缩放倍数设置给图片
if(fabsf(currSpace-originSpace)>=min_offset)
{//两指间移动距离超过min_offset,识别为手势“捏合”
CGFloat s=currSpace/originSpace; //计算缩放比例
[self scaleTo:s];
}
3. 手势
3.1. 都有哪几种手势
1、拍击UITapGestureRecognizer (任意次数的拍击)
2、向里或向外捏UIPinchGestureRecognizer (用于缩放)
3、摇动或者拖拽UIPanGestureRecognizer
4、划动UISwipeGestureRecognizer (以任意方向)
5、旋转UIRotationGestureRecognizer(手指朝相反方向移动)
6、长按UILongPressGestureRecognizer
3.2. 如何使用点击手势:UITapGestureRecognizer
//创建一个手势识别器
UITapGestureRecognizer*oneFingerTwoTaps = [[[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(oneFingerTwoTaps)] autorelease];
//Set required taps and number of touches
[oneFingerTwoTapssetNumberOfTapsRequired:2];
[oneFingerTwoTapssetNumberOfTouchesRequired:1];
//Add the gesture to the view
[[selfview] addGestureRecognizer:oneFingerTwoTaps];
消息方法oneFingerTwoTaps
-(void)oneFingerTwoTaps{
NSLog(@"Action: One finger, twotaps");}
3.3. 如何使用缩放手势:UIPinchGestureRecognizer
UIImageView *vi;
//缩放
- (IBAction)Pinch:(id)sender {
UIPinchGestureRecognizer*pinch=sender;
vi.transform=CGAffineTransformScale(vi.transform,pinch.scale,pinch.scale);
pinch.scale=1; //每次缩放后使值初始化
}
3.4. 如何使用旋转手势:UIRotationGestureRecognizer
//旋转
- (IBAction)Rotation:(id)sender {
UIRotationGestureRecognizer *rotation=sender;
vi.transform=CGAffineTransformRotate(vi.transform, rotation.rotation);
rotation.rotation=0; 注意:每次旋转后进行初始化
}
UIRotationGestureRecognizer*twoFingersRotate =
[[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];
[[selfview] addGestureRecognizer:twoFingersRotate];
-(void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer{
// Convert the radian value to show thedegree of rotation
NSLog(@"Rotation in degrees since lastchange: %f", [recognizer rotation] * (180 / M_PI));
UIImageView旋转
UIImageView *_weiboContentTextView;
_weiboContentTextView.transform=CGAffineTransformMakeRotation(CGFloatangle);传入弧度值
弧度值为:角度**M_PI/180
}
3.5. 如何使用任意方向移动手势:UISwipeGestureRecognizer
//向上移动
UISwipeGestureRecognizer*oneFingerSwipeUp =
[[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];
[oneFingerSwipeUpsetDirection:UISw
ipeGestureRecognizerDirectionUp];//这里有四个方向
[[selfview] addGestureRecognizer:oneFingerSwipeUp];
-(void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer{
CGPoint point = [recognizerlocationInView:[self view]];
NSLog(@"Swipe up - start location:%f,%f", point.x, point.y);
}
3.6. 如何使用长按手势:UILongPressGestureRecognizer
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 1.0; //seconds
lpgr.delegate = self;
[self.view addGestureRecognizer:lpgr];
- Ios中手势的利用
- iOS中手势的添加
- iOS中手势的使用
- ios中上下左右滑动的各种手势
- IOS-开发中手势的处理
- IOS中手势密码界面的实现
- iOS中常用的六种手势
- UI: UIGestureRecognizer IOS中手势的用法
- IOS 开发中手势的基本用法
- ios中平移手势的跳动问题
- iOS中关于手势
- iOS中手势之拖动手势UIPanGestureRecognizer
- ios手势的综合
- iOS 手势的用法
- iOS的手势控制
- iOS 手势的学习
- iOS的手势
- IOS的手势检测
- Java 静态代码块与非静态代码块的区别
- sybase数据库bcp导入数据乱码、换行问题
- drp项目管理日志
- ZOJ 1311 Network 割顶
- sql_查询一张表中的重复数据
- Ios中手势的利用
- 如何在点击浏览器前进、后退键时刷新页面而不读取缓存
- 程序员的职场潜意识Top10
- 关于typedef的用法总结
- C#遍历访问Dictionary和HashTable
- 定义和实现一个类的成员函数为回调函数
- Java基础复习:Map集合常用类 && Collections类 && Arrays类
- 职场交流:病从口入,祸从口出!
- Ios中Animation动画的应用