ios 从下到上动画 UIView 高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8189027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Animate UIView height from bottom to top
提问by bandejapaisa
I'm doing a simple animation of UIView height so that it reveals.
我正在做一个 UIView 高度的简单动画,以便它显示出来。
By default it seems to be revealing from top to bottom, and I want it to reveal bottom to top.
默认情况下,它似乎从上到下显示,我希望它从下到上显示。
I have the UIView anchored to the bottom of the screen.
我将 UIView 锚定到屏幕底部。
I'm sure it something simple i'm missing..... any tips?
我确定这是我遗漏的一些简单的东西..... 有什么提示吗?
Thanks
谢谢
采纳答案by bandejapaisa
Like a dog with a bone I figured this out....
就像一条有骨头的狗,我发现了这一点......
Instead of animating the frame height, I applied a transform to the view and set the anchor point of the layer.
我没有对帧高度进行动画处理,而是对视图应用了变换并设置了图层的锚点。
//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);
If I put 0 as the y scale, the view behaves weird.... at the end of the animation i just set it to hidden.
如果我将 0 作为 y 比例,则视图表现得很奇怪……在动画结束时我只是将其设置为隐藏。
On the way back up I just use the Identity Transform (reset it)
在备份的路上我只使用身份转换(重置它)
hostView.transform = CGAffineTransformIdentity;
Note that changing my anchor point shifted the position of my view. See this post for the setAnchorPoint method which normalises the view after setting the anchorPoint
请注意,更改我的锚点会改变我的视图位置。请参阅此帖子了解 setAnchorPoint 方法,该方法在设置 anchorPoint 后规范化视图
回答by atreat
I really think the simplest way to accomplish this would be to animate BOTH the height and the y properties of the view. If they happen along the same curve, it should look completely seamless to the user. As you are animating the height to 0, also animate the y component to the original y + the original height.
我真的认为实现这一点的最简单方法是为视图的高度和 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");
}];
I believe this would give the look and feel of a collapsing view. I haven't tried this out in code, but I see no reason why it wouldn't be possible like this.
我相信这会给人一种折叠视图的外观和感觉。我没有在代码中尝试过这个,但我看不出为什么它不可能像这样。
回答by Warif Akhand Rishi
hide under bottom
隐藏在底部
[self animateViewHeight:myView withAnimationType:kCATransitionFromBottom];
for reverse 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;
}
回答by aleph_null
As requested, this is the code that I'm using... I'm using a CAKeyFrameAnimation, which may be a bit more than what you're looking for. It would probably work the same with a CABasicAnimation, I'm just showing you this code because I already have it written.
根据要求,这是我正在使用的代码......我正在使用 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"];
}
回答by aopsfan
Instead you could try putting it in a view with clipsToBounds = YES
and then animate it from the bottom to the middle of the view, like so:
相反,您可以尝试将它放在一个视图中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;
}];
This way, you don't have to set the height to 0, and it solves any problems with autoresizing within the view.
这样,您不必将高度设置为 0,它解决了视图中自动调整大小的任何问题。
回答by timbroder
one way I've done it with an AdWhirlView, hide it below the screen, then animate it up;
我使用 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];