动画启用 UIBarButtonItem?

发布于 2024-08-11 18:13:37 字数 369 浏览 2 评论 0原文

有没有办法以动画方式启用或禁用按钮?我尝试了以下方法但没有成功。我猜测此时启用的属性无法像不透明度那样进行动画处理 - 但我希望我是错的。

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0f];
    theButton.enabled = YES;
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];

我不敢相信没有 setEnabled:(BOOL)enabledAnimated:(BOOL)animated 方法。

Is there a way to animate enabling or disabling a button? I've tried the following with no success. I'm guessing at this point that the enabled property cannot be animated like opacity can – but I hope I'm wrong.

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0f];
    theButton.enabled = YES;
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];

I can't believe there isn't a setEnabled:(BOOL)enabled animated:(BOOL)animated method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如梦初醒的夏天 2024-08-18 18:13:37

这实际上非常简单。 UIView.transitionWithView 可以拍摄任何视图的快照,并从快照交叉溶解到视图的新状态。您可以使用该功能来动画化许多更改,包括enabled

只需使用视图控制器的视图作为目标视图,指定 TransitionCrossDissolve,然后像平常一样进行动画处理。

UIView.transitionWithView(self.view,
    duration: 0.5,
    options: UIViewAnimationOptions.TransitionCrossDissolve,
    animations: { () -> Void in
        self.someButton.enabled = !self.someButton.enabled
}, completion:nil)

This is actually really easy. UIView.transitionWithView can take a snapshot of any view and cross-dissolve from the snapshot to the new state of the view. You can use that capability to animate a lot of changes, including enabled.

Just use the View Controller's view as the target view, specify TransitionCrossDissolve, then animate as you would normally.

UIView.transitionWithView(self.view,
    duration: 0.5,
    options: UIViewAnimationOptions.TransitionCrossDissolve,
    animations: { () -> Void in
        self.someButton.enabled = !self.someButton.enabled
}, completion:nil)
梦在深巷 2024-08-18 18:13:37

可动画化的属性(通过 UIView beginAnimations:context:)有:frame、bounds、center、transform、alpha(来自 Apple 文档)。

因此,您必须确定您的动画到底是什么(就颜色/alpha/变换动画而言)并创建手动动画(也许您必须使用 CAAnimation 而不是 UIView beginAnimations:context:)

Animatable properties (through UIView beginAnimations:context:) are: frame, bounds, center, transform, alpha (from Apple docs).

So you've to determine what exactly your animation is (in terms of color/alpha/transform animations) and create manual animation (maybe you'll have to use CAAnimation instead of UIView beginAnimations:context:)

回心转意 2024-08-18 18:13:37

您可以将按钮的启用/禁用分解为两个重叠图像之间的不透明度变化,每个图像处于一种状态或另一种状态(即使其看起来像淡入/淡出)。在动画完成处理程序中,您可以执行实际的启用/禁用切换。

You can break down the enable/disable of a button into a change in opacity between two overlapping images each in one state or the other (i.e. make it look like a fade-in/fade-out). In the animation completion handler you can to do the actual enable/disable toggle.

寂寞花火° 2024-08-18 18:13:37

这是一个想法:)

在动画块中,从视图中删除按钮并以禁用状态再次添加它。您可以在这里指定您想要哪种动画。

Here's an idea :)

Within your animation block, Remove the button from view and add it again with disabled state. You can specify what kinda animation you want here..

弥枳 2024-08-18 18:13:37

设置:self.buttonSetEvent.customView.alpha = 0.2;

并尝试这个

    CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.15];
[UIView setAnimationRepeatCount:2];
[UIView setAnimationRepeatAutoreverses:YES];

self.buttonSetEvent.customView.alpha = 1;

[UIView setAnimationDelegate:self];
[UIView commitAnimations];

Set: self.buttonSetEvent.customView.alpha = 0.2;

and try this

    CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.15];
[UIView setAnimationRepeatCount:2];
[UIView setAnimationRepeatAutoreverses:YES];

self.buttonSetEvent.customView.alpha = 1;

[UIView setAnimationDelegate:self];
[UIView commitAnimations];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文