如今的电容屏时代,也就是智能时代,基本上人人都是智能手机了,然而智能手机的屏幕需要手来点,因此我们称这个点的动作叫做手势,在手机专门有个类来识别这些手势,今天就来介绍手势识别器的用法。
//
// ViewController.m
// demo-UIGesture
//
// Created by mac on 16/8/1.
// Copyright © 2016年 mac. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:0.9 green:0.8 blue:0.8 alpha:1];
//注意:要开启用户交互和多点控制
self.view.userInteractionEnabled = YES;
self.view.multipleTouchEnabled = YES;
/*
手势识别器:
1.点击
2.轻扫
3.捏合
4.旋转
5.移动
6.长按
*/
/*
PS:因为有些方法会相互冲突,需要调用这个函数
[A requireGestureRecognizerToFail:B]函数,它可以指定当A手势发生时,即便A已经滿足条件了,也不会立刻触发,会等到指定的手势B确定失败之后才触发。
*/
//点击手势 1.单击 2.双击
UITapGestureRecognizer *tapClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleclickAction:)];
[self.view addGestureRecognizer:tapClick];
UITapGestureRecognizer *tapdoubleClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleclickAction:)];
//双击屏幕调用的方法
tapdoubleClick.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapdoubleClick];
//双击失败了就是单击,进行一个判别,不然每次双击的时候单击都会调用一次
[tapClick requireGestureRecognizerToFail:tapdoubleClick];
//2.轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
//向上滑,默认向右
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipe];
//3.捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[self.view addGestureRecognizer:pinch];
//4.旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[self.view addGestureRecognizer:rotation];
//5.移动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:pan];
/*
注意:这里移动会出现冲突
需要调用requeireGestureRecognizer方法
*/
[pan requireGestureRecognizerToFail:swipe];
//6.长按
UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[self.view addGestureRecognizer:longPress];
}
//单机屏幕事件
- (void)singleclickAction:(UITapGestureRecognizer *)sender{
NSLog(@"点击了一下屏幕");
}
//双击屏幕事件
- (void)doubleclickAction:(UITapGestureRecognizer *)sender {
NSLog(@"双击了屏幕");
}
//轻扫事件
- (void)swipeAction:(UISwipeGestureRecognizer *)sender {
NSLog(@"轻扫屏幕");
}
//捏合
- (void)pinchAction:(UIPinchGestureRecognizer *)sender {
if(sender.state == UIGestureRecognizerStateChanged){
NSLog(@"一直捏合");
CGFloat scale = sender.scale;
self.view.transform = CGAffineTransformMakeScale(scale, scale);
}else if(sender.state == UIGestureRecognizerStateEnded){
//恢复到原来的大小
NSLog(@"旋转结束");
self.view.transform = CGAffineTransformIdentity;
}
}
//旋转
- (void)rotationAction:(UIRotationGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateChanged) {
CGFloat angle = sender.rotation;
self.view.transform = CGAffineTransformMakeRotation(angle);
NSLog(@"旋转了屏幕");
}else if (sender.state == UIGestureRecognizerStateEnded){
NSLog(@"停止了旋转");
self.view.transform = CGAffineTransformIdentity;
}
}
//平移
- (void)panAction:(UIPanGestureRecognizer *)sender {
//平移的坐标
CGPoint moveDistance = [sender translationInView:self.view ];
//这是一个实时调用的方法
NSLog(@"平移了:%@",NSStringFromCGPoint(moveDistance));
}
//长按
- (void)longPressAction:(UILongPressGestureRecognizer *)sender{
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始长按屏幕");
}else if(sender.state == UIGestureRecognizerStateEnded){
NSLog(@"结束长按屏幕");
}
}
@end