简体   繁体   English

UIView Animation 完成后移除 layer.cornerRadius

[英]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:几个问题:

  • Do you have masksToBounds set to YES?您是否将 maskToBounds 设置为 YES?
  • If you're not using a custom UILabel subclass, are you wrapping the label in a view?如果您不使用自定义 UILabel 子类,是否将 label 包装在视图中?
  • How is the animation being triggered? animation 是如何被触发的? Is it by a button?是通过按钮吗? A callback from a NSURLRequest?来自 NSURLRequest 的回调? If it's triggered by an async callback are you jumping back on the main queue to perform the animation?如果它是由异步回调触发的,您是否会跳回主队列以执行 animation?
  • If the animation is triggered automatically within the lifecycle, which lifecycle method is it triggered in?如果 animation 在生命周期内自动触发,它是在哪个生命周期方法中触发的?

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:这就是它的样子:

Toast_Animation_With_Corner_Radius

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.

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