简体   繁体   中英

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?

I have an alarm screen with controls for brightness and other things. Here's what I'm trying to accomplish with it:

I have a black scrim covering all the elements on the screen that I am animating from alpha 1 to 0 after 5 seconds of no user interaction. When the scrim is tapped it should animate from alpha 0 to 1 and hide so I can interact with the controls beneath it. Then once again after 5 seconds of inactivity, the scrim should animate from 0 to 1 again. The scrim should hide and unhide whenever the user taps it.

Right now I'm getting the animations to all work properly using animateWithDuration but at the completion of each animation to alpha 0 the scrim does not hide so I am unable to access the controls underneath. How can I accomplish these animations in a way that doesn't obstruct the controls on the screen whenever the alpha of scrim is 0?

Here is my code:

- (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];
    }];
}

update your hide method to

-(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
    }];
}

and show method to

-(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) {

    }];
}

if you need delay in show initially, try the below code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM