ios - 我可以为 View 设置动画以从隐藏中淡入吗?

标签 ios objective-c animation

我正在尝试查看 scrim初始 View 加载后 5 秒后淡入 View Controller 中的所有内容。这就是我现在所拥有的。

- (void)viewDidLoad {


[super viewDidLoad];
// Do any additional setup after loading the view.

[self setupAlarmPoll];
[self setupTimeLabel];
self.shouldRefresh = YES;
[self refreshSpotifyToken];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveNotification:)
                                             name:@"backHome"
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveNotification:)
                                             name:@"applicationDidEnterBackground"
                                           object:nil];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{

    [UIView animateWithDuration:2.00 animations:^{

       self.scrim.hidden = NO;
    }];



});

但是,我没有逐渐淡入。 View 刚刚出现。我怎样才能在objective-c中完成这个动画?

最佳答案

hidden View 的属性是不可动画的。 alpha然而,动画。您需要:

  • 将 alpha 设置为零
  • 将隐藏设置为假。
  • 将 alpha 设置为 1.0

  • 通过这样做,您可以使 View 不再隐藏,而是完全透明。您将 alpha 属性从 0.0 设置为 1.0,使 View 根据需要淡入。

    代码如下所示:
    self.scrim.alpha = 0;
    self.scrim.hidden = NO;
    [UIView animateWithDuration:2.00 
      delay: 5.0
      options: 0
      animations:^{
       self.scrim.alpha = 1.0;
      }
      completion: nil
    ];
    

    (请注意,长格式 animateWithDuration 采用延迟参数,因此您不必将其包装在对 dispatch_after 的调用中。)

    另外,您为什么要在 Objective-C 中进行新的开发?

    关于ios - 我可以为 View 设置动画以从隐藏中淡入吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45472296/

    相关文章:

    ios - 以编程方式显示 iPhone 键盘

    ios - 图像 map 链接在 iOS 设备上不起作用

    iOS Web View 打开多个链接经常丢失请求

    iphone - 在 iOS 中检查互联网连接的最简单方法是什么?

    ios - 在定时器中运行 UIView.animate

    ios - 我如何设置 UIDatePicker 以在 swift 当前日期后一天显示?

    objective-c - 如果语句出错-xcode

    objective-c - XCode/Cocoa Mac 更改 View

    CSS3 动画淡出

    c# - 微妙的 WP7 枢轴 ListViewItem 动画