ios - How to animate a UIView that has constraints? -
i have 2 uiviews
animate on screen , off. both should start positioned off screen. top animates downward, bottom upward. without constraints works expected. with, uiviews
visible @ app launch (with slight animation visible). animate off screen though.
for top uiview
, have leading space, trailing space, top space, , height.
for bottom uiview
, have leading space, trailing space, bottom space, , height.
how should adjust code and/or constraints desired animation?
here code:
- (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. } - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; [self animateviewsin]; } - (void)viewwilllayoutsubviews { [super viewwilllayoutsubviews]; self.topview.frame = cgrectmake(16.0, -self.topview.frame.size.height, self.topview.frame.size.width, self.topview.frame.size.height); self.bottomview.frame = cgrectmake(16.0, self.view.bounds.size.height, self.bottomview.frame.size.width, self.bottomview.frame.size.height); } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (void)animateviewsin { [uiview animatewithduration:0.5 delay:1.0 options: uiviewanimationoptioncurveeasein animations:^{ self.topview.frame = cgrectmake(16.0, self.topview.frame.size.height/8.0, self.topview.frame.size.width, self.topview.frame.size.height); self.bottomview.frame = cgrectmake(16.0, (self.view.bounds.size.height-self.bottomview.frame.size.height)-20.0, self.bottomview.frame.size.width, self.bottomview.frame.size.height); } completion:^(bool finished){ nslog(@"done!"); }]; } - (ibaction)animateviewsout:(id)sender { cgrect baskettopframe = self.topview.frame; baskettopframe.origin.y = -baskettopframe.size.height; cgrect basketbottomframe = self.bottomview.frame; basketbottomframe.origin.y = self.view.bounds.size.height; [uiview animatewithduration:0.5 delay:1.0 options: uiviewanimationoptioncurveeaseout animations:^{ [self.view layoutifneeded]; self.topview.frame = baskettopframe; self.bottomview.frame = basketbottomframe; } completion:^(bool finished){ nslog(@"done!"); }]; }
when using constraints, should not adjusting frames. should modifying constant
property of constraint (or deactivinging 1 constraint , activating another).
here's example of animating top view in top, assuming added @iboutlet
called topconstraint
top constraint of view topview
:
in objective-c:
- (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; self.topconstraint.constant = -self.topview.frame.size.height; // or whatever want [self.view layoutifneeded]; [uiview animatewithduration:0.5 delay:1 options:0 animations:^{ self.topconstraint.constant = 0; [self.topview layoutifneeded]; } completion:nil]; }
in swift:
override func viewdidappear(_ animated: bool) { topconstraint.constant = -topview.frame.height // or whatever want view.layoutifneeded() uiview.animate(withduration: 0.5, delay: 1, animations: { topconstraint.constant = 0 self.view.layoutifneeded() }) }
Comments
Post a Comment