Objective c 设置动画时,UILabels文本将消失

Objective c 设置动画时,UILabels文本将消失,objective-c,Objective C,我有以下代码: - (void)my_button_tapped { if (my_button.tag == 0) { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; my_label.frame = CGRectMake(450, 455, 200, 20); [UIView commitAnimations]; [my_button setBa

我有以下代码:

- (void)my_button_tapped
{
if (my_button.tag == 0)
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    my_label.frame = CGRectMake(450, 455, 200, 20);
    [UIView commitAnimations];

    [my_button setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
    my_button.tag = 1;
}
else
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    my_label.frame = CGRectMake(450, 455, 0, 20);
    [UIView commitAnimations];

    [my_button setBackgroundImage:nil forState:UIControlStateNormal];
    my_button.tag = 0;
}
}

当我第一次点击我的_按钮时,标签扩展到200px宽度,当我再次按下按钮时,标签减小到0px宽度,但按下按钮时,文本立即消失。怎么了

问题在于按钮知道在动画结束时无法显示文本(由于宽度),因此它会隐藏文本标签。要获得您想要的效果,您可能需要使用
cAffineTransform
scale将其压缩为零宽度。

这是如何使用的?@WilhelmMichaelsen您需要使用
cAffineTransformMakeScale(0,1)
。返回在labels
.transform
属性上设置的CGAffineTransform。将其设置为动画的一部分应该不会有任何问题。