Pergunta

My app has to do a calculation and animation based on a choice the user makes and I need to update a UILabel before it is used in the animation, the problem is whenever I set the UILabel text it messes up the animation and I have no idea why. I have tried [self.view.layer removeAllAnimations]; before the changing the UILabel text and still no good. I even tried programmatically calling an IBAction and changing UILabel text from that block and it still messes up the animation. Nothing seems to work and it is definitely about timing as changing the UILabel text in viewDidLoad does work fine. I also checked if it is a Storyboard problem by changing another UILabel's text before the animation and the same problem occurs. If it helps at all here is the animation code:

    -(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];
}

In this case the UILabel text change causes "add","subtract","divide" and "multiply" (UIImageView's) to merge together instead of spreading as the animation is supposed to.

    -(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");
}
}

In this case it causes "var1" and "var2" (UILabel's) to not move upwards.

I understand its confusing, I've been searching everywhere and can't find a similar problem so this was kind of my last resort, if anyone has any solution or tips that would be greatly appreciated.

Foi útil?

Solução

If, by "messes up animation", you mean that it's going back to its starting location, then you probably have autolayout on. The autolayout constraints are automatically reapplied when you change the UILabel, and thus any animations that entail the changing of the frame will be thwarted. Bottom line, when using autolayout with constraints that dictate the layout, you should not be changing frame or center values manually.

That obviously leaves two possible remedies:

  1. Simplest, you could turn off auto layout. Then the setting of the label won't be triggering any constraints to be applied. See NSTimer blocks other animations for very similar issue.

  2. If you want to keep auto layout turned on, then you shouldn't be animating by changing the frame (or center) properties of controls, but rather you should create IBOutlet references for the appropriate constraints, and then animate them by changing the constant property. See Animating an image view to slide upwards

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top