I am displaying a image from code here is the code
UIImageView *preArrowImage =[[UIImageView alloc]init ]; preArrowImage.image =[UIImage imageNamed:@"arrowprev.png"]; preArrowImage.frame = CGRectMake(20, 60, 10, 30); [self.view addSubview:preArrowImage];
I want to handle the touch event on the preArrowImage programmatically.
Adding a Tap Gesture Recognizer in Interface Builder You don't need to switch between the code editor and Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.
An object that manages image data in your app.
SWIFT 5
let preArrowImage : UIImageView // also give it frame let singleTap = UITapGestureRecognizer(target: self, action: #selector(tapDetected)) preArrowImage.isUserInteractionEnabled = true preArrowImage.addGestureRecognizer(singleTap) //Action func tapDetected() { print("Imageview Clicked") }
Objective-c
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)]; singleTap.numberOfTapsRequired = 1; [preArrowImage setUserInteractionEnabled:YES]; [preArrowImage addGestureRecognizer:singleTap]; -(void)tapDetected{ NSLog(@"single Tap on imageview"); }
Simply add a UITapGesture
on the image but remember to make its UserInteraction Enabled.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)]; [preArrowImage addGestureRecognizer:singleTap]; [preArrowImage setMultipleTouchEnabled:YES]; [preArrowImage setUserInteractionEnabled:YES];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With