• UIPanGestureRecognizer中translationInView的理解


    原因是在破船大牛的blog上面看到了一个demo

    #import <UIKit/UIKit.h>

     @interface ViewController : UIViewController

    @property (weak, nonatomic) IBOutlet UITextView *textView;

     @property (weak, nonatomic) IBOutlet UIImageView *imageView;

     @property (nonatomic, assign) CGPoint gestureStartingPoint;

    @property (nonatomic, assign) CGPoint gestureStartingCenter;

    @end

    - (void)viewDidLoad

    {

        [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

        UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self

                                                                                 action:@selector(imagePanned:)];

        [self.imageView addGestureRecognizer:panGes];

        

        self.textView.textContainer.exclusionPaths = @[[self translatedBezierPath]];

        

    }

     

     

    - (UIBezierPath *)translatedBezierPath

    {

        CGRect butterflyImageRect = [self.textView convertRect:self.imageView.frame fromView:self.view];

        UIBezierPath *newButterflyPath = [UIBezierPath bezierPathWithRect:butterflyImageRect];

        

        return newButterflyPath;

    }

     

    - (void)imagePanned:(id)sender

    {

        if ([sender isKindOfClass:[UIPanGestureRecognizer class]]) {

            UIPanGestureRecognizer *localSender = sender;

            

            if (localSender.state == UIGestureRecognizerStateBegan) {

                

                self.gestureStartingPoint = [localSender translationInView:self.textView];

                self.gestureStartingCenter = self.imageView.center;

        

            } else if (localSender.state == UIGestureRecognizerStateChanged) {

                CGPoint currentPoint = [localSender translationInView:self.textView];

                

                CGFloat distanceX = currentPoint.x - self.gestureStartingPoint.x;

                CGFloat distanceY = currentPoint.y - self.gestureStartingPoint.y;

                

                CGPoint newCenter = self.gestureStartingCenter;

                

                newCenter.x += distanceX;

                newCenter.y += distanceY;

                

                self.imageView.center = newCenter;

                

                self.textView.textContainer.exclusionPaths = @[[self translatedBezierPath]];

            } else if (localSender.state == UIGestureRecognizerStateEnded) {

                self.gestureStartingPoint = CGPointZero;

                self.gestureStartingCenter = CGPointZero;

            }

        }

    }

     

    看到了translationInView方法不明白是怎么回事,就看了一下官方文档解释是:

    - (CGPoint)translationInView:(UIView *)view方法的API解释如下:

     The translation of the pan gesture in the coordinate system of the specified view.

     Return Value

    A point identifying the new location of a view in the coordinate system of its designated superview.

    字面理解是:

    在指定的视图坐标系统中转换(拖动?) pan gesture

    返回参数:返回一个明确的新的坐标位置,在指定的父视图坐标系统中

    我理解为

    该方法返回在横坐标上、纵坐标上拖动了多少像素。

  • 相关阅读:
    Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十五)阶段总结
    [Beautifulzzzz的博客目录] 快速索引点这儿O(∩_∩)O~~,红色标记的是不错的(⊙o⊙)哦~
    OpenCV学习记录之摄像头调用
    很不错的python 机器学习博客
    华清远见Linux设备驱动(每章小结)
    Linux 查看服务器开放的端口号
    MySQL简单优化
    数据探索之数据质量分析
    两张图简说代理服务器和反向代理服务器
    集合类中嵌套定义和引用的举例
  • 原文地址:https://www.cnblogs.com/sanjianghuiliu/p/3957476.html
Copyright © 2020-2023  润新知