从下到上动画UIView高度

时间:2011-11-18 20:57:01

标签: ios animation uiview

我正在做一个UIView高度的简单动画,以便它显示出来。

默认情况下,它似乎从上到下显示,我希望它从下到上显示。

我把UIView固定在屏幕的底部。

我确信它很简单,我很遗憾.....任何提示?

由于

6 个答案:

答案 0 :(得分:10)

我真的认为实现这一目标的最简单方法是为视图的高度和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");
}];

我相信这会给出一个倒塌视图的外观和感觉。我没有在代码中尝试过这个,但我认为没有理由不这样做。

答案 1 :(得分:9)

隐藏在底部

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

答案 2 :(得分:5)

就像有骨头的狗我想出来了......

我没有设置框架高度的动画,而是将变换应用于视图并设置图层的锚点。

//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比例,则视图表现得很奇怪....在动画结束时我只是将其设置为隐藏。

在备份的过程中,我只使用身份转换(重置它)

hostView.transform = CGAffineTransformIdentity;

请注意,更改我的锚点会改变我的视图位置。有关setAnchorPoint方法的信息,请参阅此文章,该方法在设置anchorPoint

后规范化视图

Changing my CALayer's anchorPoint moves the view

答案 3 :(得分:2)

相反,您可以尝试将其放在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;
}];

这样,您不必将高度设置为0,并且它解决了视图中自动调整的任何问题。

答案 4 :(得分:2)

根据要求,这是我正在使用的代码......我正在使用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"];
}

答案 5 :(得分:0)

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