zoukankan      html  css  js  c++  java
  • 手势识别器

    #import "RootViewController.h"
    #import "RootView.h"
    #import "SecondViewController.h"
    
    @interface RootViewController ()<UIGestureRecognizerDelegate>// 设置代理
    @property (nonatomic, strong) RootView *rootView;
    @end
    
    @implementation RootViewController
    
    - (void)loadView
    {
        self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.view = self.rootView;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        //添加轻拍点击事件
        [self tapGesture];
        //添加平移手势
    //    [self panGesture];
        // 添加捏合手势
        [self pinchGesture];
        // 添加旋转手势
        [self rotationGesture];
        // 添加清扫手势
        [self swipeGesture];
        // 添加长按按钮
        [self longPressGesture];
        // 添加边缘清扫
        [self screenEdgePanGesture];
        
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark 轻拍 UITapGestureRecognizer
    
    // 添加轻拍手势
    
    - (void)tapGesture
    {
        //1. 创建手势识别器对象
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
        
        //2. 讲手势添加到相应的视图上
        [self.rootView.myImageView addGestureRecognizer:tap];
    }
    
    // 实现轻拍手势事件
    - (void)tapGestureAction:(UITapGestureRecognizer *)tap
    {
        NSLog(@"轻拍");
    }
    
    #pragma mark 平移 UIPanGestureRecognizer
    - (void)panGesture
    {
        // 1.创建手势对象
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
        // 2. 将手势添加到相应的视图上
        [self.rootView.myImageView addGestureRecognizer:pan];
        
    }
    
    - (void)panGestureAction:(UIPanGestureRecognizer *)pan
    {
        // 在view上面挪动的距离
        CGPoint p = [pan translationInView:pan.view];
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
        // 清空移动距离
        [pan setTranslation:CGPointZero inView:pan.view];
        
        NSLog(@"平移");
    }
    
    #pragma mark 缩放手势 (捏合) UIPinchGestureRecognizer
    
    - (void)pinchGesture
    {
        // 1.创建手势对象
    
        UIPinchGestureRecognizer *pich = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureAction:)];
        // 设置代理
        pich.delegate = self;
        
        
        // 2. 将手势添加到相应的视图上
        [self.rootView.myImageView addGestureRecognizer:pich];
        
    }
    
    - (void)pinchGestureAction:(UIPinchGestureRecognizer *)pich
    {
        
        pich.view.transform = CGAffineTransformScale(pich.view.transform, pich.scale, pich.scale);
        pich.scale = 1;//(比例大小一样)
        
        NSLog(@"捏合");
    }
    
    #pragma mark 旋转手势 UIRotationGestureRecognizer
    
    - (void)rotationGesture
    {
        
        UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureAction:)];
        // 设置代理 (目的)旋转和捏合一起用
        rotation.delegate = self;
        
        [self.rootView.myImageView addGestureRecognizer:rotation];
        
    }
    
    - (void)rotationGestureAction:(UIRotationGestureRecognizer *)rotation
    {
        rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
        rotation.rotation = 0;//(旋转的角度清零)
        
        NSLog(@"旋转");
        
    }
    
    // 实现协议方法, 同时识别多个手势
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }
    
    #pragma mark 长按手势 UILongPressGestureRecognizer
    
    - (void)longPressGesture
    {
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureAction:)];
        longPress.minimumPressDuration = 1.0;
    //    longPressGr.minimumPressDuration = 1.0;
    //    [self.tableView addGestureRecognizer:longPressGr];
    //    [longPressGr release];
        [self.rootView.myImageView addGestureRecognizer:longPress];
        
    }
    
    - (void)longPressGestureAction:(UILongPressGestureRecognizer *)longPress
    {
        if (longPress.state == UIGestureRecognizerStateBegan) {
    //        longPress.rootView.myImageView.backgroundColor = [UIColor greenColor];
    //        CGPoint point = [longPress locationInView:self.rootView.myImageView];
    //                NSIndexPath * indexPath = [self.rootView.myImageView indexPathForRowAtPoint:point];
    //
            NSLog(@"长按按钮");
            
    //                if(indexPath == nil) return ;
        }
     //    longPressGesture.view.backgroundColor = [UIColor random];
    //    if(gesture.state == UIGestureRecognizerStateBegan)
    //    {
    //        CGPoint point = [gesture locationInView:self.tableView];
    //        
    //        NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point];
    //        
    //        if(indexPath == nil) return ;
    }
    
    #pragma mark 清扫手势 UISwipeGestureRecognizer
    
    - (void)swipeGesture
    {
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
        swipe.direction = UISwipeGestureRecognizerDirectionLeft;
        [self.rootView.myImageView addGestureRecognizer:swipe];
    }
    
    
    - (void)swipeGestureAction:(UISwipeGestureRecognizer *)swipe
    {
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        [self presentViewController:secondVC animated:YES completion:nil];
        NSLog(@"向左扫");
    }
    
    
    #pragma mark 屏幕边缘轻扫识别器 UIScreenEdgePanGestureRecognizer
    
    - (void)screenEdgePanGesture
    {
        UIScreenEdgePanGestureRecognizer *screen = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureAction:)];
        screen.edges = UIRectEdgeRight;
        [self.rootView addGestureRecognizer:screen];
        
    }
    
    - (void)screenEdgePanGestureAction:(UIScreenEdgePanGestureRecognizer *)screen
    {
        NSLog(@"");
    }
  • 相关阅读:
    2月份热门的 24 个 jQuery 插件
    走近求伯君1 求伯君,1964年11月26日出生于浙江新昌县。
    转CSDN,13原则
    SQL Server数据库如何正确加密?
    Zend Studio提供zend studio教程、zend studio下载等相关资源的公益性站点。 订阅
    高端人才必看,生意人必读
    有速度才有效率,支持Google gear离线的网站和应用
    2009.08.20总结与微软中国开发部经理段老师的通话无锡德立解决方案
    从SOA、SaaS到博科自主配置平台
    MySQL 1045错误的解决方法
  • 原文地址:https://www.cnblogs.com/leikun1113/p/5539460.html
Copyright © 2011-2022 走看看