【问题标题】:How to add a circleView progress to an UIView expanding to all UIView?如何将 circleView 进度添加到 UIView 扩展到所有 UIView?
【发布时间】:2018-01-31 00:52:30
【问题描述】:

我在情节提要中使用约束设置了UIView,我想使用带有UIBezierPathCAShapeLayer 向此视图添加一个圆形进度。

我已正确添加此内容,但圆圈并未扩展到所有视图。我在我的 viewDidLoad 函数中调用它。我的视图对调整 top-x 视图的高度和宽度有限制。

- (void)viewDidLoad {
[super viewDidLoad];

CAShapeLayer *borderLayer = [CAShapeLayer layer];
borderLayer.fillColor = [UIColor whiteColor].CGColor;
CGFloat lineWidth = 4;

CGRect rect = CGRectMake(lineWidth+lineWidth/2, lineWidth+lineWidth/2, self.myView.bounds.size.width/2, self.myView.bounds.size.height/2);
borderLayer.path = [UIBezierPath bezierPathWithOvalInRect:rect].CGPath;
borderLayer.strokeColor = [[UIColor redColor] CGColor];
borderLayer.lineWidth = lineWidth;
[borderLayer setFillColor:[UIColor clearColor].CGColor];
[self.myView.layer addSublayer:borderLayer];
}

圆圈进度未调整为UIView,显示如下:

我怎样才能把这个占据整个UIView 画成这样:

【问题讨论】:

    标签: ios objective-c uiview uibezierpath cashapelayer


    【解决方案1】:

    1 - 您的视图框架尚未在 viewDidLoad 中设置。您需要将此代码移动到 viewDidLayoutSubviews 或视图控制器生命周期的更高版本中。

    相关问题:

    Why am I having to manually set my view's frame in viewDidLoad?

    关于视图控制器生命周期的 Apple Docs: https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html#//apple_ref/doc/uid/TP40015214-CH6-SW1

    2 - 改变这个:

    CGRect rect = CGRectMake(lineWidth+lineWidth/2, lineWidth+lineWidth/2, self.myView.bounds.size.width/2, self.myView.bounds.size.height/2);
    

    到这里:

    CGRect rect = CGRectMake(lineWidth/2, lineWidth/2, self.myView.bounds.size.width - lineWidth, self.myView.bounds.size.height - lineWidth);
    

    结果(我在测试中使用了稍微不同的 lineWidth 和 view size):

    【讨论】:

    • 感谢您的回答,但替换这条线后,圆圈出现在错误的位置,已调整大小(如双精度)且未居中
    • 你在哪里添加这个borderLayer?
    • 我正在将它添加到我的视图中,即您可以在图片中看到的灰色 UIView。我正在使用最后一行:[self.myView.layer addSublayer:borderLayer];
    • 我的意思是,你在哪里调用这个代码?是在viewDidLoadviewDidLayoutSubviews,...?您可以添加其余代码吗?
    • 啊抱歉,我已经更新了我的问题。我将其调用到我的 viewdidload 中,可能是因为我的 UIView 限制了我的自动布局故事板?
    猜你喜欢
    • 2014-08-23
    • 2023-03-21
    • 2014-05-16
    • 2019-05-09
    • 2021-05-07
    • 2023-04-05
    • 2021-12-22
    • 1970-01-01
    • 2015-03-12
    相关资源
    最近更新 更多