gpt4 book ai didi

iOS多次添加 subview 和子图层

转载 作者:行者123 更新时间:2023-11-29 00:41:26 25 4
gpt4 key购买 nike

我有一个构建界面的方法,它向主 UIView(下面代码中的 containerView)添加一个 subView 和一些 subLayers:

- (void)gradeAnimation:(NSNumber*)grade withDuration:(double)duration {

scoreLabel = [[UICountingLabel alloc] init];
scoreLabel.frame = CGRectOffset(_gradeLabel.frame, 0, 5);
[_containerView addSubview:scoreLabel];

// Other code

UIBezierPath *circlePathMin = [UIBezierPath bezierPathWithArcCenter:CGPointMake(_gradientView.center.x, _gradientView.center.y) radius:_gradientView.frame.size.height * 0.5 - 5 startAngle:-M_PI_4*1.2 endAngle:angle1 clockwise:YES];
circleMin = [CAShapeLayer layer];
circleMin.path = circlePathMin.CGPath;
circleMin.lineCap = kCALineCapButt;
circleMin.fillColor = [UIColor clearColor].CGColor;
circleMin.lineWidth = 14;
circleMin.strokeColor = [UIColor colorWithRed:246.0/255.0f green:246.0f/255.0f blue:246.0f/255.0f alpha:0.7f].CGColor;
circleMin.zPosition = 3;
[_containerView.layer addSublayer:circleMin];

UIBezierPath *circlePathMax = [UIBezierPath bezierPathWithArcCenter:CGPointMake(_gradientView.center.x, _gradientView.center.y) radius:_gradientView.frame.size.height * 0.5 - 5 startAngle:angle2 endAngle:5*M_PI_4*1.2 clockwise:YES];
circleMax = [CAShapeLayer layer];
circleMax.path = circlePathMax.CGPath;
circleMax.lineCap = kCALineCapButt;
circleMax.fillColor = [UIColor clearColor].CGColor;
circleMax.lineWidth = 14;
circleMax.strokeColor = [UIColor colorWithRed:246.0/255.0f green:246.0f/255.0f blue:246.0f/255.0f alpha:0.7f].CGColor;
circleMax.zPosition = 3;
[_containerView.layer addSublayer:circleMax];

UIBezierPath *circlePathMiddle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(_gradientView.center.x, _gradientView.center.y) radius:_gradientView.frame.size.height * 0.5 - 5 startAngle:angle1+offsetRight endAngle:angle2+offsetLeft clockwise:YES];
circleMiddle = [CAShapeLayer layer];
circleMiddle.path = circlePathMiddle.CGPath;
circleMiddle.lineCap = kCALineCapButt;
circleMiddle.fillColor = [UIColor clearColor].CGColor;
circleMiddle.lineWidth = 14;
circleMiddle.strokeColor = [UIColor colorWithRed:246.0/255.0f green:246.0f/255.0f blue:246.0f/255.0f alpha:0.7f].CGColor;
circleMiddle.zPosition = 3;
[_containerView.layer addSublayer:circleMiddle];
}

我的问题是,如果我多次调用此方法,每次都会添加 subview 和子层,并且它们不会重绘,正如我希望的那样。为什么会这样?

最佳答案

我们只讨论单个对象,scoreLabel 是 UICountingLabel 的对象。每次调用gradeAnimation:withDuration:方法时,您都在创建一个新对象并将其添加到您的 View 中。

您可以获取一个属性,然后启动并添加您的 View 一次,并且在该方法中您可以更改对象的位置或其他内容。

如果您不想更改当前的方法,那么在调用该方法之前,您必须从 View 中删除以前的对象。这样,在您的视野中,一次我们只能看到一个物体。

关于iOS多次添加 subview 和子图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39387559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com