UIView隐藏/显示动画 [英] UIView Hide/Show with animation

查看:627
本文介绍了UIView隐藏/显示动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的简单目标是淡化动画隐藏和显示功能。

My simple goal is to fade animate hiding and showing functions.

Button.hidden = YES;

足够简单。但是,是否有可能让它淡出而不仅仅是消失?它看起来相当不专业。

Simple enough. However, is it possible to have it fade out rather than just disappearing? It looks rather unprofessional that way.

推荐答案

在iOS 4及更高版本中,有一种方法可以使用UIView转换方法来实现这一点无需导入QuartzCore。你可以这样说:

In iOS 4 and later, there's a way to do this just using the UIView transition method without needing to import QuartzCore. You can just say:

[UIView transitionWithView:button
                  duration:0.4
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                     button.hidden = YES;
                }
                completion:NULL];



上一个解决方案



Michail的解决方案将起作用但它实际上并不是最好的方法。

Previous Solution

Michail's solution will work, but it's not actually the best approach.

alpha衰落的问题在于,有时不同的重叠视图层在淡出时看起来很奇怪。还有一些使用Core Animation的替代方案。首先在您的应用程序中包含QuartzCore框架,并将 #import< QuartzCore / QuartzCore.h> 添加到您的标头中。现在您可以执行以下操作之一:

The problem with alpha fading is that sometimes the different overlapping view layers look weird as they fade out. There are some other alternatives using Core Animation. First include the QuartzCore framework in your app and add #import <QuartzCore/QuartzCore.h> to your header. Now you can do one of the following:

1)设置 button.layer.shouldRasterize = YES; 然后使用Michail在他的回答中提供的alpha动画代码。这样可以防止图层奇怪地混合,但会有轻微的性能损失,并且如果按钮在像素边界上没有精确对齐,可能会使按钮看起来模糊。

1) set button.layer.shouldRasterize = YES; and then use the alpha animation code that Michail provided in his answer. This will prevent the layers from blending weirdly, but has a slight performance penalty, and can make the button look blurry if it's not aligned exactly on a pixel boundary.

或者:

2)使用以下代码为fade设置动画:

2) Use the following code to animate the fade instead:

CATransition *animation = [CATransition animation];
animation.type = kCATransitionFade;
animation.duration = 0.4;
[button.layer addAnimation:animation forKey:nil];

button.hidden = YES;

这种方法的优点是你可以交叉淡出按钮的任何属性,即使它们不是动画(例如按钮的文字或图像),只需设置过渡,然后立即设置你的属性。

The nice thing about this approach is you can crossfade any property of the button even if they aren't animatable (e.g. the text or image of the button), just set up the transition and then set your properties immediately afterwards.

这篇关于UIView隐藏/显示动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆