2016-07-03 8 views
1

開始点をovalInRect形式の「right」から「left」に変更する方法はありますか? で、ovalInRectの開始点

let ovalPath = UIBezierPath(ovalInRect: CGRect(x: 515, y: 276.5, width: 280.5, height: 214.5)) 
     let ovalShapeLayer = CAShapeLayer() 
     ovalShapeLayer.fillColor = UIColor.clearColor().CGColor 
     ovalShapeLayer.strokeColor = UIColor.lightGrayColor().CGColor 
     ovalShapeLayer.lineWidth = 1.5 
     ovalShapeLayer.path = ovalPath.CGPath 

     self.view.layer.insertSublayer(ovalShapeLayer, atIndex: 1) 

おかげで原点を中心と直径1の円と

+0

あなたは' bezierPathWithArcCenterを使用している場合左から始まります。しかしそれは確かにサークルです。左から楕円が必要な場合は、独自の 'UIBezierPath'を定義するか、このルーチンを使用する必要がありますが、ビュー/レイヤーを変換してそれを反転させる必要があります。 – Rob

+0

私はこれを試しましたが、何もしません... mirrorXorigin = CGAffineTransformMakeScale(-1.0,1.0) ovalPath.applyTransform(mirrorXorigin) –

+0

shapeLayerにapplyTransformがありません –

答えて

3

スタート...私はそれをどのように作成するかに関係なく、「開始」ポイントは「右側に常にあるようですあなたが欲しい開始角度その後、あなたの矩形に内接する楕円形にその円を変換

extension UIBezierPath { 

    convenience init(ovalInRect rect: CGRect, startAngle: CGFloat, clockwise: Bool) { 
     self.init() 
     // Create a circle at the origin with diameter 1. 
     addArcWithCenter(.zero, radius: 0.5, startAngle: startAngle, endAngle: startAngle + 2 * CGFloat(M_PI), clockwise: clockwise) 
     closePath() 

     // Construct a transform that moves the circle to inscribe `rect`. 
     var transform = CGAffineTransformIdentity 
     // This part moves the center of the circle to the center of `rect`. 
     transform = CGAffineTransformTranslate(transform, rect.midX, rect.midY) 
     // This part scales the circle to an oval with the same width and height as `rect`. 
     transform = CGAffineTransformScale(transform, rect.width, rect.height) 

     applyTransform(transform) 
    } 

} 

使用例:。。

let oval = UIBezierPath(ovalInRect: CGRectMake(50, 20, 100, 200), startAngle: CGFloat(-M_PI), clockwise: true) 
Swift.print(oval.bounds) 

出力:半径::startAngleの:endAngle:時計回り: `あなたが-M_PI``からM_PI` `に行くので、もし、あなたが、それを開始と終了を制御することができます

(50.0, 20.0, 100.0, 200.0) 
関連する問題