gpt4 book ai didi

ios - UILabel 动画不正确

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:34 24 4
gpt4 key购买 nike

出于某种原因,UILabel 的文本想要在没有动画的情况下设置其对齐方式,我不知道如何让文本与标签的其余部分一起设置动画。

我现在有以下代码:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40,200,100,50)];
[label setText:@"Label Name"];
[label setBackgroundColor:[UIColor blueColor]];
[label setFont:[UIFont systemFontOfSize:17.0]];
[label setTextColor:[UIColor blackColor]];
[label setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:label];
[UIView animateWithDuration:3 animations:^{
[label setFrame:CGRectMake(40,200,200,300)];
}];

此代码在按钮的操作方法中调用。通常动画会与标签的创建分开发生,但我将其放在相同的方法中进行测试。

由于某种原因,当它运行时,标签会按预期完美调整大小,我可以看到框架调整大小。但是,文本会在水平轴上自行对齐到动画结束时的位置。但是,在垂直轴上,它会正确地设置动画。

现在这是动画的样子:

animate1 animate2 animate3

出于某种原因,标签的文本在上下移动时保持居中,但它不会像我认为的那样保持左右居中。

我找遍了,找不到解决办法。有谁知道 UILabel 的行为是这样的,是否有任何解决方法?

最佳答案

您可以添加一个 UIView,然后添加 UILabel 作为其 subview :

 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(40, 20, 100, 50)];
[view setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:view];

UILabel *label = [[UILabel alloc] init];
[label setText:@"Label Name"];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont systemFontOfSize:17.0]];
[label setTextColor:[UIColor blackColor]];
[label setTextAlignment:NSTextAlignmentCenter];
[label sizeToFit];
[label setNumberOfLines:0];
[label setFrame:CGRectOffset(label.frame, (view.frame.size.width - label.frame.size.width)/2, (view.frame.size.height - label.frame.size.height)/2)];
[view addSubview:label];

[UIView animateWithDuration:3 animations:^{
[view setFrame:CGRectMake(40, 20, 200, 300)];
[label setFrame:CGRectMake((view.frame.size.width - label.frame.size.width)/2, (view.frame.size.height - label.frame.size.height)/2, label.frame.size.width, label.frame.size.height)];
} completion:^(BOOL finish){

}];

关于ios - UILabel 动画不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15534022/

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