I set the event to a view containing UIImageView, but it doesn't handle it in the UIImageView part.

Asked 2 years ago, Updated 2 years ago, 99 views

I made a View as below and linked the event to the function, but the UIImageView part does not respond to the event.Are there any additional items that I should set up?

·Create a view containing UILabel and UIImageView on the storyboard, and use the class as UIControl
·Link TouchDown to function on viewController

[_controladdTarget:selfaction:@selector(onClick:) forControlEvents:UICcontrolEventTouchDown];

ios uiimage

2022-09-30 17:44

1 Answers

Change the UILabel instance and UIImageView instance property userInteractionEnabled to Enabled (YES).You can do this either on a program or on a storyboard.
userInteractionEnabled is a property of UIView, and the default is YES.In other words, responding to user interaction is the default.However, only UILabel and UIImageView have a default of NO.This is probably because most of them use UIImageView and UILabel to display "just" images and text that they don't want to respond to user interaction.
Enter a description of the image here

Oh, it was a little late... I'm not going to answer the question because I'm half done like this, but I'm going to put the relevant tips on it and put a little more shape on it.

Tap UILabel, UIImageView, which is not a subclass of UIControl, to show how to run the action method.This eliminates the need to place UIControl subclasses behind it.

We use the UIGestureRecognizer for that.In this subclass UITapGestureRecognizer, create a UIImageView instance that responds to the tap

#import "ViewController.h"

@ interface ViewController()

// Image View Responding to Tap
@property(weak, nonatomic) IBOutlet UIImageView* imageView;

@end

@implementation ViewController

- (void) viewDidLoad{
    superviewDidLoad;

    // Create a UItapGestureRecognizer instance and incorporate it into the Image view. 
    UITapGestureRecognizer*tapGestureRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(imageTap:)];
    self.imageView addGestureRecognizer:tapGestureRecognizer;
}

// Action method called when you tap Image View.
- (void) imageTap: (UITapGestureRecognizer*)recognizer{
    NSLog(@"Image View was pasted.");
}

@end

You can do the same with UILabel.


2022-09-30 17:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.