zoukankan      html  css  js  c++  java
  • iOS中的手势识别的积累:UIGestureRecognizer(轻拍手势,长按手势,清扫手势,平移手势,捏合手势,旋转手势,屏幕边缘平移手势) ----仿射变换

    #import "RootViewController.h"
    #import "UIColor+MyUIColor.h"
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    /*
     UITapGestureRecognizer是轻拍⼿手势识别器,能识别轻拍操作
     UILongPressGestureRecognizer是⻓长按⼿手势识别器,能识别⻓长按操作。
     UIRotationGestureRecognizer是旋转⼿手势识别器,能识别旋转操作。
     UIPinchGestureRecognizer是捏合⼿手势识别器,能识别捏合操作。
     UIPanGestureRecognizer是平移⼿手势识别器,能识别拖拽操作。
     UISwipeGestureRecognizer是轻扫⼿手势识别器,能识别拖拽操作。
     UIScreenEdgePanGestureRecognizer是屏幕边缘轻扫识别器,是iOS7中新增的⼿手势。
     
     */
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, [UIScreen mainScreen].bounds.size.width, 400)];
        [self.view addSubview:redView];
        redView.backgroundColor = [UIColor redColor];
      
        [redView release];
        // 手势识别器的基类:UIGestureRecognizer
       //轻拍手势
        /*
        //轻拍手势
        //1.创建轻拍手势对象
        UITapGestureRecognizer *tapGesTure = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
        //2.将视图添加到视图上
        //设置轻拍的次数
        tapGesTure.numberOfTapsRequired = 2;
        //设置触摸的点数
        tapGesTure.numberOfTouchesRequired = 2;
         [redView addGestureRecognizer:tapGesTure];
         //3.释放
         [tapGesTure release];
       */
       //长按手势
        /*
        //1.创建长按手势对象
        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
        //设置长按手势的识别时间
        longGesture.minimumPressDuration = 0.3;
        //2.添加到视图
        [redView addGestureRecognizer:longGesture];
        
        //3.释放
        [longGesture release];
        */
       //清扫手势
        //1.创建清扫手势
        /*
        UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
        //2.添加到视图
        [redView addGestureRecognizer:swipeGestureLeft];
        //释放
        [swipeGestureLeft release];
        
        UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
        //设置清扫方向
        swipeGestureRight.direction = UISwipeGestureRecognizerDirectionLeft;
        
        //2.添加到视图
        [redView addGestureRecognizer:swipeGestureRight];
        //释放
        [swipeGestureRight release];
         */
        //平移手势
       
        //1.创建平移手势对象
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
        //2.添加手势对象
        [redView addGestureRecognizer:panGesture];
        //3.释放
        [panGesture release];
       
        //捏合手势
        /*
        //1.创建捏合手势
        UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
        //2.添加到视图
        [redView addGestureRecognizer:pinchGesture];
        //3.释放
        [pinchGesture release];
        
        */
        //旋转手势
        /*
        //1.创建旋转手势对象
        UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationGesture:)];
        //2.添加到视图
        [redView addGestureRecognizer:rotationGesture];
        //3.释放
        [rotationGesture release];
         */
        //屏幕边缘平移手势
        //1.创建屏幕边缘平移手势对象
    //    UIScreenEdgePanGestureRecognizer *screenEdgePan =  [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handelScreenEdgeGesture:)];
    //    //2.添加视图
    //    //设置平移的屏幕边界
    //    screenEdgePan.edges = UIRectEdgeLeft;
    //    [redView addGestureRecognizer:screenEdgePan];
    //    //3.释放视图
    //    [screenEdgePan release];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark ---- handle gesture action
    //屏幕边缘手势
    - (void)handelScreenEdgeGesture:(UIScreenEdgePanGestureRecognizer *)screenEdge
    {
        //1.获取平移增量
        CGPoint point = [screenEdge translationInView:screenEdge.view];
       
        //2.平移视图
        screenEdge.view.transform = CGAffineTransformTranslate(screenEdge.view.transform, point.x, 0);
        //3.将之前增量清零
        [screenEdge setTranslation:CGPointZero inView:screenEdge.view];
        
    }
    //旋转手势
    - (void)handleRotationGesture:(UIRotationGestureRecognizer *)rotationGesture
    {
        //放射变换
        rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation);
        rotationGesture.rotation = 0;
    }
    
    //捏合手势
    - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch
    {
        //仿射变换
        //从原来位置开始缩放
        pinch.view.transform =CGAffineTransformMakeScale(pinch.scale, pinch.scale);
        
    //    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    //    //将之前的缩放比例置1
    //    pinch.scale = 1;
    }
    
    //平移手势
    - (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture
    {
    //    //1.获取平移增量
    //    CGPoint point = [panGesture translationInView:panGesture.view];
    //    //2.改变视图的位置
    //    panGesture.view.center = CGPointMake(panGesture.view.center.x + point.x, point.y + panGesture.view.center.y);
    //    
    //    //3.将之前的增量清零
    //    [panGesture setTranslation:CGPointZero inView:panGesture.view];
        //仿射变换
        //1.获取平移增量
        CGPoint point = [panGesture translationInView:panGesture.view];
        //2.修改视图的transform
       // panGesture.view.transform = CGAffineTransformMakeScale(point.x, point.y);
        //panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);
        panGesture.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
        //3.将之前的增量清零
        //[panGesture setTranslation:CGPointZero inView:panGesture.view];
    }
    
    //清扫手势
    - (void)handleSwipeGesture:(UISwipeGestureRecognizer *)swipGesture
    {
       
        if (swipGesture.direction == UISwipeGestureRecognizerDirectionRight) {
             swipGesture.view.backgroundColor = [UIColor randomColor];
        }
        if (swipGesture.direction == UISwipeGestureRecognizerDirectionLeft) {
            self.view.backgroundColor = [UIColor randomColor];
        }
    }
    
    
    
    //处理轻拍手势
    - (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture
    {
        tapGesture.view.backgroundColor = [UIColor randomColor];
        
    }
    //处理长按手势
    - (void)handleLongPress:(UILongPressGestureRecognizer *)longGesture{
        //当手势识别器为长按时,触发长按改变颜色的方法
        if (longGesture.state == UIGestureRecognizerStateBegan) {
             longGesture.view.backgroundColor = [UIColor randomColor];
        }
       
    }
    - (void)
    @end

    随机色

    + (UIColor *)randomColor
    {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0  blue:arc4random() % 256 / 255.0  alpha:1];
    }
  • 相关阅读:
    python操作redis
    python正则表达式-案例
    hive序列化和反序列化serde
    python配置文件
    Java写入的常用技巧
    Java从string数组创建临时文件
    Java官方操纵byte数组的方式
    python实例方法、静态方法和类方法
    ast.literal_eval(转)
    impala学习笔记
  • 原文地址:https://www.cnblogs.com/wohaoxue/p/4764553.html
Copyright © 2011-2022 走看看