简体   繁体   中英

iOS adding subViews and subLayers multiple times

I have a method that builds the interface adding a subView and some subLayers to the main UIView (containerView in the code below):

- (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];
}

My problem is that if I call this method multiple times, subview and sublayers are added each time and they are not redrawn, as instead I would like. Why is this happening?

Lets talk about only a single object, scoreLabel is a object of UICountingLabel. Each time you are calling gradeAnimation: withDuration: method, you are creating a new object and adding that to your view.

You can take a property, then initiate and add your view once, and in the method you can change the position or other stuff of the object.

If you do not want to change your current method, then before calling the method, you have to remove previous objects from your view. So that at a time we will have only one object in your view.

if you are using addSublayer or addSubview then definitely it will add new layer or new view to containerView . If you not want to add it every time and want to redraw (i mean new instance - as i understand from your redraw word in question) then add below line,

 _containerView = [[UIView alloc]initWithFrame:self.view.bounds]; // you can set your desired frame 

in gradeAnimation method as first line. so it will create new instance of of everytime. or you can first remove sublayers and subviews and then add new one!!! or you can create new containerView in method and can add subviews and layer to it and can assign it to _containerView .

First remove all sublayers from the containerView. Add below code at the starting of your method.

for (CALayer *layer in _containerView) {
    [layer removeFromSuperlayer];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM