简体   繁体   English

UIView iOS 9中的碰撞边界路径原点

[英]Collision bounding path origin in UIView iOS 9

I'm trying to setup the collison bounding path of a custom UIView. 我正在尝试设置自定义UIView的Collison边界路径。 What I currently do is converting a SVG file to a path using PocketSVG and creating an UIBezierPath from it. 我目前要做的是使用PocketSVG将SVG文件转换为路径,并从中创建UIBezierPath。 Here's my code: 这是我的代码:

@implementation CustomView {

    UIBezierPath * _mask;
    CGPathRef _myPath;

}

- (UIDynamicItemCollisionBoundsType) collisionBoundsType {

    return UIDynamicItemCollisionBoundsTypePath;

}

- (UIBezierPath *) collisionBoundingPath {

    _myPath = [PocketSVG pathFromSVGFileNamed:@"line5"];

    _mask = [UIBezierPath bezierPathWithCGPath:_myPath];

    return _mask;

}

It works "correctly" but the UIBezierPath is drawn at the center of my view so this is what I get: 它可以“正确”地工作,但是UIBezierPath绘制在我的视图中心,所以这就是我得到的:

Left: Actual behavior ---> Right: Expected behavior 左:实际行为---> 右:预期行为

在此处输入图片说明

And since the collision bounds are invisible the collision seems to happen before touching the view visually (they are actually touching because of the origin of the collision bounds path). 而且由于碰撞边界是不可见的,因此碰撞似乎发生在视觉接触视图之前(实际上是由于碰撞边界路径的起源而接触)。

According to Apple documentation regarding collisionBoundingPath : 根据Apple文件中的flictingBoundingPath的规定

The path object you create must represent a convex polygon with counter-clockwise or clockwise winding, and the path must not intersect itself. 您创建的路径对象必须表示一个具有逆时针或顺时针缠绕的凸多边形,并且该路径不得与自己相交。 The (0, 0) point of the path must be located at the center point of the corresponding dynamic item. 路径的(0,0)点必须位于相应动态项目的中心点。 If the center point does not match the path's origin, collision behaviors may not work as expected. 如果中心点与路径的原点不匹配,则碰撞行为可能无法按预期进行。

So my main question is, since the (0,0) coordinates of the path must be at the center of my view, how can I achieve what I'm looking for? 因此,我的主要问题是,由于路径的(0,0)坐标必须位于我的视图中心,因此如何实现所需的内容? The perfect scenario would be if UIBezierPath start drawing at the origin of its UIView but since I'm adding a path from a SVG it is automatically draw from the center of the view. 理想的情况是UIBezierPath从其UIView的原点开始绘制,但是由于我要从SVG添加路径,因此它会自动从视图的中心绘制。

You can use a transformation to move the CGPath into the appropriate position. 您可以使用转换将CGPath移到适当的位置。 For example: 例如:

- (UIBezierPath *) collisionBoundingPath {

    _myPath = [PocketSVG pathFromSVGFileNamed:@"line5"];

    CGAffineTransform translation = CGAffineTransformMakeTranslation(-self.bounds.size.width * 0.5,-self.bounds.size.height * 0.5);
    CGPathRef movedPath = CGPathCreateCopyByTransformingPath(_myPath,&translation);

    _mask = [UIBezierPath bezierPathWithCGPath:movedPath];

    return _mask;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM