XCode4.2-使用复杂的触摸和手势UIXXGestureRecognizer

使用复杂的触摸和手势

Apple有各种手势识别器的Class,下面,将使用几个手势识别器,实现:轻按、轻扫、张合、旋转(摇动暂不涉及)。每个手势都将有一个标签的反馈。

包括三个UIView,分别响应 轻按、轻扫、张合,一个UIImageView响应张合。使用SingleView模板。

ViewController.h代码:

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController{

    UIView *tap;

    UIView *swipe;

    UIView *pinch;

    UIView *rotateView;

    

    UILabel *outputLabel;

    UIImageView *imageView;

}


@property(strong,nonatomicIBOutlet UIView *tap;      //点击视图

@property(strong,nonatomicIBOutlet UIView *swipe;    //轻扫视图

@property(strong,nonatomicIBOutlet UIView *pinch;    //张合视图

@property(strong,nonatomicIBOutlet UIView *rotateView;    // 旋转


@property(strong,nonatomicIBOutlet UILabel *outputLabel;  //  响应事件Label

@property(strong,nonatomicIBOutlet UIImageView *imageView;  // 图片View

@end


必须要在页面拖入相应控件并连线,在此不赘述。ViewController中包含对象部分可以省略,但建议写上。

1-点击事件

- (void)viewDidLoad

{

    [super viewDidLoad];


    UITapGestureRecognizer *tapRecognizer;  //创建一个轻按手势识别器

tapRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self 

                                                         action:@selector(foundTap:)];

                                    //初始化识别器,并使用函数指针(方法指针)触发同时实现方法

    tapRecognizer.numberOfTapsRequired=1;  //轻按对象次数(1)触发此行为

    tapRecognizer.numberOfTouchesRequired=1//要求响应的手指数

    [tap addGestureRecognizer:tapRecognizer];  //相应的对象添加控制器

}

-(void)foundTap:(UITapGestureRecognizer *)recognizer{

    outputLabel.text=@"已轻点";

}

仔细观察实现的方法,均是按着以下步骤:新建一个控制器实例--实例初始化--将其添加到类对象--实现函数指针中的方法(@selector()).

【若想获得轻按或轻扫手势坐标,可添加:

CGPoint location=[recognizer locationInView:<the view> 

<the view>即为手势识别器的视图名;location有两个参数x和y;还记得水果忍者吗?】

2-轻扫:

接下来,我们按着上述步骤实现轻扫:

还是viewDidLoad:

UISwipeGestureRecognizer *swipeRecognizer;  //创建一个轻扫识别器

    swipeRecognizer=[[UISwipeGestureRecognizer alloc]initWithTarget:self 

                                                             action:@selector(foundSwipe:)];

                           //对象初始化 并有函数指针

    swipeRecognizer.direction=

    UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft;  

                                                    //手势相应 向左或向右滑动

    swipeRecognizer.numberOfTouchesRequired=1;   //扫动对象次数

    [swipe addGestureRecognizer:swipeRecognizer];  //向对象添加控制器


函数指针指向的方法:

-(void)foundSwipe:(UITapGestureRecognizer *)recognizer{

    outputLabel.text=@"已扫动";

}

相应扫动方向时,有四个方向可供选择:

UISwipeGestureRecognizerDirectionRight/Left/Up/Down;


3-张合:

将实现放大缩小ImageView。首先定义几个参数,.h中#import下方:

#define originWidth 330.0

#define originHeight 310.0

#define originX 40.0

#define originY 350.0

ViewDidLoad中:

UIPinchGestureRecognizer *pinchReconizer;   //新建一个张合识别器对象

    pinchReconizer=[[UIPinchGestureRecognizer alloc]initWithTarget:self 

                                                            action:@selector(foundPinch)];

                                       //初始化对象

    [pinch addGestureRecognizer:pinchReconizer];  //添加控制器

函数指针方法:

-(void)foundPinch:(UIPinchGestureRecognizer *)recognizer{ //注意此处的类

    NSString *feedback;

    double scale;

    scale=recognizer.scale;   //识别器的scale(刻度尺、尺度)

    imageView.transform=CGAffineTransformMakeRotation(0.0); //视图旋转角度-不旋转

    feedback=[[NSString alloc]initWithFormat:@"缩放:Scale:%1.2f,Velocity:%1.2f",recognizer.scale,recognizer.velocity];

    outputLabel.text=feedback;

    imageView.frame=CGRectMake(originXoriginYoriginWidth*scale, originHeight*scale);

                            //图片顶点不变,图片宽*尺度=放大后的宽度

}


4-旋转

首先Cocoa类使用弧度为单位,角度和弧度的转换关系:

          角度=弧度*180/Pi

所以,viewDidLoad如下:

UIRotationGestureRecognizer *rotationRecognizer;    //新建旋转手势控制器对象

    rotationRecognizer=[[UIRotationGestureRecognizer alloc]initWithTarget:self 

                                                    action:@selector(foundRotation:)];

                                                    //初始化对象

    [rotateView addGestureRecognizer:rotationRecognizer]; //添加控制器


函数指针指向的方法:

-(void)foundRotation:(UIRotationGestureRecognizer *)recognizer{

    NSString *feedback;

    double rotation;

    rotation=recognizer.rotation;  //返回一个控制器角度

    feedback=[[NSString allocinitWithFormat:@"旋转,Radians:%1.2f,Velocity:%1.2f",

              recognizer.rotation,recognizer.velocity];

    outputLabel.text=feedback;

    imageView.transform=CGAffineTransformMakeRotation(rotation);

                 //视图旋转角度为rotation,即手势的旋转角度

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

limaning

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值