Animating a UIView to move down, then slide up

I'm trying to understand why this is not working

in my tableViewcontroller viewDidLoad

self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320,0)]; self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 0)]; self.headerLabel.textAlignment = NSTextAlignmentCenter; self.headerLabel.text = @"text"; [self.view addSubview:self.headerView]; [self.headerView addSubview:self.headerLabel]; [UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ self.headerLabel.frame = CGRectMake(0, 5, 320,15); self.headerView.frame = CGRectMake(0, 5, 320,15); } completion:^(BOOL finished) { [UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ self.headerLabel.frame = CGRectMake(0, 5, 320,0); self.headerView.frame = CGRectMake(0, 5, 320,0); } completion:^(BOOL finished) { }]; }]; 

if I delete the backup portion of the slide in the completion block of the first animation call. It works. View slides down. However, I can’t get it fully compressed. When I include the slide code in the completion block, the view does not appear at all at startup, and I don’t know why and Im going insane

+7
ios objective-c animation slider
source share
2 answers

I'm not sure why the shortcut disappears, but you can fix it by providing a view and name the appropriate height when creating them and only animate the position of the shortcut y, not its height.

 - (void)viewDidLoad { [super viewDidLoad]; self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -30, 320,30)]; self.headerView.backgroundColor = [UIColor yellowColor]; self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 21)]; self.headerLabel.textAlignment = NSTextAlignmentCenter; self.headerLabel.text = @"text"; [self.view addSubview:self.headerView]; [self.headerView addSubview:self.headerLabel]; [UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ self.headerView.frame = CGRectMake(0, 0, 320,30); } completion:^(BOOL finished) { [UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ self.headerView.frame = CGRectMake(0, -30, 320,30); } completion:^(BOOL finished) { }]; }]; } 
+15
source share

viewDidLoad is not the best place to run animations. You must move the code to viewWillAppear :, and if you want this to happen for the first time when presenting the view, you must add the BOOL property to your controller (i.e. self.hasperformedInitialHeaderAnimation), therefore:

 -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; if(!self.hasPerformedInitialHeaderAnimation){ self.hasPerformedInitalHeaderAnimation = YES; [UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ self.headerLabel.frame = CGRectMake(0, 5, 320,15); self.headerView.frame = CGRectMake(0, 5, 320,15); } completion:^(BOOL finished) { [UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ self.headerLabel.frame = CGRectMake(0, 5, 320,0); self.headerView.frame = CGRectMake(0, 5, 320,0); } completion:^(BOOL finished) { }]; } 
+4
source share

All Articles