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

后端 未结 11 1984
悲哀的现实
悲哀的现实 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:15

    i also beleive you can assign an image to a button.

    The image can take up the entire frame and can also have no other artifacts of the buttone if you set it up right.

    check out the Property

    UIButtonInstance.currentImage
    

    That way you are not hogging your resources with elements that are essentially already there.

    0 讨论(0)
  • 2020-12-25 12:16

    The best way of doing this is:

    myButton.hidden = YES;
    

    You can't use the buttonType property because it is a read only property. Only way to use it is when creating your button dynamically.

    0 讨论(0)
  • 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;
    
    }
    
    0 讨论(0)
  • 2020-12-25 12:21

    You can hide a button (or any object) and keep it active by adding a mask to its layer. The button will be invisible but will still catch events.

    let layer = CAShapeLayer()
    layer.frame = .zero
    myButton.layer.mask = layer
    
    0 讨论(0)
  • 2020-12-25 12:25

    I managed to do it using the following code. If the iPad is landscape the button will be located in the top right of the screen.

        UIButton *adminButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        adminButton.frame = CGRectMake(974.0f, 0.0f, 50.0f, 50.0f);
        [adminButton setBackgroundColor:[UIColor clearColor]];
        [adminButton setTag:1];
        [adminButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
        [self.view addSubview:adminButton];
    

    Pressing the 'adminButton' will run the following function:

    - (void)buttonPressed:(id)sender  {
    
    int buttonId = ((UIButton *)sender).tag;
    
      switch(buttonId) {
        case 1:
           NSLog (@"Admin button was pressed");
            break;
        case 2:
            //if there was a button with Tag 2 this will be called
            break;
    
        default:
            NSLog(@"Key pressed: %i", buttonId);
            break;
       }
    
    }
    
    0 讨论(0)
  • 2020-12-25 12:27

    It's the only way I found...

    [yourButton setImage:[UIImage imageNamed:@"AnEmptyButtonWithTheSameSize.png"] forState:UIControlStateNormal];
    

    Take care of the image. It must be .png

    0 讨论(0)
提交回复
热议问题