从下到上animationUIView高度

我正在做UIView高度的一个简单的animation,以便它揭示。

默认情况下,它似乎是从上到下揭示,我希望它揭示自下而上。

我有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]; 

为反向animation

 [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; } 

就像一只带有骨头的狗,我想到了这一点….

而不是animation帧高度,而是应用转换到视图,并设置图层的锚点。

 //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); 

如果我把0作为y比例,视图performance怪异….在animation结束时,我只是将其设置为隐藏。

在备份的路上,我只是使用身份变换(重置它)

 hostView.transform = CGAffineTransformIdentity; 

请注意,改变我的定位点改变了我的观点。 看到这个post的setAnchorPoint方法,设置anchorPoint后,规范视图

改变我的CALayer的anchorPoint移动视图

相反,你可以尝试把它放在视图中,使用clipsToBounds = YES ,然后从视图的底部到中间对它进行animation处理,如下所示:

 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; }]; 

这样,您不必将高度设置为0,就可以解决在视图中进行自动调整的任何问题。

按照要求,这是我正在使用的代码…我正在使用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做的一个方法,把它隐藏在屏幕下面,然后animation;

 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];