使用复杂的触摸和手势

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,nonatomic) IBOutlet UIView *tap;      //点击视图
@property(strong,nonatomic) IBOutlet UIView *swipe;    //轻扫视图
@property(strong,nonatomic) IBOutlet UIView *pinch;    //张合视图
@property(strong,nonatomic) IBOutlet UIView *rotateView;    // 旋转

@property(strong,nonatomic) IBOutlet UILabel *outputLabel;  //  响应事件Label
@property(strong,nonatomic) IBOutlet 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(originX, originY, originWidth*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 alloc] initWithFormat:@"旋转,Radians:%1.2f,Velocity:%1.2f",
              recognizer.rotation,recognizer.velocity];
    outputLabel.text=feedback;
    imageView.transform=CGAffineTransformMakeRotation(rotation);
                 //视图旋转角度为rotation,即手势的旋转角度
}