我正在做一个UIView高度的简单动画,以便它逐渐显示出来。
默认情况下,它似乎是从顶部向底部逐渐显示出来的,而我希望它从底部向顶部逐渐显示出来。
我将UIView锚定在屏幕底部。
我相信我错过了一些简单的东西....... 有什么提示吗?
谢谢
我正在做一个UIView高度的简单动画,以便它逐渐显示出来。
默认情况下,它似乎是从顶部向底部逐渐显示出来的,而我希望它从底部向顶部逐渐显示出来。
我将UIView锚定在屏幕底部。
我相信我错过了一些简单的东西....... 有什么提示吗?
谢谢
我真的认为实现这个最简单的方法是同时动画化视图的高度和y属性。如果它们沿着相同的曲线发生,对用户来说应该看起来完全无缝。当您将高度动画化为0时,还要将y组件动画化为原始y + 原始高度。
UIView *view = ...;
float originalY = view.frame.origin.y;
float originalH = view.bounds.size.height;
[UIView animateWithDuration:1.2f delay:1.0f options:UIViewAnimationCurveEaseInOut animations:^{
view.frame = CGRectMake(view.frame.origin.x, (originalY + originalH), view.bounds.size.width, 0);
}completion:^(BOOL finished) {
NSLog(@"Animation is complete");
}];
我认为这样做会使视图实现折叠的外观和感觉。虽然我没有在代码中尝试过,但我认为它完全可以这样实现。
[self animateViewHeight:myView withAnimationType:kCATransitionFromBottom];
用于反向动画
[self animateViewHeight:myView withAnimationType:kCATransitionFromTop];
...
- (void)animateViewHeight:(UIView*)animateView withAnimationType:(NSString*)animType {
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionPush];
[animation setSubtype:animType];
[animation setDuration:0.5];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[animateView layer] addAnimation:animation forKey:kCATransition];
animateView.hidden = !animateView.hidden;
}
像一个拿着骨头不放的狗一样,我终于想通了...
不是通过动画来改变框架高度,而是通过对视图应用变换并设置层的锚点来实现。
//set the anchor point to the bottom of the view
[self setAnchorPoint:CGPointMake(0.5, 1.0) forView:hostView];
//Scale the height to close to zero
hostView.transform = CGAffineTransformMakeScale(1, 0.00001);
hostView.transform = CGAffineTransformIdentity;
相反,您可以尝试将其放在一个视图中,并使用 clipsToBounds = YES
,然后从底部向视图中间动画化,如下所示:
viewToAnimate.frame = CGRectMake(viewToAnimate.frame.origin.x,
viewToAnimate.superview.frame.size.height,
viewToAnimate.frame.size.width,
viewToAnimate.frame.size.height);
[UIView animateWithDuration:0.5 animations:^{
viewToAnimate.center = viewToAnimate.superview.center;
}];
根据您的要求,这是我正在使用的代码... 我正在使用CAKeyFrameAnimation,这可能比您要求的更复杂。 使用CABasicAnimation可能会产生相同的效果,我只是向您展示这段代码,因为我已经编写了它。
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
springLayer = [[CALayer alloc] init];
springLayer.backgroundColor = [UIColor redColor].CGColor;
springLayer.anchorPoint = CGPointMake(0, 1);
springLayer.frame = CGRectMake(125, 285, 100, 115);
[springLayer setNeedsDisplay];
[self.layer addSublayer:springLayer];
[self test];
}
return self;
}
-(void)test {
CAKeyframeAnimation *heightAnim = [[CAKeyframeAnimation alloc] init];
heightAnim.duration = 3;
heightAnim.removedOnCompletion = NO;
heightAnim.fillMode = kCAFillModeForwards;
heightAnim.beginTime = CACurrentMediaTime() + 0.25;
NSMutableArray *v = [[NSMutableArray alloc] init];
NSMutableArray *t = [[NSMutableArray alloc] init];
float dest = 250;
float difference = 135;
while (difference > 1.0) {
[v addObject:[NSNumber numberWithFloat:dest-difference]];
[t addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
difference *= 0.7;
[v addObject:[NSNumber numberWithFloat:dest+difference]];
[t addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
difference *= 0.7;
}
heightAnim.values = v;
heightAnim.timingFunctions = t;
[springLayer addAnimation:heightAnim forKey:@"bounds.size.height"];
}
我曾经用AdWhirlView的方式来实现,将其隐藏在屏幕下方,然后将其动画化上移;
AdWhirlView *adWhirlView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
adWhirlView.delegate = self;
adWhirlView.frame = CGRectMake(0, 430+kAdWhirlViewHeight, kAdWhirlViewWidth, kAdWhirlViewHeight);
[self.parentViewController.view insertSubview:adWhirlView belowSubview:self.view];
[UIView beginAnimations:@"AdWhirlIn" context:nil];
[UIView setAnimationDuration:.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
adWhirlView.frame = CGRectMake(0, 430, kAdWhirlViewWidth, kAdWhirlViewHeight);
[UIView commitAnimations];