美文网首页
手势识别(一)

手势识别(一)

作者: 小苗晓雪 | 来源:发表于2017-06-17 12:01 被阅读16次
效果图 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

愿编程让这个世界更美好

相关文章

  • 手势控制:点击、滑动、平移、捏合、旋转、长按、轻扫

    手势识别器(Gesture Recognizer)用于识别触摸序列并触发响应事件。当手势识别器识别到一个手势或手势...

  • 3.6 iOS手势识别的状态和手势识别器幕后原理

    2.2手势识别的状态和手势识别器幕后原理 (一)手势的状态 (二)离散型手势识别器和连续型手势识别器之间的对比: ...

  • 手势——UIGestureRecognizer

    一、简介 UIGestureRecognizer是具体手势识别器的基类。 手势识别器对象(或简单地说是手势识别器)...

  • Gesture手势

    手势识别器 手势识别器是对触摸事件做了封装,我们无需自己去判断某个手势是否触发,手势识别器本身起到了识别作用,我们...

  • 手势识别

    手势识别 6种手势识别 在iOS开发中有6中手势识别:点按、捏合、拖动、轻扫、旋转、长按苹果推出手势识别主要是为了...

  • UIGestureRecognizer

    什么是手势识别器? 手势识别器就是对触摸事件做了封装,我们不需要判断某个手势是否触发,手势识别器本身起到了识别作用...

  • UIGestureRecognizer手势识别器学习笔记

    UIGestureRecognizer 具体手势识别器的基类。一个手势识别器对象,或者简单地说一个手势识别器,解耦...

  • ios多手势传递,用于页面头部悬浮滚动

    /* 是否允许多个手势识别器共同识别,一个控件的手势识别后是否阻断手势识别继续向下传播,默认返回NO;如果为YES...

  • iOS手势识别器

    1.手势识别器 1.手势识别器是iOS中比较抽象的一个类,用于识别一个手势,所谓手势:有规律的触摸。是对触摸事件做...

  • EasyAR手势姿势识别

    简介 简单试了下EasyAR的手势识别以及姿势识别 手势识别 目前官方是有两种手势 测试了感觉识别度蛮高的 也蛮快...

网友评论

      本文标题:手势识别(一)

      本文链接:https://www.haomeiwen.com/subject/eewvqxtx.html