2016-03-10 27 views
1

我試圖設置自定義UIView的collison邊界路徑。我目前所做的是將SVG文件轉換爲使用PocketSVG的路徑並從中創建UIBezierPath。這裏是我的代碼:UIView中的碰撞邊界路徑起源iOS 9

@implementation CustomView { 

    UIBezierPath * _mask; 
    CGPathRef _myPath; 

} 

- (UIDynamicItemCollisionBoundsType) collisionBoundsType { 

    return UIDynamicItemCollisionBoundsTypePath; 

} 

- (UIBezierPath *) collisionBoundingPath { 

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

    _mask = [UIBezierPath bezierPathWithCGPath:_myPath]; 

    return _mask; 

} 

它的工作原理「正常」,但UIBezierPath在我的視野的中心繪製的,所以這是我所得到的:

左:實際行爲--->右鍵預期行爲

enter image description here

而且由於碰撞邊界是不可見的碰撞似乎觸摸視圖VISU之前發生盟友(他們實際上是因爲碰撞邊界路徑的起源而感動)。

根據Apple documentation regarding collisionBoundingPath

的路徑對象創建必須代表與 逆時針或順時針卷繞的凸多邊形,而且路徑不得 與自身相交。路徑的(0,0)點必須位於相應動態項目的中心點 處。如果中心點 與路徑的原點不匹配,則碰撞行爲可能不起作用 預期。

所以我的主要問題是,由於路徑的(0,0)座標必須位於我的視圖的中心,所以我如何才能實現我正在尋找的?完美的場景是如果UIBezierPath開始在其UIView的原點繪製,但由於我從SVG添加路徑,它會自動從視圖的中心繪製。

回答

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; 

} 
+0

我不得不打一點,但它的工作,謝謝你! – iDeC