Question

as titled, would like to tap the screen, screen will be a bit dark translucent, while the finger leaves the screen, the screen turn back to normal, which is similar as UIButton does.

In this case, I know UIButton is much easier, but here is just a sample

my codes as the following:

- (IBAction)tapped:(UITapGestureRecognizer *)sender
{
    UIView * tintView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:tintView];

    switch (sender.state) {
        case UIGestureRecognizerStateRecognized: {
        [tintView setBackgroundColor:[UIColor colorWithRed:.25 green:.25 blue:.25 alpha:.5]];
        NSLog(@"begin");
    }
    default: {
        [tintView setBackgroundColor:[UIColor clearColor]];
        NSLog(@"ended");
    }
    }
}

But, when tapping the screen, it won't be changed as above codes looks, although begin and ended are captured in the console.

If interchanging those codes between case and default like

    switch (sender.state) {
        case UIGestureRecognizerStateRecognized: {
        [tintView setBackgroundColor:[UIColor clearColor]];
        NSLog(@"begin");
    }
    default: {
        [tintView setBackgroundColor:[UIColor colorWithRed:.25 green:.25 blue:.25 alpha:.5]];
        NSLog(@"ended");
    }
    }

begin and ended could be shown in the console, but the screen will be darker and darker when tapping, never back to normal, clear color.

What's wrong with my codes? How to make it happen?

Thanks!

Was it helpful?

Solution

[self performSelector: withObject: afterDelay:] would be the usual way to accomplish something like this.

You'd setup your method which dismisses the darkened view separately, then reference it as the selector

[self performSelector:@selector(methodWhichDismissesDarkView:) withObject:nil afterDelay:0.2]

call this immediately after you darken the view, and 0.2 seconds later it will fire.

To really do it right though, make sure you can gracefully handle interruptions that happen during the delay by using this:

[NSObject cancelPreviousPerformRequestsWithTarget:self];

Which will cancel the pending action when the app no longer needs to handle it.

OTHER TIPS

by following @ryancumley 's hint, below is updated codes.

@property (strong, nonatomic)  UIView * tintView;
@synthesize tintView;

- (IBAction)tapped:(UITapGestureRecognizer *)sender
{
        switch (sender.state) {
        case UIGestureRecognizerStateRecognized: {
            [self.tintView setBackgroundColor:[UIColor colorWithWhite:0.000 alpha:0.150]];
            [self performSelector:@selector(setTintView:) withObject:nil afterDelay:.25];
            NSLog(@"begin");
        }
        default: {
            break;
            NSLog(@"ended");
        }
    }
}

- (void)setTintView:(UIView *)tintView
{
    [self.tintView setBackgroundColor:[UIColor colorWithRed:.25 green:.25 blue:.25 alpha:0]];
}

It works finally and looks like tapping a button. But, NSLog(@"ended") not triggered.

Why my original can't work?

Is this updated a correct approach or workaround?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top