【问题标题】:Setting an animation to happen only at launch?将动画设置为仅在启动时发生?
【发布时间】:2013-05-26 01:40:14
【问题描述】:

我有一个问题希望你们能帮助我。所以几乎我有一个基本动画,我希望它只在应用程序启动时出现,而不是在我进入应用程序后实际返回页面时出现,因为现在我在 viewWillAppear 方法中拥有它。下面是我的动画代码。任何帮助将不胜感激。

正如您在查看我的代码时可能会说的那样,我正在尝试淡入图像,如果您知道更好的方法,您可以告诉我吗?但这很好用。

我的.m:

#import "HomeViewController.h"
@interface HomeViewController ()
@property (nonatomic,weak) IBOutlet UIImageView *logo;
@end
@implementation HomeViewController

- (void)viewDidAppear:(BOOL)animated
{
    [[self logo]setAlpha:0];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:2];
    [UIView setAnimationDelay:0];
    [[self logo]setAlpha:1];
    [UIView commitAnimations];
}

【问题讨论】:

  • 放在viewDidLoad中怎么样?
  • 或使用dispatch_once。或者有一些BOOL 属性(或static)在你做动画后设置,并且只根据BOOL 属性的值再次做那个动画。

标签: uiimageview uiimage iboutlet viewdidappear


【解决方案1】:

为什么不使用NSUserDefaults

你可以这样做:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

if (![userDefaults boolForKey:@"didPlayAnimation"])
{        
    [[self logo]setAlpha:0];

    [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
         [[self logo]setAlpha:1];
    } completion:^(BOOL finished) {
        [userDefaults setBool:YES forKey:@"didPlayAnimation"];
    }]
}

我建议您使用上面代码中看到的基于块的动画,因为您使用的开始/提交动画方法在 iOS 4.0 及更高版本上不鼓励使用(请参阅UIView class reference)。

由于您希望每次启动应用程序时都执行此操作,因此请在应用程序终止时将该 BOOL 设置为 NO。你可以在方法中做到这一点

- (void)applicationWillTerminate:(UIApplication *)application

您的应用委托,即实现 UIApplicationDelegate 协议的类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 2014-01-09
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    相关资源
    最近更新 更多