如何在更改uiview的隐藏模式时添加动画?

时间:2011-05-30 14:13:54

标签: ios animation button uiview navigationbar

我想在更改隐藏模式时向视图添加动画,即

my_view.hidden=YES;

我在导航栏中添加了一个按钮。当我们点击它时,新视图被设置为取消隐藏。它绘制在导航表的上方。

12 个答案:

答案 0 :(得分:54)

将视图的不透明度从100%设置为0%。让动画完成回调将视图设置为隐藏。您可能还希望在回调期间将不透明度重置为100%,因此当您取消隐藏时,视图将显示完全不透明。

yourView.alpha = 0.0 //for zero opacity
yourView.alpha = 1.0 //for 100% opacity

答案 1 :(得分:30)

然而,没有动画隐藏;使用下面的Swift代码得到相同的结果:

UIView.animate(withDuration: 0.2, delay: 0, options: [], animations: {            
    self.yourView.alpha = 0 // Here you will get the animation you want
}, completion: { _ in  
    self.yourView.hidden = true // Here you hide it when animation done
})

答案 2 :(得分:29)

不幸的是,隐藏不是通过UIView动画可动画的属性。我认为你最好的选择可能是使用@Erik B建议的其中一个动画,或者开始涉足更强大的Core Animations。看一下UIView动画和核心动画的文档。

通过使用UIView动画从另一个视图下方滑动新视图,我实现了类似于您的建议。这使它看起来像一个抽屉滑出。如果你想做类似的事情,你需要拦截内部事件的触摸并将动画代码放在那里。

- (IBAction)buttonClicked:(id)sender {
    [UIView animateWithDuration:0.5
                          delay:0.0 
                        options:UIViewAnimationCurveEaseOut
                     animations:^(void) {
                        self.myView.frame = /* set the frame here */
                     } 
                     completion:NULL];
}

答案 3 :(得分:19)

我认为更合适的方法是:

[UIView transitionWithView:aView
                  duration:0.3
                   options:UIViewAnimationOptionTransitionCrossDissolve 
                animations:^(void){
                              aView.hidden = NO;
                           }
                completion:nil];

答案 4 :(得分:19)

已更新为 Swift 3

UIView.animate(withDuration: 0.2, delay: 0.2, options: .curveEaseOut,
      animations: {firstView.alpha = 0}, 
      completion: { _ in firstView.isHidden = true
        //Do anything else that depends on this animation ending
    })

如果您希望在第一个视图消失后回复某些内容,则可以使用alpha = 1hidden = false复制完成块中的代码。

答案 5 :(得分:18)

这是我写的一个类别,用于在UIView上引入一个新的“隐藏”属性,它正确支持动画:

@implementation UIView (AnimateHidden)

-(void)setHiddenAnimated:(BOOL)hide
{
  [UIView animateWithDuration:0.5
                        delay:0.0
                      options: UIViewAnimationCurveEaseOut
                   animations:^
                             {
                           [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
                           if (hide)
                             self.alpha=0;
                           else
                           {
                             self.hidden= NO;
                             self.alpha=1;
                           }
                         }
      completion:^(BOOL b)
      {
        if (hide)
          self.hidden= YES;
      }
  ];
}
@end

答案 6 :(得分:9)

这是更正N.J.版本:

@implementation UIView (AnimateHidden)

-(void)setHiddenAnimated:(BOOL)hide duration:(NSTimeInterval)duration {
    if(self.hidden == hide)
        return;
    if(hide)
        self.alpha = 1;
    else {
        self.alpha = 0;
        self.hidden = NO;
    }
    [UIView animateWithDuration:duration animations:^{
        if (hide)
            self.alpha = 0;
        else
            self.alpha = 1;
    } completion:^(BOOL finished) {
        if(finished)
            self.hidden = hide;
    }];
}

@end

答案 7 :(得分:6)

这是快速版本:

 UIView.animateWithDuration(0.5, delay: 0.2, options:
    UIViewAnimationOptions.CurveEaseOut, animations: {
            objView.alpha = 0
        }, completion: { finished in
            objView.hidden = true
  })

执行持续时间为5秒且延迟2秒后的动画。

可用的AnimationOptions是:

CurveEaseInOut, CurveEaseIn, CurveEaseOut, CurveLinear

答案 8 :(得分:3)

新泽西州和斯坦尼斯拉夫的答案帮助我为此制作了一个新的类别,我认为这会改善他们的答案,所以我想发布我提出的内容,以防其它人帮助。

请注意,它仅适用于iOS4或更高版本,因为它使用的是块。

<强>的UIView + AnimateHidden.m

#import "UIView+AnimateHidden.h"

@implementation UIView (AnimateHidden)

- (void)setHidden:(BOOL)hidden animated:(BOOL)animated
{
    // If the hidden value is already set, do nothing
    if (hidden == self.hidden) {
        return;
    }
    // If no animation requested, do the normal setHidden method
    else if (animated == NO) {
        [self setHidden:hidden];
        return;
    }
    else {
        // Store the view's current alpha value
        CGFloat origAlpha = self.alpha;

        // If we're unhiding the view, make it invisible initially
        if (hidden == NO) {
            self.alpha = 0;
        }

        // Unhide the view so we can see the animation
        self.hidden = NO;

        // Do the animation
        [UIView animateWithDuration:0.5
                              delay:0.0
                            options: UIViewAnimationOptionCurveEaseOut
                         animations:^{
            // Start animation block
            if (hidden == YES) {
                self.alpha = 0;
            }
            else {
                self.alpha = origAlpha;
            }
            // End animation block
        }
                        completion:^(BOOL b){
            // Start completion block
            // Finish up by hiding the view if necessary...
            self.hidden = hidden;
            // ... and putting back the correct alpha value
            self.alpha = origAlpha;
            // End completion block
        }];
    }
}

@end

答案 9 :(得分:3)

由于其中一些答案有点混乱,我想我可以发布这个API的简约设计。我还添加了延迟和持续时间 - 因为为什么不。

实施中我们有。

#import "UIView+AnimateHidden.h"

@implementation UIView (AnimateHidden)

- (void)setHiddenAnimated:(BOOL)hide
                    delay:(NSTimeInterval)delay
                 duration:(NSTimeInterval)duration {
    [UIView animateWithDuration:duration
                          delay:delay
                        options:UIViewAnimationOptionAllowAnimatedContent
                     animations:^{
                         if (hide) {
                             self.alpha = 0;
                         } else {
                             self.alpha = 0;
                             self.hidden = NO; // We need this to see the animation 0 -> 1
                             self.alpha = 1;
                         }
    } completion:^(BOOL finished) {
        self.hidden = hide;
    }];
}

@end

标题文件中,我们有。

#import <UIKit/UIKit.h>

@interface UIView (AnimateHidden)

- (void)setHiddenAnimated:(BOOL)hide
                    delay:(NSTimeInterval)delay
                 duration:(NSTimeInterval)duration;

@end

答案 10 :(得分:0)

如果您希望使用UIView不支持的更复杂的动画类型或动画

,则为另一个版本
- (void)setHidden:(BOOL)hidden withAnimationDuration:(NSTimeInterval)duration
{
    CATransition* transition = ({
        CATransition* its = [CATransition animation];
        its.duration = duration;
        its.timingFunction =
            [CAMediaTimingFunction
             functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
        its.type = kCATransitionPush;
        its.subtype = (hidden ? @"fromBottom" : @"fromTop");
        its
    });

    UIView* containerView = self.superview;
    [containerView.layer removeAllAnimations];
    [containerView.layer addAnimation: transition forKey: kCATransition];

    self.hidden = hidden;

    if (!hidden) {
        [self.superview bringSubviewToFront: self];
    }
}

答案 11 :(得分:0)

以下是我用来建模视图的代码&#34;增长&#34;和&#34;收缩&#34;在&#34;显示更多......&#34;并且&#34;显示更少......&#34;按钮单击。模仿Palyancodr的答案

这种方法允许我在故事板中创建两个视图,以便约束在不同的iOS设备上按预期工作,并且我不需要自定义所有约束的代码。

@IBAction func showMoreOrLessAction(_ sender: Any) {
    // if small view showing
    if showMoreLargeView.isHidden {
        showMoreSmallView.isHidden = true

        //showMoreLargeView.isHidden = false
        UIView.animate(withDuration: 0.2, delay: 0, options: [], animations: {
            self.showMoreLargeView.alpha = 1 // Here you will get the animation you want
        }, completion: { _ in
            self.showMoreLargeView.isHidden = false // Here you hide it when animation done
        })
    }
    else { // large view showing
        //showMoreSmallView.isHidden = false
        UIView.animate(withDuration: 0.2, delay: 0, options: [], animations: {
            self.showMoreSmallView.alpha = 1 // Here you will get the animation you want
        }, completion: { _ in
            self.showMoreSmallView.isHidden = false // Here you hide it when animation done
        })

        showMoreLargeView.isHidden = true
    }
}