我正在阅读 Big Nerd Ranch 指南第 4 版,但在处理 View 和 View 层次结构的章节中,我遇到了一些与 View 来源有关的问题。
我已经在我的 View Controller 中添加了一个自定义 subview 并覆盖了 drawRect 以在屏幕中间绘制一堆圆圈,但是,圆圈的原点不会改变并且它们都卡在了左边角,我不知道为什么。我实际上只是从书中复制了代码,它似乎工作得很好。
这是我的 drawRect:
- (void)drawRect:(CGRect)rect
{
CGRect bounds = self.bounds;
//Figure out the center of the bounds rectangle
CGPoint center;
center.x = bounds.origin.x + bounds.size.width/2.0;
center.y = bounds.origin.y + bounds.size.height/2.0;
//The largest circle will circumscribe the view
float maxRadius = hypotf(bounds.size.width, bounds.size.height)/2.0;
UIBezierPath *path = [[UIBezierPath alloc]init];
//Loops and create multiple circles with different radii
for (float currentRadius = maxRadius; currentRadius > 0 ; currentRadius -= 20) {
[path moveToPoint:CGPointMake(center.x +currentRadius, center.y)];
[path addArcWithCenter:center radius:currentRadius startAngle:0.0 endAngle:M_PI*2.0 clockwise:YES];
}
path.lineWidth = 10;
[self.circleColor setStroke];
[path stroke];
}
这是结果。一堆不在屏幕中央的圆圈......
最佳答案
我会做一些事情。
通过创建另一个 View (较小的、亮蓝色的或任何其他 View )找出 View 的原点,然后说 centerView.center = biggerView.center。当然, Hook 。
根据您的结果,适当移动您的框架/ View 。
自动布局是否有效?定义对了吗?尝试将其关闭。
尝试使用 self.center.x 和 y 而不是创建另一个 center.x/y。
首先实际执行此操作。记录或断点边界并确保它的宽度不为 0。您可以使用 NSStringFromCGRect() 打印它
关于iOS - 无法更改 View 原点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24721620/