为什么我的椭圆不在中心?

时间:2016-01-31 01:14:59

标签: swift core-graphics transformation quartz-graphics

我试图在他们的中心周围设置几个以不同大小和不同旋转为中心的椭圆,但我得到了这个:

enter image description here

到目前为止我的游乐场代码:

import UIKit

func radians(x: Int)->CGFloat{
    let pi = M_PI
    return CGFloat(pi * Double(x) / 180.0)
}

let size = CGSize(width: 600, height: 600)
UIGraphicsBeginImageContextWithOptions(size, true, 0.0)

let context = UIGraphicsGetCurrentContext()

CGContextSetStrokeColorWithColor(context, UIColor.whiteColor().CGColor);

let center:CGPoint=CGPointMake(size.width/2, size.height/2)

CGContextSetLineWidth(context, 2.0);

var step=0
while step<12{
var width:CGFloat = 25.0+CGFloat(step)*20.0
var height:CGFloat = 100.0+CGFloat(step)*20.0
CGContextRotateCTM( context, radians( 10 ) ) ;
var circle:CGRect = CGRectMake(center.x-width/2,center.y-height/2,width,height);


CGContextAddEllipseInRect(context, circle);

CGContextStrokePath(context);
    step++
}

let image = UIGraphicsGetImageFromCurrentImageContext()


UIGraphicsEndImageContext()

1 个答案:

答案 0 :(得分:0)

预期结果是:

enter image description here

我在SEVERAL尝试后找到了解决方案。

import UIKit

func radians(x: Int)->CGFloat{
    let pi = M_PI
    return CGFloat(pi * Double(x) / 180.0)
}

let size = CGSize(width: 300, height: 300)
UIGraphicsBeginImageContextWithOptions(size, true, 0.0)

let context = UIGraphicsGetCurrentContext()

CGContextSetStrokeColorWithColor(context, UIColor.whiteColor().CGColor);

let center:CGPoint=CGPointMake(size.width/2, size.height/2)

CGContextSetLineWidth(context, 2.0);
CGContextTranslateCTM(context, center.x, center.y)
var step=0
while step<12{
var width:CGFloat = 30.0+CGFloat(step)*CGFloat(step)
var height:CGFloat = 50.0+CGFloat(step)*CGFloat(2*step)

var circle:CGRect = CGRectMake(-width/2,-height/2,width,height)
    CGContextRotateCTM( context, radians(step))

CGContextAddEllipseInRect(context, circle);

CGContextStrokePath(context);
    step++
}

let image = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()