[英]Collision bounding path origin in UIView iOS 9
我正在嘗試設置自定義UIView的Collison邊界路徑。 我目前要做的是使用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文件中的flictingBoundingPath的規定 :
您創建的路徑對象必須表示一個具有逆時針或順時針纏繞的凸多邊形,並且該路徑不得與自己相交。 路徑的(0,0)點必須位於相應動態項目的中心點。 如果中心點與路徑的原點不匹配,則碰撞行為可能無法按預期進行。
因此,我的主要問題是,由於路徑的(0,0)坐標必須位於我的視圖中心,因此如何實現所需的內容? 理想的情況是UIBezierPath從其UIView的原點開始繪制,但是由於我要從SVG添加路徑,因此它會自動從視圖的中心繪制。
您可以使用轉換將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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.