2

I have a series of UIButtons which I use to update a couple of UILabels and initiate animation of an object from offscreen to onscreen. The progression goes like this. Button 1 & 2 are visible and when the user pushes a button a UILabel updates and a new UIImageView animates onto the screen. This works fine the first iteration, on round two, buttons 3 and 4 which were hidden, become visible. Once the user selects either button 3 or 4, another UIImageView should animate onto the screen (on top of the other images... "stacking" them). What is happening is the UIImageView which animated on after the user pressed either button 1 or 2, disappears, and nothing animates. This only occurs if the UILabels have to update based on the button selection. In the UILabel doesnt need to update, the animation works fine.

Relevent Code:

-(IBAction)didTapSizeButton:(id) sender {

if ([sender tag] == 1) {
    sizeLabel.text = @"12 oz";
}
else if ([sender tag] == 2) {
    sizeLabel.text = @"24 oz";
}

if (coasterTwo.center.x != 150) {

    [UIView animateWithDuration:0.35
                          delay:0
                        options:(UIViewAnimationOptionCurveEaseOut)
                     animations:^{
                         coasterTwo.center = CGPointMake(150, 220);

                     } completion:^(BOOL finished) {


                     }];

}

containerOne.hidden = NO;
containerTwo.hidden = NO;

}

-(IBAction)didTapContainerButton:(id)sender {

if ([sender tag] == 3) {
    containerLabel.text = @"Bottle";
}

else if ([sender tag] == 4) {
    containerLabel.text = @"Can";
}

if (coasterTwo.center.x == 150 && coasterThree.center.x != 150) {

    [UIView animateWithDuration:0.35
                          delay:0
                        options:(UIViewAnimationOptionCurveEaseOut)
                     animations:^{
                         CGPoint center = coasterThree.center;
                         center.x += 305;
                         coasterThree.center = center;

                     }
                     completion:^(BOOL finished){

                     }];

}


}

The UIImageViews are obviously reset to their starting positions as soon as I reach back to update the UILabel... Im just not sure how to prevent that.

5
  • Weird. You're not using a subclassed UIView or anything like that, are you? Why do you say that "UIImageViews are obviously reset to their starting positions"? There's nothing in the code above that would suggest that. Generally they won't be reset to starting positions unless you've got code somewhere that is resetting the position (frame, origin, or center), such as your own drawRect. If you have any code that does that (other than viewDidLoad) please share that.
    – Rob
    Jul 3, 2012 at 14:36
  • Very weird indeed... there is no other code in the .m file other than to handle button customization, and setting the .hidden and .tag properties on the buttons. with the exception of viewDidLoad and shouldAutorotateToInterfaceOrientation the entire .m is shown. Resetting the UIImageView location may not be the appropriate word, but it best represents the action that occurs when the UIlabel is updated when button 3 or 4 is press.
    – blueHula
    Jul 3, 2012 at 14:51
  • The problem doesn't rest here, though, I think.
    – Rob
    Jul 3, 2012 at 18:57
  • I was thinking something in IB was messed up, but i've double and triple checked to make sure everything is wired up correctly. There is nothing beyond template code in the AppDelegate and literally this and view did load are the only things in the view controller.
    – blueHula
    Jul 3, 2012 at 20:41
  • I've rewritten the methods and tried a number of options and this behavior expressly happens when I update the UILabel from the second set of buttons. No UILabel update? No problem, animation happens as it is supposed to. As soon as I update a label, all UIImageViews which had animated onto the screen disappear.
    – blueHula
    Jul 4, 2012 at 16:12

3 Answers 3

0

I would recommend using the static UIView commitAnimations method because you don't need the completion block:

[UIView beginAnimations:nil context:nil];
//Code for changing frames / alpha / anything else that is animatable
[UIView commitAnimations];

Try this out, see if it helps.

1
  • No luck there, still get the same behavior. The animation works fine if you comment out the if/else statements regarding the tags and just let the buttons function without updating the UILabels. Im thinking the problem is somewhere in there.
    – blueHula
    Jul 3, 2012 at 7:18
0

There's got to be something simple. As I said, the code for didTapContainerButton seems suspect (everytime you hit it coasterThree will be shifted another 305 points to the right), but there's nothing shown above that would cause coasterTwo to disappear. The only thing that would do that is if (a) some IB link is messed up (though it's not clear how that could result in images from disappearing when you hit button 3 or 4); (b) there's code you're not showing which might hide it; or (c) you've got some other control on your window that is hiding it (I've definitely seen situations where updating a label caused it to be brought to the front, obscuring something behind it ... you can diagnose that by making the label have a transparent background, explicitly move the image to the foreground, or try setting alphas to 0.5 so you can see through stuff while you debug).

To diagnose the first possibility, you'd have to upload your project to DropBox or something and we could take a look at it (if you're confortable doing that ... if not, make a copy of your project, get rid of anything confidential/proprietary, and then upload that).

To diagnose the second possibility, you could just update your question with the full code of the view controller's .m file.

To diagnose this third possibility, you could either make sure that before your animation of an image that you move it up front (before you animate coasterThree, do [self.view bringSubviewToFront:coasterThree];). You could also make sure that the backgroundColor of all of your controls (and if you have any container UIViews that you've added in IB, them, too) are transparent [UIColor clearColor]. You could also, just for diagnostic purposes, temporarily reduce the alpha so you can see through them (and thus if anything is blocking something else (you can do this in IB, click on the main UIView background, press command+A to select all of the controls, and change alpha to 0.5).

Bottom line, I'd pursue option 3, and if you don't have luck, either share your project or share a comprehensive code sample. If you don't feel comfortable doing that, I might suggest that you throw away your old NIB and rebuild it from scratch (in case there's something weird in the one you've got) or start building a whole new test project where you incrementally re-implement your UI and see if you can figure out where it went wrong.

But there's nothing inconsistent with UIImageViews animating and setting UILabel text value. Down below I have code with two buttons, two images that animate on and off, and a label that's updated, and it all works fine. (I had a crisis of confidence: I was starting to fear that there was some weird interaction with UILabels and animation.) Sigh.

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGPoint center;

    self.label.text = @"";

    // move image 1 (dog image) off screen to the left

    center = self.image1.center;
    center.x = -0.5 * self.view.frame.size.width;
    self.image1.center = center;
    self.image1.hidden = YES;

    // move image 2 (cat image) off screen to the right

    center = self.image2.center;
    center.x = 1.5 * self.view.frame.size.width;
    self.image2.center = center;
    self.image2.hidden = YES;

    // Do any additional setup after loading the view.
}

// if you hit button 1 (dog) button, change label, and animate the dog image on and cat image off

- (IBAction)button1:(id)sender 
{
    self.label.text = @"Dog";

    self.image1.hidden = NO;

    [UIView animateWithDuration:0.5 
                     animations:^{
                         CGPoint center;

                         // move image 1 (dog image) onscreen

                         center = self.image1.center;
                         center.x = 0.5 * self.view.frame.size.width;
                         self.image1.center = center;

                         // move image 2 (cat image) off screen to the right (if it's not there already)

                         center = self.image2.center;
                         center.x = 1.5 * self.view.frame.size.width;
                         self.image2.center = center;
                     }];
}

// if you hit button 2 (cat) button, change label, and animate the dog image off and cat image on

- (IBAction)button2:(id)sender 
{
    self.label.text = @"Cat";

    self.image2.hidden = NO;

    [UIView animateWithDuration:0.5 
                     animations:^{
                         CGPoint center;

                         // move image 1 (dog image) off screen to the left

                         center = self.image1.center;
                         center.x = -0.5 * self.view.frame.size.width;
                         self.image1.center = center;

                         // move image 2 (cat image) on screen

                         center = self.image2.center;
                         center.x = 0.5 * self.view.frame.size.width;
                         self.image2.center = center;
                     }];
}
0

Same thing just happened to one of my students. To prove you're not going crazy... try running it in iOS 5.x simulator ( and hopefully observe the desired behavior).

For iOS 6, you'll either have to code some state preservation for your UIView -- or -- if you haven't grown to love 'em and think you can't live without 'em -- get rid of the constraints in the xib file by unchecking "Uses Autolayout" in the file inspector.

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.