zoukankan      html  css  js  c++  java
  • 触摸和手势

    ResturesAndEventHandle

    【手势与事件处理】

    所谓手势就是手指在屏幕上的动作

    【触摸事件】

     重写方法

    1、开始

    • (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event;

       //每当手指触摸屏幕的时候就会产生一个事件

        获取集合中的一个触摸

        UITouch *touch = [touches anyObject];

        触摸的视图   touch.view

        时间戳 (何时触摸)touch.timestamp

        点击次数touch.tapCount

        触摸的位置 当前的位置

        CGPoint currentPoint = [touch locationInView:self.view];

        触摸之前的位置

        CGPoint pre = [touch previousLocationInView:self.view];

        NSLog(@"%@ %@",NSStringFromCGPoint(currentPoint) ,NSStringFromCGPoint(pre) );

    • 2、移动
    • (void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event;

    //事件传递是从上往下,UILabel和UIImageView 默认是不接收事件的,如果要开启,设置userInteractionEnabled = YES

        //触摸是从下往上  ,会先检测父视图能不能接受触摸,如果能 父视图的里边的空间都可以接受触摸,如果不能,父视图的控件,不管怎么设置都不能接收事件

    判断手势是否在同一视图上

        if (touch.view == self.myLabel) {

            //获得偏移

            CGPoint sub = CGPointMake(currentPoint.x - pre.x, currentPoint.y - pre.y);

            //当前位置

            CGPoint center = touch.view.center;

            //把偏移加上去

            center = CGPointMake(center.x + sub.x, center.y + sub.y);

            touch.view.center = center;

        }

    • 3、结束      (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event;
    • 4、取消     (void)touchesCancelled:(NSSet )touches withEvent:(UIEvent )event;

    UITouch的常见属性:

    tapCount

    touch.view 点击的视图

    previousLocationInView

    locationInView

    应用

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

        UITouch *touch = [touches anyObject];

        CGPoint prePoint = [touch previousLocationInView:self.view];

        NSLog(@"上一次点击时的坐标:%@",NSStringFromCGPoint(prePoint));

        CGPoint currentPoint = [touch locationInView:self.view];

        NSLog(@"当前点击的坐标:%@",NSStringFromCGPoint(currentPoint));

        CGPoint pointDelt = CGPointMake(currentPoint.x-prePoint.x, currentPoint.y-prePoint.y);

        CGPoint center = CGPointMake(_myView.center.x+pointDelt.x, _myView.center.y+pointDelt.y);

        [_myView setCenter:center];

    }

    【各种手势】

    1.手势

    基类UIGestureRecognizer

    UITapGestureRecognizer Tap 点击

    UIPanGestureRecognizer Pan (慢速滑动,拖移)

    UILongPressGestureRecognizer LongPress (长按)

    UIPinGestureRecognizer Pinch (捏合,两手指往内或外拨动)

    UIRotationGestureRecognizer Rotation (旋转)

    UISwipeGestureRecognizer Swipe (快速滑动,轻扫)

    2. UITapGestureRecognizer

    点击:

    //创建了一个点击的手势

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];

        //设置手势

        //点击多少下,事件被调用

        tap.numberOfTapsRequired = 2;

        //需要多少个手指头

        tap.numberOfTouchesRequired = 2;

    3. UIPanGestureRecognizer

    拖移

    - (void)panGestureHandle:(UIPanGestureRecognizer *)pgr

    {

        switch (pgr.state) {

            case UIGestureRecognizerStateBegan:

            case UIGestureRecognizerStateChanged:

                //NSLog(@"滑动手势识别成功");

            {

    //获取手势作用的view

                UIView *view = pgr.view;

    //获取偏移

                CGPoint offset = [pgr translationInView:self.view];

    //设置偏移

                view.transform = CGAffineTransformTranslate(view.transform, offset.x, offset.y);

                //因为滑动手势效果会累加,所以每次需要清零

                [pgr setTranslation:CGPointZero inView:self.view];

            }

                break;

            case UIGestureRecognizerStateEnded:

                NSLog(@"滑动手势识别结束");

                break;

            default:

                break;

        }

    }

    4. UILongPressGestureRecognizer

    长按

    minimumPressDuration //设置最小持续时间

    - (void)longPress:(UILongPressGestureRecognizer *)longPress {

        NSLog(@"长按手势:");

        switch (longPress.state) {

            case UIGestureRecognizerStateBegan:

                NSLog(@"开始");

                break;

            case UIGestureRecognizerStateEnded:

                NSLog(@"结束");

                break;

            default:

                break;

        }

    }

    5. UIPinGestureRecognizer

    捏合

    - (void)pinchGestureHandle:(UIPinchGestureRecognizer *)pgr

    {

        switch (pgr.state) {

            case UIGestureRecognizerStateBegan:

            case UIGestureRecognizerStateChanged:

            {

                UIView *view = pgr.view;

                view.transform = CGAffineTransformScale(view.transform, pgr.scale, pgr.scale);

                //缩放比例会累加,所以需要重新复位

                pgr.scale = 1;

            }

                break;

            case UIGestureRecognizerStateEnded:

                NSLog(@"捏合手势结束");

                break;

            default:

                break;

        }    

    }

    加一个复位按钮

    _imageView.transform = CGAffineTransformIdentity;

    6. UIRotationGestureRecognizer

    旋转

    - (void)rotation:(UIRotationGestureRecognizer *)sender {

     switch (sender.state) {

            case UIGestureRecognizerStateBegan:

            case UIGestureRecognizerStateChanged:

            {

                UIView *view = sender.view;

                view.transform = CGAffineTransformRotate(view.transform, sender.rotation);

                sender.rotation = 0;

            }

                break;

                

            default:

                break;

        }

    }

    7轻扫

    //添加滑动事件,有方向限制

     - (void)addSwipe {

       

        UISwipeGestureRecognizerDirection directions[] = {

            UISwipeGestureRecognizerDirectionDown,

            

            UISwipeGestureRecognizerDirectionLeft,

            

            UISwipeGestureRecognizerDirectionRight,

            

            UISwipeGestureRecognizerDirectionUp

        };

        for (int i = 0; i < 4 ; i++) {

            UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];

            swipe.direction = directions[i];

            [self.myLabel addGestureRecognizer:swipe];

        }

    }

    #define step 10;

    - (void)swipe:(UISwipeGestureRecognizer*)sender {

        UIView *view = sender.view;

        CGPoint offset = CGPointZero;

        switch (sender.direction) {

            case UISwipeGestureRecognizerDirectionUp:

                offset.y -= step;

                

                break;

            case UISwipeGestureRecognizerDirectionDown:

                offset.y += step;

                break;

            case UISwipeGestureRecognizerDirectionLeft:

                offset.x -= step;

                break;

            case UISwipeGestureRecognizerDirectionRight:

                offset.x += 10;

                break;

                

            default:

                break;

        }

        view.transform = CGAffineTransformTranslate(view.transform, offset.x, offset.y);

    }

    是否允许识别多个手势

    遵守协议<UIGestureRecognizerDelegate>

    /**

     *  实现协议的方法// 一个手势检测到了,并不影响另一个手势的检测

     */

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

        if (gestureRecognizer.view == otherGestureRecognizer.view) {

            return YES;

        }

        return NO;

    }

    晃动事件

    参考day08

    晃动事件接口

    • (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
      {
      [super motionBegan:motion withEvent:event];
      //NSLog(@”晃动开始”);
      }
    • (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
      {
      NSLog(@”晃动结束”);
      }
    • (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
      {
      NSLog(@”滑动被取消”);
      }

    // 把image中的rect区域拿出来作为一个新的图片

    • (UIImage )clipImage:(UIImage )image withRect:(CGRect)rect {
    • CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
    • return [UIImage imageWithCGImage:imageRef];
    • }
  • 相关阅读:
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
  • 原文地址:https://www.cnblogs.com/jiang-xiao-yan/p/5952488.html
Copyright © 2011-2022 走看看