iOS - 无法更改 View 原点

标签 ios objective-c drawrect

我正在阅读 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];
}

这是结果。一堆不在屏幕中央的圆圈......

最佳答案

我会做一些事情。

  1. 通过创建另一个 View (较小的、亮蓝色的或任何其他 View )找出 View 的原点,然后说 centerView.center = biggerView.center。当然, Hook 。

  2. 根据您的结果,适当移动您的框架/ View 。

  3. 自动布局是否有效?定义对了吗?尝试将其关闭。

  4. 尝试使用 self.center.x 和 y 而不是创建另一个 center.x/y。

  5. 首先实际执行此操作。记录或断点边界并确保它的宽度不为 0。您可以使用 NSStringFromCGRect() 打印它

关于iOS - 无法更改 View 原点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24721620/

相关文章:

iphone - 有没有办法在不离开 App 的情况下将评分发送到 App Store?

ios - 在两个 iphone 输出上播放两首歌曲 [iOS]

objective-c - UIImage 和 NSCoding iOS 5.1

ios - 不应该画画的时候..?

iOS - 获取 Twitter 显示名称和时间

ios - STPopUpController 将点击的数据传递给底部表单样式 iOS

ios - swift : How to create a UIView with transparent circle in the middle?

android - 通过将 Canvas 视为 View 来绘制矩形

ios - 检测结束拖动/缩放 MKMapView

objective-c - 是否可以使用 sharedHTTPCookieStorage 为 UIWebView 手动设置 cookie?