从上到下动画UIview,反之亦然

时间:2015-12-23 07:42:29

标签: ios objective-c uiview uibutton

我有一个UIView位于X = 0和Y = 0位置,高度为110,UIButton位于屏幕底部,高度为50,我想通过动画显示两者但是UIView必须从顶部到它的高度动画,UIButton从底部到它的高度,我是全新的,请帮我怎么做。

4 个答案:

答案 0 :(得分:1)

可能符合您要求的基本动画

[UIView transitionWithView:view
                  duration:0.5
                   options:UIViewAnimationOptionTransitionNone
                animations:^{
                   //update your frame here;
                }
                completion:nil];

请通过Apple Documentationstack overflow

答案 1 :(得分:1)

以下代码让我在顶部显示搜索视图。

if (!isSearchActive) {
    [UIView animateWithDuration:0.4
                          delay:0.1
                        options: UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         searchView.frame   =   CGRectMake(0, 56, mainWidth, 50);
                     }
                     completion:^(BOOL finished){
                     }];
    isSearchActive=YES;
}
else{
    [UIView animateWithDuration:0.3
                          delay:0.1
                        options: UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         searchView.frame   =   CGRectMake(0, 56, mainWidth, 0);
                     }
                     completion:^(BOOL finished){
                     }];
    isSearchActive=NO;
}

点击任意按钮调用此代码即可显示和隐藏视图。

答案 2 :(得分:1)

尝试从上到下动画:

- (CAAnimation*)movedown;
{
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"position.y";
    animation.fromValue = @600;
    animation.toValue = @50;
    animation.duration = 0.25;

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];


    [trashView.layer addAnimation:animation forKey:@"basic"];
    trashView.layer.position =CGPointMake(0,-20);
    return animation;
}

和从下到上的代码:

- (CAAnimation*)moveup;
{
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"position.y";
    animation.fromValue = @-500;
    animation.toValue = @10;
    animation.duration = 0.25;

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];


    [trashView.layer addAnimation:animation forKey:@"basic"];
    trashView.layer.position =CGPointMake(0,20);
    return animation;

}

您可以使用第一段代码来制作视图动画,但必须在代码中替换viewcontroller trashview

和第二个块用于按钮,但您必须在trashview更换按钮。

但你可以根据需要设定动画的价值。

答案 3 :(得分:0)

   private func startAnimation() {
            let animation = CABasicAnimation(keyPath: "position.y")
            animation.fromValue = 10  //Top Y 
            animation.toValue = 204  //Bottom Y
            animation.duration = 15 * 0.10 //duration * speed
            animation.repeatCount = 100
            animation.autoreverses = false
            animation.isRemovedOnCompletion = true
            animation.fillMode = .forwards
            self.yourView.layer.add(animation, forKey: "position.y")
        }

  private func stopAnimation() {
        self.yourView.layer.removeAllAnimations()
    }