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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6177393/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:09:20  来源:igfitidea点击:

How to add animation while changing the hidden mode of a uiview?

iosanimationbuttonuiviewnavigationbar

提问by Sanchit Paurush

I want to add animation to a view while changing its hidden mode i.e

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

my_view.hidden=YES;

I have added a button in navigationbar. When we click on it the new view is set to be unhide. It draws at the upper of the navigation table.

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

采纳答案by Eytan

Unfortunately, hidden is not a property that is animatable through UIView animations. I think your best bet may be to use one of the animations @Erik B suggested, or start dabbling with Core Animations which are much more powerful. Take a glance at the documentation for UIView animations and Core Animations.

不幸的是, hidden 不是可以通过 UIView 动画设置动画的属性。我认为你最好的选择可能是使用 @Erik B 建议的动画之一,或者开始涉足更强大的核心动画。浏览一下 UIView 动画和核心动画的文档。

I achieved something like what your suggesting by using UIView animations to slide the new view from below another view. This made it appear like a drawer sliding out. If you want to do something like that, you need to intercept the touch up inside event and place the animation code there.

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

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

回答by Jeremy W. Sherman

Animate the view's opacity from 100% to 0%. Have the animation completion callback set the view to be hidden. You might also want to reset the opacity back to 100% during the callback, so the view will display fully opaque when you unhide it.

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

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

回答by Palyancodr

There is no animation for hiding however; you get the same result with the Swift code below:

但是没有隐藏动画;使用下面的 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
})

回答by Abhi

I think more appropriate way to do it is:

我认为更合适的方法是:

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

回答by Paula Hasstenteufel

Updated to Swift 3:

更新到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
    })

And if you wish to animate something back after first view is gone, you can replicate the code inside the completion block with alpha = 1and hidden = false.

如果您希望在第一个视图消失后重新设置动画,您可以使用alpha = 1和复制完成块内的代码hidden = false

回答by N.J.

Here's a category I wrote to introduce a new "hidden" property on UIView which correctly supports animation:

这是我写的一个类别,用于在 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

回答by Stan

This is corrected N.J. version:

这是更正的新泽西州版本:

@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

回答by Jayprakash Dubey

Here is swift version for this :

这是快速版本:

Swift 2

斯威夫特 2

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

Swift 3, 4, 5

斯威夫特 3、4、5

UIView.animate(withDuration: 0.5, delay: 0.2, options: UIView.AnimationOptions.curveEaseOut, animations: {
    objView.alpha = 0
}, completion: { finished in
    objView.isHidden = true
})

This performs animation with duration of 5 seconds and after delay of 2 seconds.

这会在 5 秒的持续时间和 2 秒的延迟后执行动画。

Available AnimationOptions are :

可用的动画选项是:

CurveEaseInOut, CurveEaseIn, CurveEaseOut, CurveLinear

回答by Entalpi

Since a few of these answers are a bit cluttered I figured I could post my minimalistic design of this API. I also added the delay and duration - because why not.

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

In the implementationwe have.

实现中,我们有。

#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

In the headerfile we have.

在我们的文件中。

#import <UIKit/UIKit.h>

@interface UIView (AnimateHidden)

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

@end

回答by Craig Brown

NJ's and Stanislav's answers here helped me make a new category for this, which I think improves on their answers, so thought I'd post what I came up with in case it helps anyone else.

新泽西州和斯坦尼斯拉夫在这里的答案帮助我为此创建了一个新类别,我认为这改进了他们的答案,所以我想我会发布我的想法,以防它对其他人有所帮助。

Note it will only work in iOS4 or later as it's using blocks.

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

UIView+AnimateHidden.m

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