Is there an way to make an invisible UIButton that will still “be there” and catch touch events for my UIImageView?

后端 未结 11 2044
悲哀的现实
悲哀的现实 2020-12-25 11:41

I thought to be clever and just put an transparent UIButton over an UIImageView with the exact frame size, so that I can wire it up easily with any event I like, for example

11条回答
  •  情话喂你
    2020-12-25 12:16

    Custom UIButtons respond to user interactions unless their alpha is set to 0. Put a custom UIButton on top of your imageView and connect to buttonPressed action. I have also set an additional highlighted image for my UIView, now it really behaves like a UIButton. First I have defined a duration for the UIView for staying highlighted:

    #define HIGHLIGHTED_DURATION 0.1 
    

    Then set the image view highlighted if the button is pressed and start a timer to keep it highlighted for that duration. Do not forget to set the highlighted image for your imageview.

    - (IBAction)buttonPressed:(id)sender {
    
    [_yourImageView setHighlighted:YES];
    _timer = [NSTimer scheduledTimerWithTimeInterval:HIGHLIGHTED_DURATION
                                              target:self
                                            selector:@selector(removeHighlighted)
                                            userInfo:nil
                                             repeats:NO];
    }
    

    And simply undo highlighting when the timer finishes:

    -(void) removeHighlighted{
        _yourImageView.highlighted = NO;
    
    }
    

提交回复
热议问题