2

Hi I have piece of code which only one line is not working...

[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    [label setFrame:CGRectMake(0, 0, frame.size.width, kStandardLabelHeight)]; //working
    [self.currentLabel setFrame:CGRectOffset(self.currentLabel.frame, frame.size.width, 0)]; //not working 
    [self.currentLabel setAlpha:0.0f]; //working
} completion:^(BOOL finished) {
    [self.currentLabel removeFromSuperview];
    self.currentLabel = label;
}];

I'm running out of ideas what is wrong...

ninja_iOS
  • 1,183
  • 1
  • 13
  • 20

4 Answers4

2

What I find out is: if I turn off "Use Auto Layout", then it magically working, so this issue is connected with auto layout.

After a search, I find this: http://weblog.invasivecode.com/post/42362079291/auto-layout-and-core-animation-auto-layout-was

Add constraint outlet mapping for your view, then instead of using setFrame, updating constraints will do the trick!

Below is my final implementation (_topConstraint is the top vertical space constraint of table view):

- (IBAction)switchButtonTouched:(id)sender
{
    if (_topConstraint.constant <= 0)
        _topConstraint.constant = _buttonView.frame.size.height;
    else
        _topConstraint.constant = 0;

    [_tableView setNeedsUpdateConstraints];
    [UIView animateWithDuration:0.5f animations:^(void){
        [_tableView layoutIfNeeded];
    } completion:^(BOOL finished){
    }];
}
chenyi1976
  • 1,104
  • 9
  • 17
0

Frame must be a CGRect with 4 parameters: (xPosition,yPosition,width,height). Use CGRectMake for set frame

self.currentLabel.frame = CGRectMake(100, 100, self.currentLabel.frame.size.width, self.currentLabel.frame.size.height)

You set 0.3 sec the animation duration, and when it s end you remove it from the view. This is too short. set the duration for example 2, and you will see that your label is moving .

Magyar Miklós
  • 4,182
  • 2
  • 24
  • 42
  • self.currentLabel.frame = CGRectMake(100, 100, self.currentLabel.frame.size.width, self.currentLabel.frame.size.height) – Magyar Miklós Sep 22 '13 at 17:38
  • You set 0.3 sec the animation duration, and when it s end you remove it from the view. This is too short. set the duration for example 2, and you will see that your label is moving . – Magyar Miklós Sep 22 '13 at 17:51
0

Sometimes you should set frames for animating views also after animation, for example in completion block, try something like that

[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    [label setFrame:CGRectMake(0, 0, frame.size.width, kStandardLabelHeight)]; //working
    [self.currentLabel setFrame:CGRectOffset(self.currentLabel.frame, frame.size.width, 0)]; //not working 
    [self.currentLabel setAlpha:0.0f]; //working
} completion:^(BOOL finished) {
    self.frame = currentLabelFrame;
    label.frame = label.frame;
    [self.currentLabel removeFromSuperview];
    self.currentLabel = label;
}];
Anton
  • 139
  • 5
-1

If your issue is neither Autolayout related, or the others listed here, there is also another possible problem that turned out to be my issue. I'm simultaneously running a UIView transition (transitionFromView:toView:) on views that take up the same screen space (Different superviews, not related, other than positioning in the superview of the overarching view controller).

My code looked like this:

[UIView transitionFromView:self.someView
                    toView:self.someOtherView
                  duration:0.6f 
                   options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionShowHideTransitionViews 
                completion:NULL];

[UIView animateWithDuration:0.3
                      delay:0.0 options:UIViewAnimationOptionCurveEaseOut 
                  animations:^{
                        [self.someThirdView setFrame:someFrame]; //not working 
                        [self.someThirdView setAlpha:1.0f]; //working
                } completion:nil];

The frame changed, but it popped to the new frame rather than animating. I'm guessing this is an internal iOS issue. As soon as I removed the "transitionFromView:..." call, the frame animation worked fine.

My solution for now has been to move the second animation into the completion block of the transitionFromView. It's not perfect, as the two animations should have lined up, but it is a passable solution for now.

Matt Foley
  • 921
  • 1
  • 8
  • 24