繁体   English   中英

UIView Animation 完成后移除 layer.cornerRadius

[英]UIView Animation Removes layer.cornerRadius After Completion

我有一个UILabel ,我正在为约束设置动画,以便它下降到视图中。 我正在使用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 在viewDidLoad中设置。

有没有办法防止这种情况发生?

我怀疑你在这里继承了 UILabel ,因为看起来你在那里有填充,对吗?

您在那里进行的任何自定义绘图/计算都可能出现问题,因此发布该代码以供检查也可能会有所帮助。

几个问题:

  • 您是否将 maskToBounds 设置为 YES?
  • 如果您不使用自定义 UILabel 子类,是否将 label 包装在视图中?
  • animation 是如何被触发的? 是通过按钮吗? 来自 NSURLRequest 的回调? 如果它是由异步回调触发的,您是否会跳回主队列以执行 animation?
  • 如果 animation 在生命周期内自动触发,它是在哪个生命周期方法中触发的?

我无法在带有香草 UILabel 的测试项目中重现该问题。 然后我尝试了一个包含额外填充的 UILabel 子类,但仍然无法在那里重现它。

我在下面包含了示例代码片段:

#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

这就是它的样子:

Toast_Animation_With_Corner_Radius

您还需要在viewDidLayoutSubviews上设置角半径

override func viewDidLayoutSubviews() {

    super.viewDidLayoutSubviews()
    label.layer.cornerRadius = yourValue
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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