I'm updating an old app with an AdBannerView
and when there is no ad, it slides off screen. When there is an ad it slides on screen. Basic stuff.
Old style, I set the frame in an animation block.
New style, I have a IBOutlet
to the constraint which determines the Y position, in this case it's distance from the bottom of the superview, and modify the constant.
- (void)moveBannerOffScreen {
[UIView animateWithDuration:5
animations:^{
_addBannerDistanceFromBottomConstraint.constant = -32;
}];
bannerIsVisible = FALSE;
}
- (void)moveBannerOnScreen {
[UIView animateWithDuration:5
animations:^{
_addBannerDistanceFromBottomConstraint.constant = 0;
}];
bannerIsVisible = TRUE;
}
And the banner moves, exactly as expected, but no animation.
UPDATE: I re-watched WWDC12 video "Best Practices for Mastering Auto Layout" which covers animation. It discusses how to update constraints using CoreAnimation
.
I've tried with the following code, but get the exact same results.
- (void)moveBannerOffScreen {
_addBannerDistanceFromBottomConstraint.constant = -32;
[UIView animateWithDuration:2
animations:^{
[self.view setNeedsLayout];
}];
bannerIsVisible = FALSE;
}
- (void)moveBannerOnScreen {
_addBannerDistanceFromBottomConstraint.constant = 0;
[UIView animateWithDuration:2
animations:^{
[self.view setNeedsLayout];
}];
bannerIsVisible = TRUE;
}
On a side note, I have checked numerous times and this is being executed on the main thread.
转载于:https://stackoverflow.com/questions/12622424/how-do-i-animate-constraint-changes