我现在通过CAShapeLayer绘制一个圆圈,我希望我的圆圈位于屏幕中间。圆圈中点必须是我视角的中间点。
我现在尝试这样做:
- (void)_initCircle {
[_circle removeFromSuperlayer];
_circle = [CAShapeLayer layer];
_radius = 100;
// Create a circle
_circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0 * _radius, 2.0 * _radius) cornerRadius:_radius].CGPath;
// Center the shape in self.view
_circle.position = CGPointMake(CGRectGetMidX(self.view.bounds) - _radius,
CGRectGetMidY(self.view.bounds) - _radius);
_circle.fillColor = [UIColor clearColor].CGColor;
_circle.lineWidth = 5;
// Add to parent layer
[self.view.layer addSublayer:_circle];
}
屏幕截图清楚地表明,圆圈在中间并不完美,因为标签位于屏幕中间(中心Y)的中心位置。那个圆圈的蓝色和黑色应该等于我认为的标签。
我没有看到计算错误,有人可以帮助我吗?
编辑:
将边界更改为框架并让半径关闭可以得到以下结果:
答案 0 :(得分:2)
图层相对于它们的锚点定位自己,所以你可以尝试这样做:
// Center the shape in self.view
_circle.position = self.view.center;
_circle.anchorPoint = CGPointMake(0.5, 0.5);
答案 1 :(得分:0)
import UIKit
class DrawCircleView.UIView{
init(frame:CGRect){
super,init(frame:frame)
//Initialization code
}
init(coder aDecoder:NSCoder!){
super.init(coder:aDecoder)
}
//Only override drawRect:if you perform custom drawing.
//An empty implementation adversely affects performance during animation.
override func drawRect(rect:CGRect)
{
//Drawing code
var context = UIGraphicsGetCurrentContext()
CGContextAddArc(context, 150,300,100,0,3.14*2,0)
//you can change (150/300/100),these arguments to point the position.
CGContextStrokePath(context)
}
}