1.实现iOS手势分类
1》�iOS系统在3.2以前用的是UIResponder
2》iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了UIGestureRecognizer类。手势识别UIGestureRecognizer类是个抽象类
2.iOS大概的手势种类
�1.点击(Tap)
�点击作为最常用手势,用于按下或选择一个控件或条目(类似于普通的鼠标点击)
�2.拖动(Drag) 拖动用于实现一些页面的滚动,以及对控件的移动功能。
�3.滑动(Flick) 滑动用于实现页面的快速滚动和翻页的功能。
�4.横扫(Swipe) �横扫手势用于激活列表项的快捷操作菜单
�5.双击(DoubleTap)
�双击放大并居中显示图片,或恢复原大小(如果当前已经放大)。同时,双击能够激活针对文字编辑菜单。
�6.放大(Pinch open)
�放大手势可以实现以下功能:打开订阅源,打开文章的详情。在照片查看的时候,放大手势也可实现放大图片的功能。
�7.缩小(Pinch close)
�缩小手势,可以实现与放大手势相反且对应的功能的功能:关闭订阅源退出到首页,关闭文章退出至索引页。在照片查看的时候,缩小手势也可实现缩小图片的功能。
�8.长按(Touch &Hold)
�在我的订阅页,长按订阅源将自动进入编辑模式,同时选中手指当前按下的订阅源。这时可直接拖动订阅源移动位置。
�针对文字长按,将出现放大镜辅助功能。松开后,则出现编辑菜单。
�针对图片长按,将出现编辑菜单。
�9.摇晃(Shake)
摇晃手势,将出现撤销与重做菜单。主要是针对用户文本输入的。
3.UIGestureRecognizer子类
�UITapGestureRecognizer((点一下)
�UIPinchGestureRecognizer(缩放)
�UIRotationGestureRecognizer(旋转)
�UISwipeGestureRecognizer(滑动,快)
�UIPanGestureRecognizer(拖移,慢)
�UILongPressGestureRecognizer
UIScreenEdgePanGestureRecognizer(屏幕边缘平移手势)
4.实现手势代码
1.[UITapGestureRecognizer] (点击)
UITapGestureRecognizer *tapGes =[[UITapGestureRecognizeralloc] initWithTarget:self action:@selector(handleTap:)];
[imageV addGestureRecognizer:tapGes];
//单机方法 singleRecognizer.numberOfTapsRequired = 1;
//双击方法 doubleRecognizer.numberOfTapsRequired = 2
//双击失败执行单击[singleRecognizer requireGestureRecognizerToFail:doubleRecognizer];
2.�[UIPinchGestureRecognizer](缩放)
UIPinchGestureRecognizer *pinchGes = [[UIPinchGestureRecognizeralloc] initWithTarget: self
action:@selector(handlePinch:)];
[imageVaddGestureRecognizer:pinchGes];
- (void) handlePinch:(UIPinchGestureRecognizer*) pinch{
if (pinch.state==UIGestureRecognizerStateChanged) {
//取到缩放比率
CGFloatscale = pinch.scale;
//缩放
pinch.view.transform=CGAffineTransformMakeScale(scale, scale);
}
else if(pinch.state==UIGestureRecognizerStateEnded) {
[UIViewanimateWithDuration:.5animations:^{
pinch.view.transform=CGAffineTransformIdentity;
}];
}
}
3.�[UIRotationGestureRecognizer](旋转)
//添加旋转手势
UIRotationGestureRecognizer*rotationGes = [[UIRotationGestureRecognizeralloc] initWithTarget:self action:@selector(handleRotation:)];
[imageVaddGestureRecognizer:rotationGes];
- (void) handleRotation:(UIRotationGestureRecognizer*) rotation{
if(rotation.state==UIGestureRecognizerStateChanged) {
//取到弧度
CGFloatangle = rotation.rotation;
//正在旋转
rotation.view.transform=CGAffineTransformMakeRotation(angle);
}else if(rotation.state==UIGestureRecognizerStateEnded) {
//还原
[UIViewanimateWithDuration:.5animations:^{
rotation.view.transform=CGAffineTransformIdentity;
}];
}
}
4.UISwipeGestureRecognizer [(滑动,快)]
//轻扫手势
UISwipeGestureRecognizer*swipe =[[UISwipeGestureRecognizeralloc] initWithTarget:self action:@selector(swipeView:)];
**//设置属性,swipe也是有两种属性设置手指个数及轻扫方向**
swipe.numberOfTouchesRequired=1;
***//设置轻扫方向(默认是从左往右)***
***//direction是一个枚举值有四个选项,我们可以设置从左往右,从右往左,从下往上以及从上往下***
***//设置轻扫方向(默认是从左往右)***
swipe.direction=UISwipeGestureRecognizerDirectionLeft;
[imageVaddGestureRecognizer:swipe];
�5.[UIPanGestureRecognizer](拖移,慢)
//添加拖动手势
UIPanGestureRecognizer*panGes = [[UIPanGestureRecognizeralloc] initWithTarget:self action:@selector(handlePan1:)];
[imageVaddGestureRecognizer:panGes];
- (void) handlePan1:(UIPanGestureRecognizer*)pan {
//手指所在的坐标
CGPointpoint = [pan locationInView:self.view];_view.center= point;
}
6.�[UILongPressGestureRecognizer]
// 长按
UILongPressGestureRecognizer*longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:self action:@selector(longPressAction:)];
//设置最短时间
longPress.minimumPressDuration=1;
[imgView addGestureRecognizer:longPress];
-(void)longPressAction:(UILongPressGestureRecognizer*)longPress{
if (longPress.state==UIGestureRecognizerStateBegan) {
NSLog(@"长按开始");}
else if(longPress.state==UIGestureRecognizerStateEnded){NSLog(@"长按结束");
}}
Motion摇晃
//让当前对象成为第一响应者
-(BOOL)canBecomeFirstResponder{
return YES;
}
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"摇一摇开始");
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"摇一摇结束");
}
网友评论