Click Event on UIImageView programmatically in ios
Asked Answered
C

8

103

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.

Chili answered 15/6, 2013 at 5:24 Comment(2)
This may be of some use #9009475Di
Check this answer will help you https://mcmap.net/q/209448/-click-event-on-uiimageview-programmatically-in-iosKob
K
260

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");
  }
Kissner answered 15/6, 2013 at 5:35 Comment(3)
for Swift 2 you should replace Selector("tapDetected") with: "tapDetected:"Rockett
By default singleTap.numberOfTapsRequired=1, Happy Coding ThanksSidewheel
'let singleTap = UITapGestureRecognizer(target: self, action: #selector(NavigationViewController.tapDetectedImage))'Lyndialyndon
V
18

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];
Vanburen answered 15/6, 2013 at 5:32 Comment(0)
H
6

Now in Swift!

let singleTap = UITapGestureRecognizer(target: self, action: Selector("tapDetected"))
singleTap.numberOfTapsRequired = 1

preArrowImage.userInteractionEnabled = true
preArrowImage.addGestureRecognizer(singleTap)


//Action
func tapDetected() {
    println("Single Tap on imageview")
}
Hamadryad answered 21/5, 2015 at 22:55 Comment(0)
K
4

Swift 3

On UIImageView enable UserInterAction

 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)

        if let touch = touches.first {
            if touch.view == self.imgVwPostPreview { //image View property

                //Do your actions
                //self.delegate?.didClickOnCellImageView(indexPath: self.cellIndexPath!)
            }
        }
    }
Kob answered 26/12, 2016 at 9:13 Comment(0)
G
3

Using story board with view in view controller.
view may be ImageView, UIView, UILabel, TextField or other controller


After selecting one of the gesture recognizer drag it to your controller.
Now it will display in "Document outline" and header of view controller xib.

Gesture icon in document out line and xib
Drag gesture to UILable
Drag gesture to UILable


Manually create (IBAction)method in header file


create IBAction method in header file
Now drag it from handler to gesture icon in xib
Link it with gesture icon

Geraud answered 20/5, 2015 at 5:24 Comment(0)
D
1

Xamarin.iOS

UIImageView preArrowImage; // also give it frame
var singleTap = new UITapGestureRecognizer(TapDetected);
singleTap.ShouldReceiveTouch -= TapGesture_ShouldReceiveTouch;
singleTap.ShouldReceiveTouch += TapGesture_ShouldReceiveTouch;
preArrowImage.UserInteractionEnabled = true;
preArrowImage.AddGestureRecognizer(singleTap)

bool TapGesture_ShouldReceiveTouch(UIGestureRecognizer recognizer, UITouch touch)
{
    return true;
}

void TapDetected(UITapGestureRecognizer tapGestureRecognizer)
{
    print("Imageview Clicked")
}
Dye answered 6/4, 2020 at 8:40 Comment(0)
I
0

If your UIImageView is an IBOutlet which you get from a Storyboard or Nib, you could add a UIGestureRecognizer to the image view, enable user interaction and connect a IBAction to the gesture recognizer.

For more info check my answer on this question: How to you make a UIImageView on the storyboard clickable (swift)

Imbed answered 18/5, 2017 at 16:49 Comment(0)
A
0

Here's another option: Cheat.

I mean, think laterally.

Set up your UIImage how you want, cropped, aspect fit, whatever.

Overlay this with a UIView (equal dimensions, position etc.)

Set the background to clearcolour, and the class to UIControl.

Point the touch up inside event to your handler, and voila.

Araminta answered 24/5, 2017 at 14:48 Comment(1)
Even easier than an UIView: Use a button and delete the text. ;)Dorris

© 2022 - 2024 — McMap. All rights reserved.