Sumit Patel Sumit Patel - 1 year ago 294
iOS Question

Click Event on UIImageView programmatically in ios

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.

Answer Source


UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
[preArrowImage setUserInteractionEnabled:YES];
[preArrowImage addGestureRecognizer:singleTap];

    NSLog(@"single Tap on imageview");


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

func tapDetected() {
    println("Single Tap on imageview")


Replace Selector("tapDetected") with: "tapDetected:"

SWIFT - 2.2

Replace "tapDectected:" with #selector(className.tapDetected)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download