【问题标题】:Collision bounding path origin in UIView iOS 9UIView iOS 9中的碰撞边界路径原点
【发布时间】:2016-03-10 01:16:37
【问题描述】:

我正在尝试设置自定义 UIView 的碰撞边界路径。我目前所做的是使用 PocketSVG 将 SVG 文件转换为路径并从中创建 UIBezierPath。这是我的代码:

@implementation CustomView {

    UIBezierPath * _mask;
    CGPathRef _myPath;

}

- (UIDynamicItemCollisionBoundsType) collisionBoundsType {

    return UIDynamicItemCollisionBoundsTypePath;

}

- (UIBezierPath *) collisionBoundingPath {

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

    _mask = [UIBezierPath bezierPathWithCGPath:_myPath];

    return _mask;

}

它“正常”工作,但 UIBezierPath 绘制在我视图的中心,所以这就是我得到的:

左:实际行为 ---> 右:预期行为

并且由于碰撞边界是不可见的,因此碰撞似乎发生在视觉上触摸视图之前(由于碰撞边界路径的原点,它们实际上是触摸的)。

根据Apple documentation regarding collisionBoundingPath

你创建的路径对象必须代表一个凸多边形 逆时针或顺时针缠绕,路径不得 与自身相交。路径的 (0, 0) 点必须位于 对应动态项的中心点。如果中心点 与路径的原点不匹配,碰撞行为可能无法正常工作 预计。

所以我的主要问题是,由于路径的 (0,0) 坐标必须位于我的视图中心,我怎样才能实现我正在寻找的东西?完美的场景是如果 UIBezierPath 开始在其 UIView 的原点绘制,但由于我从 SVG 添加路径,它会自动从视图的中心绘制。

【问题讨论】:

    标签: ios objective-c svg uikit uibezierpath


    【解决方案1】:

    您可以使用转换将 CGPath 移动到适当的位置。例如:

    - (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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-05
      • 2015-01-31
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-18
      • 2013-04-01
      • 1970-01-01
      相关资源
      最近更新 更多