[英]UIView Animation Removes layer.cornerRadius After Completion
I have a UILabel
that I am animating the constraints for so that it drop down into view.我有一个
UILabel
,我正在为约束设置动画,以便它下降到视图中。 I am using layer.cornerRadius
to give the view rounded corners, but for whatever reason after the animation completes the corner radius is removed.我正在使用
layer.cornerRadius
为视图提供圆角,但是在 animation 完成后,无论出于何种原因,圆角半径都被删除了。
[UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.4 options:UIViewAnimationOptionCurveEaseInOut animations:^{
if (shouldShow) {
self.labelOverMapTopConstraint.constant = 16;
} else {
self.labelOverMapTopConstraint.constant = -40;
}
[self.view layoutIfNeeded];
} completion:nil];
cornerRadius is set in viewDidLoad
. cornerRadius 在
viewDidLoad
中设置。
Is there a way to prevent this from happening?有没有办法防止这种情况发生?
I suspect you're subclassing UILabel here since it looks like you have padding in there, is that correct?我怀疑你在这里继承了 UILabel ,因为看起来你在那里有填充,对吗?
There could be something going awry with any custom drawing/calculations you're doing in there, so it would probably be helpful to post that code for inspection as well.您在那里进行的任何自定义绘图/计算都可能出现问题,因此发布该代码以供检查也可能会有所帮助。
A few questions:几个问题:
I wasn't able to reproduce the issue in a test project with a vanilla UILabel.我无法在带有香草 UILabel 的测试项目中重现该问题。 I then tried it with a UILabel subclass which includes additional padding and still wasn't able to reproduce it there.
然后我尝试了一个包含额外填充的 UILabel 子类,但仍然无法在那里重现它。
I've included example code snippets below:我在下面包含了示例代码片段:
#import "ViewController.h"
#import "R4NInsetLabel.h"
@interface ViewController ()
@property BOOL showingToast;
@property (strong, nullable) IBOutlet R4NInsetLabel *toastLabel;
@property (strong, nullable) IBOutlet NSLayoutConstraint *toastLabelTopConstraint;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
self.showingToast = NO;
// start with the label pushed off the top of the screen
self.toastLabelTopConstraint.constant = -40.0f;
self.toastLabel.layer.cornerRadius = 6.0f;
self.toastLabel.layer.masksToBounds = YES;
}
- (IBAction)toggleToast:(id)sender {
[UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.4 options:UIViewAnimationOptionCurveEaseInOut animations:^{
if (self.showingToast == NO) {
self.toastLabelTopConstraint.constant = 16;
self.showingToast = YES;
} else {
self.toastLabelTopConstraint.constant = -40;
self.showingToast = NO;
}
[self.view layoutIfNeeded];
} completion:nil];
}
@end
#import "R4NInsetLabel.h"
IB_DESIGNABLE
@interface R4NInsetLabel()
@property IBInspectable CGFloat contentPadding;
@property (nonatomic) UIEdgeInsets contentInsets;
- (CGSize)_addInsetsToSize:(CGSize)size;
@end
@implementation R4NInsetLabel
- (UIEdgeInsets)contentInsets {
return UIEdgeInsetsMake(self.contentPadding, self.contentPadding, self.contentPadding, self.contentPadding);
}
- (CGSize)_addInsetsToSize:(CGSize)size {
CGFloat width = size.width + self.contentInsets.left + self.contentInsets.right;
CGFloat height = size.height + self.contentInsets.top + self.contentInsets.bottom;
return CGSizeMake(width, height);
}
- (void)drawTextInRect:(CGRect)rect {
CGRect insetRect = UIEdgeInsetsInsetRect(rect, self.contentInsets);
[super drawTextInRect:insetRect];
}
- (CGSize)intrinsicContentSize {
CGSize baseSize = [super intrinsicContentSize];
return [self _addInsetsToSize:baseSize];
}
- (CGSize)sizeThatFits:(CGSize)size {
CGSize baseSize = [super sizeThatFits:size];
return [self _addInsetsToSize:baseSize];
}
@end
And here's what it looks like:这就是它的样子:
You also need to set the corner radius on viewDidLayoutSubviews
您还需要在
viewDidLayoutSubviews
上设置角半径
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
label.layer.cornerRadius = yourValue
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.