0
votes

I'm trying to animate two UIView together (an UIImageView and a UILabel) in three step:
0) both views are in the top left corner of the screen
1) then I change the frames (and for label the font size, too) so the views became bigger and on the center 2) I change again the frames (and for label the font size, too) to put both views on the botton right corner. (this aniamtion starts after some seconds)
3) I remove both views from superview.

The UIImageView is perfect after every animation. For UILabel, first animation works, second one doesn't ! After first animation my label immidiately became smaller and after the delay it moves like the image view. I don't know why! I use "setFrame" to change together origin and size..

My code is:

@implementation Animazione
{
  UIImageView * imageView;
  UILabel * message;
}
-(void) startAnimationOn: (UIView *) superView;
{
    UIImage * image = [UIImage imageNamed:@"..."];
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)];
    [imageView setImage:image];
    [superView imageView];
    CGRect startRect = [self startDimension]; //make a CGRect
    [imageView setFrame:startRect];


    message = [[UILabel alloc] init];
    message.text = @"A text";
    message.textAlignment = NSTextAlignmentCenter;
    message.adjustsFontSizeToFitWidth = YES;
    [self setLabelSize: startRect];

    [message setBackgroundColor:[UIColor greenColor]]; 

    [superView addSubview:message];


    [UIView animateWithDuration:2.0
                          delay:3.0
                        options:(UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionBeginFromCurrentState)
                     animations:^{
                         [UIView setAnimationDelegate:self];
                         [UIView setAnimationDidStopSelector:@selector(hideViews:finished:context:)];

                         CGRect big = [self bigDimension];
                         [imageViews setFrame:big];
                         //  [self setLabelSize:big];
                         [message setFrame:big];

                     }
                     completion:^(BOOL finished){
                         NSLog(@"grande");

                     }];

}


  -(void) hideViews:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
 {
    [UIView animateWithDuration:3.0
                          delay:5.0
                        options:(UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction)
                     animations:^{

                         [UIView setAnimationDelegate:self];
                         [UIView setAnimationDidStopSelector:@selector(deliteViews:finished:context:)];

                         CGRect finally = [self finallyDimension];
                         [imageView setFrame:finally];
                       //  [self setLabelSize:finally];
                         [message setFrame:finally];

                     }
                     completion:^(BOOL finished){
                         NSLog(@"scomparso");
                     }];

}
}

-(void) deliteViews:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    [imageView removeFromSuperview];
    [message removeFromSuperview];
    imageView = nil;
    message = nil;
}

}

- (void)setLabelSize: (CGRect) originalDim
{
[message setFrame: originalDim];
message.font = [message.font fontWithSize:originalDim.size.height/8+10];
}

Edit:

Actually I have not explained well what I expect from animations and what they do instead. For the first animation, I expect:
- nothing for 3 second (the delay)
- imageview and label grow and move at the same time (duration 2 second)
And it happens.

For the first animation, I expect:
- nothing for 5 second (the delay)
- imageview and label become smaller and move at the same time (duration 3 second)
What really happens:
- immediately the label change dimensions
- nothing for 5 second
- imageview becomes smaller and move, label move

1
not related, but my eyes cry due do all of that try/catch dance your are doing. If you want to catch every uncaught exception for logging purposes, just set an exception handler with NSSetUncaughtExceptionHandlerGabriele Petronella
When you say 'the first animation works but the second one doesn't' it isn't clear what you mean. Give more detail regarding exactly what happens (or doesn't).Will Jenkins
Thank you very much @Gabriele !!! I edit my question, I hope it is clearerChiara Gandolfi

1 Answers

1
votes

Try 1: Try to set those settings to your label :

[label setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[label setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];

It will avoid it to be compressed by the frame (but not being made bigger).

Try 2:

Try to use UITextView rather than UILabel, could be better cause UITextView use a contentView that maybe won't be resized immediatly.