手势识别(一)

效果图 Gif.gif

AppDelegate.m

//#import "COSTouchVisualizerWindow.h"
#import "AppDelegate.h"
#import "TapViewController.h"
#import "PanViewController.h"
#import "EdgePanViewController.h"
#import "PinchViewController.h"
#import "SwipeViewController.h"
#import "LongPressViewController.h"

/*
 
 手势识别:特定的手势 , 将低级别的动作处理封装为了高级别的动作 , 简化处理;
 UIGestureRecognizer 的子类:
 tap , pan , swipe , longPress , Pinch(捏合)
 */

/*
 
 手势分为单独手势和连续手势
 */


@interface AppDelegate ()<UITableViewDataSource, UITableViewDelegate>
{
    UITableViewController *_tableVC;
}
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    _tableVC = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    _tableVC.tableView.delegate = self;
    _tableVC.tableView.dataSource = self;
    _tableVC.edgesForExtendedLayout = UIRectEdgeNone;
    _tableVC.navigationItem.title = @"Gesture1 Demo";
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:_tableVC];
    
//    UITapGestureRecognizer
    //numberOfTapsRequired:这一次点击手指需要点几次?
    //numberOfTouchesRequired:这一次点击需要几个手指来点击?
    
    self.window.rootViewController = nav;
    
    [self.window makeKeyAndVisible];

    return YES;
}


#pragma mark - table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    NSString *title;
    switch (indexPath.row) {
        case 0:
            title = @"Gesture - Tap";
            break;
        case 1:
            title = @"Gesture - Pan";
            break;
        case 2:
            title = @"Gesture - EdgePan";
            break;
        case 3:
            title = @"Gesture - Pinch";
            break;
        case 4:
            title = @"Gesture - Swipe";
            break;
        case 5:
            title = @"Gesture - LongPress";
            break;
        default:
            break;
    }
    cell.textLabel.text = title;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIViewController *targetVC;
    switch (indexPath.row) {
        case 0:
            targetVC = (UIViewController *)[[TapViewController alloc] init];
            break;
        case 1:
            targetVC = (UIViewController *)[[PanViewController alloc] init];
            break;
        case 2:
            targetVC = (UIViewController *)[[EdgePanViewController alloc] init];
            break;
        case 3:
            targetVC = (UIViewController *)[[PinchViewController alloc] init];
            break;
        case 4:
            targetVC = (UIViewController *)[[SwipeViewController alloc] init];
            break;
        case 5:
            targetVC = (UIViewController *)[[LongPressViewController alloc] init];
            break;
        default:
            break;
    }
    
    if (targetVC)
    {
        [_tableVC.navigationController pushViewController:targetVC animated:YES];
    }
    
}

#pragma mark - Other Method
- (void)applicationWillResignActive:(UIApplication *)application {
    
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
}

- (void)applicationWillTerminate:(UIApplication *)application {
    
}

@end

TestView.m

#import "TestView.h"

@implementation TestView


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"TestView touch began");
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"TestView touch ended");
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"TestView touch cancelled");
}

@end

TapViewController.m

#import "TapViewController.h"


@interface TapViewController ()

{
    UIView *_testView;
}

@end

@implementation TapViewController


#pragma mark - init
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.navigationItem.title = @"Tap";
        self.edgesForExtendedLayout = UIRectEdgeNone;
        _testView = [[UIView alloc] init];
        _testView.backgroundColor = [UIColor orangeColor];
        //创建手势:
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [_testView addGestureRecognizer:tapGesture];
        
        [tapGesture addTarget:self action:@selector(tap2:)];
        
        
    }
    return self;
    
}


#pragma mark - lifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _testView.frame = CGRectMake(50, 50, 150, 150);
    [self.view addSubview:_testView];
    
}


- (void)tap:(UITapGestureRecognizer *)tapGesture {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"警告" message:@"这是一个警告信息" preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"已取消操作");
    }]];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:^{
        NSLog(@"跳转到 alertVC");
    }];
}

- (void)tap2:(UITapGestureRecognizer *)tapGesture {
    NSLog(@"执行了tap2方法");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end

PanViewController.m

#import "PanViewController.h"

@interface PanViewController ()
{
    UIView *_testView;
}
@end

@implementation PanViewController


#pragma mark - init
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.navigationItem.title = @"Pan";
        self.edgesForExtendedLayout = UIRectEdgeNone;
        
        _testView = [[UIView alloc] init];
        _testView.backgroundColor = [UIColor redColor];
        
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [_testView addGestureRecognizer:panGesture];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _testView.frame = CGRectMake(50, 50, 150, 150);
    [self.view addSubview:_testView];
}


- (void)pan:(UIPanGestureRecognizer *)panGesture {
    //获取当前手势在其父视图上的位移:
    CGPoint translatePoint = [panGesture translationInView:self.view];
    //产生了多少位移就进行多少偏移:
    _testView.center = CGPointMake(_testView.center.x + translatePoint.x, _testView.center.y + translatePoint.y);
    //每次都将偏移量在它的父视图上重置为0 , 否则它会偏移量每次会递增发生较大移动:
//    [panGesture setTranslation: CGPointZero inView:self.view];
    if (panGesture.state == UIGestureRecognizerStatePossible) {
        NSLog(@"可能");
    } else if (panGesture.state == UIGestureRecognizerStateBegan) {
        NSLog(@"开始");
    } else if (panGesture.state == UIGestureRecognizerStateChanged) {
        NSLog(@"改变");
    } else if (panGesture.state == UIGestureRecognizerStateEnded) {
        NSLog(@"结束");
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end

EdgePanViewController.m

#import "EdgePanViewController.h"

@interface EdgePanViewController ()
{
    UIView *_testView;
}
@end


@implementation EdgePanViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.navigationItem.title = @"Edge Pan";
        self.edgesForExtendedLayout = UIRectEdgeNone;
        _testView = [[UIView alloc] init];
        _testView.backgroundColor = [UIColor orangeColor];
    }
    
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _testView.frame = CGRectMake(50, 50, 150, 150);
    [self.view addSubview:_testView];
    
    //创建屏幕边缘滑动手势:
    UIScreenEdgePanGestureRecognizer *edgePanGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanGesture:)];
    //右侧边缘作用手势:
    edgePanGesture.edges = UIRectEdgeRight;
    [_testView addGestureRecognizer:edgePanGesture];
}



- (void)edgePanGesture:(UIScreenEdgePanGestureRecognizer *)edgePanGesture {
    //获取当前手势在当前视图上的位移:
    CGPoint translatePoint = [edgePanGesture translationInView:self.view];
    _testView.center = CGPointMake(_testView.center.x - translatePoint.x, _testView.center.y);
    [edgePanGesture setTranslation:CGPointZero inView:self.view];
    
//    [[UIApplication sharedApplication].keyWindow.rootViewController dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end

PinchViewController.m

#import "PinchViewController.h"

@interface PinchViewController ()
{
    UIView *_testView;
}
@end

@implementation PinchViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.navigationItem.title = @"Pinch";
        self.edgesForExtendedLayout = UIRectEdgeNone;
        _testView = [[UIView alloc] init];
        _testView.backgroundColor = [UIColor orangeColor];
        _testView.frame = CGRectMake(150, 200, 150, 150);
        [self.view addSubview:_testView];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
    [_testView addGestureRecognizer:pinchGesture];
}


- (void)pinchGesture:(UIPinchGestureRecognizer *)pinchGesture {
    if (pinchGesture.state == UIGestureRecognizerStateChanged) {
        _testView.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);
    } else if (pinchGesture.state == UIGestureRecognizerStateEnded) {
        _testView.transform = CGAffineTransformIdentity;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end

SwipeViewController

#import "SwipeViewController.h"

@interface SwipeViewController ()
{
    UIView *_testView;
}
@end

@implementation SwipeViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.navigationItem.title = @"Swipe 清扫手势";
        self.edgesForExtendedLayout = UIRectEdgeNone;
        _testView = [[UIView alloc] init];
        _testView.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:_testView];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _testView.frame = CGRectMake(50, 50, 150, 150);
    //创建清扫手势:
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    //清扫手势定义为上下方向:
    swipeGesture.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;
    [_testView addGestureRecognizer:swipeGesture];
}


- (void)swipeGesture:(UISwipeGestureRecognizer *)swipeGesture {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:@"Swipe Gesture" preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消操作");
    }]];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:^{
        NSLog(@"弹窗出现了~");
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

LongPressViewController.m

#import "LongPressViewController.h"

@interface LongPressViewController ()
{
    UIView *_testView;
}
@end

@implementation LongPressViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.navigationItem.title = @"Long Press";
        self.edgesForExtendedLayout = UIRectEdgeNone;
        _testView = [[UIView alloc] init];
        _testView.backgroundColor = [UIColor orangeColor];
        _testView.frame = CGRectMake(50, 50, 150, 150);
        
        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
        longPressGesture.minimumPressDuration = 2.0f;
        [_testView addGestureRecognizer:longPressGesture];
        [self.view addSubview:_testView];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
}


- (void)longPressGesture:(UILongPressGestureRecognizer *)longPressGesture {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"longPressGesture" preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消操作");
    }]];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:^{
        NSLog(@"弹出警告");
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end

愿编程让这个世界更美好

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容