4

我的应用程序必须根据用户做出的选择进行计算和动画,并且我需要UILabel在动画中使用它之前更新它,问题是每当我设置UILabel文本时它会弄乱动画,我不知道为什么。我试过 [self.view.layer removeAllAnimations]; 在更改UILabel文本之前,仍然没有好处。我什至尝试以编程方式从该块中调用IBAction和更改UILabel文本,但它仍然会弄乱动画。似乎没有任何效果,这肯定是关于时间的,因为更改UILabelviewDidLoad 中的文本确实可以正常工作。我还通过在动画之前更改另一个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's)合并在一起,而不是像动画应该的那样展开。

    -(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) 不会向上移动。

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

4

1 回答 1

1

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

这显然留下了两种可能的补救措施:

  1. 最简单的,你可以关闭自动布局。然后标签的设置将不会触发任何要应用的约束。有关非常相似的问题,请参阅NSTimer 阻止其他动画

  2. 如果您想保持自动布局打开,那么您不应该通过更改控件的frame(或center)属性来制作动画,而是应该IBOutlet为适当的约束创建引用,然后通过更改constant属性来制作动画。请参阅动画图像视图以向上滑动

于 2013-05-11T02:35:27.003 回答