如何将 alpha 值从 1 变为 0 的视图进行动画处理、隐藏视图、将视图从 0 变为 1 进行动画处理,然后取消隐藏视图?

How can I animate a view from alpha value 1 to 0, hide the view, animate the view from 0 to 1, then unhide the view?

我有一个带有亮度和其他控件的闹钟屏幕。这是我试图用它完成的事情:

我有一个黑色 scrim 覆盖屏幕上的所有元素,在 5 秒无用户交互后,我将其设置为从 alpha 1 到 0 的动画。当轻触稀松布时,它应该从 alpha 0 变为 1 并隐藏,这样我就可以与其下方的控件进行交互。然后在 5 秒不活动后,稀松布应该再次从 0 变为 1。稀松布应在用户点击时隐藏和取消隐藏。

现在我正在使用 animateWithDuration 让所有动画正常工作,但是在每个动画完成到 alpha 0 时,稀松布不会隐藏,所以我无法访问下面的控件。当 scrim 的 alpha 为 0 时,如何以不妨碍屏幕控件的方式完成这些动画?

这是我的代码:

- (void)viewDidLoad {

    [super viewDidLoad];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]        initWithTarget:self action:@selector(hideScrim)];

    tapGesture.cancelsTouchesInView = NO;

    [self.scrim addGestureRecognizer:tapGesture];
}

-(void)viewDidAppear:(BOOL)animated {

    [self showScrim];
}

-(void)showScrim {

    self.scrim.alpha = 0.0;
    self.scrim.hidden = NO;

    [UIView animateWithDuration:0.50 delay:5.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.scrim.alpha = 1.0;
    } completion:^(BOOL finished) {

    }];
}

-(void)hideScrim
{

    [UIView animateWithDuration:0.50 animations:^{
        self.scrim.alpha = 1.0;
    } completion:^(BOOL finished) {
        self.scrim.hidden = YES;
        [self showScrim];
    }];
}

将隐藏方法更新为

-(void)hideScrim
{

    [UIView animateWithDuration:0.50 animations:^{
        self.scrim.alpha = 1.0;
    } completion:^(BOOL finished) {
        self.scrim.hidden = YES;
        [self performSelector:@selector(showScrim) withObject:nil afterDelay:5];//if you want to show again after 5 seconds
    }];
}

并向

展示方法
-(void)showScrim {

    self.scrim.alpha = 0.0;
    self.scrim.hidden = NO;

    [UIView animateWithDuration:0.50 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.scrim.alpha = 1.0;
    } completion:^(BOOL finished) {

    }];
}

如果您最初需要延迟显示,请尝试以下代码

self.scrim.hidden = YES;
[self performSelector:@selector(showScrim) withObject:nil afterDelay:5];