改变UILabel文本搞砸了动画?

时间:2013-05-11 02:24:42

标签: iphone ios uilabel uianimation

我的应用程序必须根据用户的选择进行计算和动画,我需要在动画中使用之前更新UILabel,问题是每当我设置UILabel时文字它弄乱了动画,我不明白为什么。我试过[self.view.layer removeAllAnimations];在更改UILabel文本之前仍然没有好处。我甚至尝试以编程方式调用IBAction并更改该块中的UILabel文本,但它仍然会混淆动画。似乎没有什么工作,它肯定是关于时间的,因为更改viewDidLoad中的UILabel文本确实可以正常工作。我还通过在动画之前更改另一个UILabel的文本来检查是否是故事板问题,并且出现同样的问题。如果它在这里有帮助就是动画代码:

    -(IBAction) swipeDown:(UISwipeGestureRecognizer *)recognizer{
multiply.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
subtract.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
add.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
divide.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
circle.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
signcircle.frame = CGRectMake(145.0f, 70.0f, signcircle.frame.size.width,signcircle.frame.size.height);
sign.frame = CGRectMake(152.0f, 68.0f, sign.frame.size.width,sign.frame.size.height);
type=@"multiply"; 
if(fivebutton.highlighted){
    circle.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
    multiply.transform = CGAffineTransformMakeScale(1.35f, 1.35f);
    multiply.frame = CGRectMake(120.0f, 248.0f, multiply.frame.size.width, multiply.frame.size.height);
    [UIView animateWithDuration:.25f animations:^{
        add.frame = CGRectMake(130.0f, 202.0f, add.frame.size.width, add.frame.size.height);
        add.alpha=1.0;
        subtract.frame = CGRectMake(82.0f, 255.0f, subtract.frame.size.width, subtract.frame.size.height);
        subtract.alpha=1.0;
        divide.frame = CGRectMake(178.0f, 255.0f, divide.frame.size.width, divide.frame.size.height);
        divide.alpha=1.0;
        multiply.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
        multiply.frame = CGRectMake(130.0f, 305.0f, multiply.frame.size.width, divide.frame.size.height);
        multiply.alpha=1.0;
        circle.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
        circle.alpha=1.0;
    UIImage *downimage = [UIImage imageNamed: @"multiplydown.png"];
    [multiply setImage:downimage];
    onebutton.alpha=0.1;
    one.alpha=0.1;
    twobutton.alpha=0.1;
    two.alpha=0.1;
    threebutton.alpha=0.1;
    three.alpha=0.1;
    fourbutton.alpha=0.1;
    four.alpha=0.1;
    sixbutton.alpha=0.1;
    six.alpha=0.1;
    fivebutton.alpha=0.0;
    sevenbutton.alpha=0.1;
    seven.alpha=0.1;
    eightbutton.alpha=0.1;
    eight.alpha=0.1;
    ninebutton.alpha=0.1;
    nine.alpha=0.1;
    zerobutton.alpha=0.1;
    zero.alpha=0.1;
    decimalbutton.alpha=0.1;
    decimal.alpha=0.1;
    equalsbutton.alpha=0.1;
    equals.alpha=0.1;
    }];
    [UIView commitAnimations];
}

在这种情况下,UILabel文本更改会导致“添加”,“减去”,“除”和“乘”(UIImageView')合并在一起,而不是像动画一样传播到。

    -(void) fiveTapEnded{
var2.hidden=NO;
if ([type isEqual:@"add"]) {
    NSLog(@"Is add");
}
if ([type isEqual:@"subtract"]) {
    NSLog(@"Is subtract");
}
if ([type isEqual:@"multiply"]) {
    var1.textAlignment = NSTextAlignmentRight;
    var1.frame = CGRectMake(40.0f, 70.0f, var1.frame.size.width,var1.frame.size.height);
    var2.frame = CGRectMake(145.0f, 70.0f, var2.frame.size.width,var2.frame.size.height);
    [UIView animateWithDuration:.25f animations:^{
        add.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
        add.alpha=0.0;
        multiply.transform = CGAffineTransformMakeScale(1.5f, 1.5f);
        multiply.alpha=0.0;
        divide.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
        divide.alpha=0.0;
        circle.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
        circle.alpha=0.0;
        subtract.alpha=0.0;
        var1.frame = CGRectMake(0.0f, 20.0f, var1.frame.size.width,var1.frame.size.height);
        var2.frame = CGRectMake(185.0f, 20.0f, var2.frame.size.width,var2.frame.size.height);
        signcircle.frame = CGRectMake(145.0f, 30.0f, signcircle.frame.size.width,signcircle.frame.size.height);
        sign.frame = CGRectMake(152.0f, 26.5f, sign.frame.size.width,sign.frame.size.height);
        result.frame = CGRectMake(8.0f, 100.0f, result.frame.size.width,result.frame.size.height);
        signcircle.alpha=1.0;
        sign.alpha=1.0;
        var2.alpha=1.0;
        result.alpha=1.0;
        one.alpha=1.0;
        onebutton.alpha=1.0;
        twobutton.alpha=1.0;
        two.alpha=1.0;
        three.alpha=1.0;
        threebutton.alpha=1.0;
        fourbutton.alpha=1.0;
        four.alpha=1.0;
        sixbutton.alpha=1.0;
        six.alpha=1.0;
        five.alpha=1.0;
            fivebutton.alpha=1.0;;
        sevenbutton.alpha=1.0;
        seven.alpha=1.0;
        eightbutton.alpha=1.0;
        eight.alpha=1.0;
        ninebutton.alpha=1.0;
        nine.alpha=1.0;
        zerobutton.alpha=1.0;
        zero.alpha=1.0;
        decimalbutton.alpha=1.0;
        decimal.alpha=1.0;
        equalsbutton.alpha=1.0;
        equals.alpha=1.0;
        }];
    [self.view.layer removeAllAnimations];
    [self functionToBeCalled:self];
}
if ([type isEqual:@"divide"]) {
    NSLog(@"Is divide");
}
}

在这种情况下,它会导致“var1”和“var2”(UILabel's)不向上移动。

我理解它令人困惑,我一直在各地寻找并找不到类似的问题,所以这是我的最后一招,如果有人有任何解决方案或提示将非常感激。

1 个答案:

答案 0 :(得分:1)

如果,通过“弄乱动画”,你的意思是它会回到它的起始位置,那么你可能已经自动布局了。更改UILabel时会自动重新应用自动布局约束,因此任何需要更改帧的动画都将被阻止。最重要的是,当使用带有指示布局的约束的自动布局时,您不应手动更改framecenter值。

这显然有两种可能的补救措施:

  1. 最简单,您可以关闭自动布局。然后标签的设置将不会触发任何要应用的约束。有关非常类似的问题,请参阅NSTimer blocks other animations

  2. 如果您想要启用自动布局,则不应通过更改控件的frame(或center)属性来设置动画,而应创建{{ 1}}引用适当的约束,然后通过更改IBOutlet属性为它们设置动画。请参阅Animating an image view to slide upwards