68-iOS事件补充:手势

iOS事件补充:手势

1.UIGestureRecognizer(手势识别器)
1>作用
利用UIGestureRecognizer,能轻松识别用户在某个view上面做的常见手势

2>注意: UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为
  但是没有实现功能,使用它的子类才能处理具体的手势

3> UIGestureRecognizer的常见子类(常见的手势)
UITapGestureRecognizer (敲击)
UIPinchGestureRecognizer (捏合,用于缩放)
UIPanGestureRecognizer
(拖拽)
UISwipeGestureRecognizer
(轻扫)
UIRotationGestureRecognizer
(旋转)
UILongPressGestureRecognizer
(长按)

4>手势的状态
通过判断手势的state属性,可以知道当前手势处于什么状态

state 是枚举属性 取值如下
typedef 
NS_ENUM (NSInteger, UIGestureRecognizerState) {
没有触摸事件发生,所有手势识别的默认状态: UIGestureRecognizerStatePossible,
手势状态常用取值
手势开始: UIGestureRecognizerStateBegan,
手势状态改变: UIGestureRecognizerStateChanged,
手势完成: UIGestureRecognizerStateEnded,
手势取消,恢复至Possible状态: UIGestureRecognizerStateCancelled,
手势失败,恢复至Possible状态: UIGestureRecognizerStateFailed,
识别到手势识别: UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
};

5>手势的方向
使用轻扫(UISwipeGestureRecognizer)手势时候,默认手势只能识别一个方向(左向右),
如果想识别多个方向,必须再次创建轻扫手势,并设置
轻扫手势方向 direction

左向右(默认) :UISwipeGestureRecognizerDirection Right
右向左: UISwipeGestureRecognizerDirection Left
上向下: UISwipeGestureRecognizerDirection Down
下向上: UISwipeGestureRecognizerDirection Up

6>遵守UIGestureRecognizerDelegate(手势识别器协议),可以通过实现代理方法完成一些功能
是否允许手指触发手势(默认:YES 允许):  shouldReceiveTouch
是否同时支持多个手势(默认:NO 不支持):   shouldRecognizeSimultaneouslyWithGestureRecognizer

7>手势的简单实现(代码封装)

#import  "ViewController.h"
//UIGestureRecognizerDelegate(手势识别器协议)
@interface   ViewController  ()< UIGestureRecognizerDelegate ]]>
//在UIImageView上操作
@property  ( weak nonatomic IBOutlet   UIImageView  *imageView;
@end
@implementation  ViewController
- (
void )viewDidLoad {
    [
super   viewDidLoad ];
    
//[self dianAn];
    
//[self changAn];
    
//[self qingSao];
    
//[self xuanzhuan];
    
//[self niehe];
    [
self   tuozhuai ];
}

//拖拽手势
-(
void ) tuozhuai
{
    
//1.拖拽:UIPanGestureRecognizer
    
UIPanGestureRecognizer  *pan=[[ UIPanGestureRecognizer   alloc initWithTarget : self   action : @selector (pan:)];
    [
_imageView   addGestureRecognizer :pan];
}
//监听 拖拽手势
-(
void )pan :( UIPanGestureRecognizer  *)pan
{
    
//取得view当前的点
    
CGPoint  panP=[pan  translationInView : _imageView ];
    
_imageView . transform = CGAffineTransformTranslate ( _imageView . transform , panP. x , panP. y ) ;
    
   
 //拖拽手势复位:相对于上一次的点
    [pan 
setTranslation : CGPointZero   inView : _imageView ];
    
NSLog ( @"pan" );
}

//捏合手势
-(
void )niehe
{
     //2.捏合:UIPinchGestureRecognizer
    
UIPinchGestureRecognizer  *pinch=[[ UIPinchGestureRecognizer   alloc initWithTarget : self   action : @selector (pinch:)];
    [
_imageView   addGestureRecognizer :pinch];
    pinch.
delegate = self ;
}
//监听 捏合手势
-(
void )pinch:( UIPinchGestureRecognizer  *)pinch
{
    
//pinch.scale 缩放比例
    
_imageView . transform = CGAffineTransformScale ( _imageView . transform , pinch. scale , pinch. scale );
    
    
//捏合手势复位:相对于上一次的缩放比例
    pinch.
scale = 1 ;
    
NSLog ( @"pinch" );
}

//旋转手势
-(
void ) xuanzhuan
{
    
//3.旋转: UIRotationGestureRecognizer
    
UIRotationGestureRecognizer  *rotation=[[ UIRotationGestureRecognizer   alloc initWithTarget : self   action : @selector (rotation:)];
    [
_imageView   addGestureRecognizer :rotation];
    rotation.
delegate = self ;
}
//监听 旋转手势
-(
void )rotation:( UIRotationGestureRecognizer  *)rotation
{
    
//rotation  旋转的角度
    
_imageView . transform = CGAffineTransformRotate ( _imageView . transform , rotation. rotation );
    
   
 //旋转手势复位: 相对于上一次旋转的角度(必须有这一步操作)
    rotation.
rotation = 0 ;
    
    
NSLog ( @"rotation" );
}

//轻扫手势
-(
void ) qingSao
{
    
//4.轻扫: UISwipeGestureRecognizer
    
//默认手势只能识别一个方向(左向右)
    
//如果想识别多个方向,必须再次创建轻扫手势
    
/*
     
设置轻扫手势方向 direction
     
     
左向右(默认) : UISwipeGestureRecognizerDirection Right
     
右向左: UISwipeGestureRecognizerDirection Left
     
上向下: UISwipeGestureRecognizerDirection Down
     
下向上: UISwipeGestureRecognizerDirection Up
     */

    
    
UISwipeGestureRecognizer  *swipe=[[ UISwipeGestureRecognizer   alloc initWithTarget : self   action : @selector (swipe:)];
   
 //左向右:默认
    
//swipe.direction=UISwipeGestureRecognizerDirectionRight;
    [
_imageView   addGestureRecognizer :swipe];
    
    
UISwipeGestureRecognizer  *swipeRight=[[ UISwipeGestureRecognizer   alloc initWithTarget : self   action : @selector (swipe:)];
    
//右向左
    swipeRight.
direction = UISwipeGestureRecognizerDirectionLeft ;
    [
_imageView   addGestureRecognizer :swipeRight];
    
    
UISwipeGestureRecognizer  *swipeDown=[[ UISwipeGestureRecognizer   alloc initWithTarget : self   action : @selector (swipe:)];
    
//上向下
    swipeDown.
direction = UISwipeGestureRecognizerDirectionDown ;
    [
_imageView   addGestureRecognizer :swipeDown];
    
    
UISwipeGestureRecognizer  *swipeUp=[[ UISwipeGestureRecognizer   alloc initWithTarget : self   action : @selector (swipe:)];
    
//下向上
    swipeUp.
direction = UISwipeGestureRecognizerDirectionUp ;
    [
_imageView   addGestureRecognizer :swipeUp];
    
}
//监听 轻扫手势
-(
void )swipe:( UISwipeGestureRecognizer  *)swipe
{
    
NSLog ( @"swipe" );
}

//长按手势
-(
void ) changAn
{
   
 //5.长按: UILongPressGestureRecognizer
    
UILongPressGestureRecognizer  *longpress=[[ UILongPressGestureRecognizer   alloc initWithTarget : self   action : @selector (longPress:)];
    [
_imageView   addGestureRecognizer :longpress];
}
//监听 长按手势
-(
void )longPress:( UILongPressGestureRecognizer  *) longpress
{
    
    
/*
     
长按手势默认会触发2次,长按一次,手指抬起一次
     
     通过判断手势的
state 属性,可以知道,当前手势处于什么状态
     state 是枚举属性 取值如下
     
     typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
     // 没有触摸事件发生,所有手势识别的默认状态
     UIGestureRecognizerStatePossible,
     
     //手势开始
     UIGestureRecognizerStateBegan,
     // 手势状态改变
     UIGestureRecognizerStateChanged,
     // 手势完成
     UIGestureRecognizerStateEnded,


     // 手势取消,恢复至Possible状态
     UIGestureRecognizerStateCancelled,
     // 手势失败,恢复至Possible状态
     UIGestureRecognizerStateFailed,
     // 识别到手势识别
     UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
     };
     */

    
if  (longpress. state == UIGestureRecognizerStateBegan ) {
        
NSLog ( @"开始长按longPress" );
    }
    
else   if  (longpress. state == UIGestureRecognizerStateEnded )
    {
        
NSLog ( @"手指抬起longPress" );
    }
}


//点按手势
-(
void ) dianAn
{
    
//6.点按 UITapGestureRecognizer
    
UITapGestureRecognizer  *tap=[[ UITapGestureRecognizer   alloc initWithTarget : self   action : @selector (tap:)];
    
    
//设置手势触发的 点按次数
    tap.
numberOfTapsRequired = 2 ;
    
    
//需要 几根手指一起点按才能触发
    tap.
numberOfTouchesRequired = 2 ;
    
    
//设置点按的代理对象为自己
    tap.
delegate = self ;
    
    
//给ImageView添加点按手势
    [
_imageView   addGestureRecognizer :tap];
}
//监听 点按手势
-(
void ) tap:( UITapGestureRecognizer  *)tap
{
    
NSLog ( @"tap" );
}


//遵守手势识别器协议可以实现的方法
#pragma mark - UIGestureRecognizer(手势识别器) 代理方法

//是否允许手指触发手势
//默认:YES 允许
-(
BOOL )gestureRecognizer:( UIGestureRecognizer  *)gestureRecognizer shouldReceiveTouch:( UITouch  *)touch
{
    
return   YES ;
}

//是否同时支持多个手势
//默认:NO 不支持
-(
BOOL )gestureRecognizer:( UIGestureRecognizer  *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:( UIGestureRecognizer  *)otherGestureRecognizer
{
    
return   YES ;
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值