38

I want to call my button click event while touching the uiimageview named as(image1).Imageview placed in uiview(view1).

This is my code:

- (IBAction)buttonClicked:(id)sender
{

 myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(alphabutt:) userInfo:nil repeats:NO];
 }


-(IBAction)alphabutt:(id)sender
{
 NSLog(@"alphabutt!");


 if(i==1)
 {
  NSLog(@"i==1");
  [image1 setImage:[UIImage imageNamed:@"appleimg.jpg"]];
  [view1 addSubview:image1];  
  transition1 = [CATransition animation];

  transition1.duration = 0.75;

  transition1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

  transition1.type=kCATransitionReveal;
  transition1.subtype=kCATransitionFromRight;
  transitioning = YES;
  transition1.delegate = self;
  [image1.layer addAnimation:transition1 forKey:nil];  
 }
 if(i==2)
 {
  NSLog(@"i==2");
  [image1 setImage:[UIImage imageNamed:@"boatimg.jpg"]];
  [view1 addSubview:image1];  
  transition2 = [CATransition animation];

  transition2.duration = 0.75;

  transition2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

  transition2.type=kCATransitionFade;
  transition2.subtype=kCATransitionFromRight;
  transitioning = YES;
  transition2.delegate = self;
  [image1.layer addAnimation:transition2 forKey:nil];

  i=0;
 }
i++;

}

The above is my code while clicking the button the buttonclicked event is called.Inside buttonclick event the timer is working to call the alphabutt(Button click event)with the time interval of 2.0.alphabutt is called with every 2.0.I want to do this animation when i'm touch the uiimageview(image1) then only it calls the button click event (alphabutt). how can I write the touch event for imageview...

Please explain briefly with some code to perform uiimageview touch event...

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • Please reply me as soon as possible... –  Sep 23 '10 at 05:14
  • Please, ALWAYS format your code. Your question is confusing about imageview touch and button touch (what do you want to ask here?) – vodkhang Sep 23 '10 at 05:18
  • 1
    Sorry for not formatting the code....In my code inside buttonclicked event NSTimer(myTimer)is used to call the another button click event(alphabutt)with the time intervel of 2.0.Instead of timer i want to call the alphabutt(button click event)by uiimageview(image1)touchevent .How can i do this...Plz help me... –  Sep 23 '10 at 05:40
  • In my code only one uiview(view1)and inside that view uiimageview(image1)is placed...Please reply me as soon as possible...Thank you Renya –  Sep 23 '10 at 05:41
  • The answer of Vivek Sehrawat at [Click Event on UIImageView programmatically in ios][1] [1]: https://stackoverflow.com/questions/17120476/click-event-on-uiimageview-programmatically-in-ios/17120537#17120537 is useful for you :D – Huy Hóm Hỉnh Sep 17 '14 at 04:01
  • Check this **simple answer** [http://stackoverflow.com/a/41328885/3177007](http://stackoverflow.com/a/41328885/3177007) – Mohamed Jaleel Nazir Dec 26 '16 at 09:15

4 Answers4

63

you can use UITapGestureRecognizer added to the UIImageView via addGestureRecognizer

snippets:

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

and

- (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
    NSLog(@"%@", [gestureRecognizer view]);
}
Tek Yin
  • 3,011
  • 3
  • 25
  • 42
58

Make your image view user interaction enabled in the xib (or through the code if you are adding it programmatically) and use the UIResponder methods touchesBegan, touchesMoved, touchesEnded etc to detect the touch on the image view:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    if ([touch view] == yourImageView)
    {
            //add your code for image touch here 
    }

}
Swapnil Luktuke
  • 10,385
  • 2
  • 35
  • 58
  • hi...My application is based on tabbar application.I'm giving all the code for touch events inside the myAppAppDelegate.m...It's not working what should i do in my application.Please explain briefly...I'm waiting Please reply soon...Thank you... –  Sep 23 '10 at 07:15
  • you dont have to handle touches in app delegate... implement the touch methods in your view controller which contains the image... – Swapnil Luktuke Sep 23 '10 at 07:30
  • 1
    and make sure your image view is 'userInteractionEnabled' – Swapnil Luktuke Sep 23 '10 at 07:34
  • Check **Swift version** [http://stackoverflow.com/a/41328885/3177007](http://stackoverflow.com/a/41328885/3177007) – Mohamed Jaleel Nazir Dec 26 '16 at 09:16
15

Here I show you two possibilities for achieving that in Swift:

First Possibility

import UIKit

class ViewController: UIViewController {
    @IBOutlet var myUIImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // You can also manually set it through IB 
        // selecting the UIImageView in the storyboard
        // and checking "User Interaction Enabled" checkbox 
        // in the Attributes Inspector panel.
        self.myUIImageView.userInteractionEnabled = true
    }

    // You can choose to use one of the UIResponder methods:
    // touchesBegan, touchesMoved, touchesEnded etc, in order to detect the touch
    // on the UIImageView.
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
       let touch: UITouch? = touches.first
       if touch?.view == myUIImageView {
          println("myUIImageView has been tapped by the user.")
       }
       super.touchesEnded(touches, withEvent: event)
    }
}

Second Possibility

import UIKit

class ViewController: UIViewController {
    @IBOutlet var myUIImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let singleTap = UITapGestureRecognizer(target: self, action: "myUIImageViewTapped:")
        singleTap.numberOfTapsRequired = 1 // Initialized to 1 by default
        singleTap.numberOfTouchesRequired = 1 // Initialized to 1 by default
        self.myUIImageView.addGestureRecognizer(singleTap)
        self.myUIImageView.userInteractionEnabled = true
    }

    func myUIImageViewTapped(recognizer: UITapGestureRecognizer) {
        if(recognizer.state == UIGestureRecognizerState.Ended){
            println("myUIImageView has been tapped by the user.")
        }
    }
}
King-Wizard
  • 15,628
  • 6
  • 82
  • 76
6

You can also drag and drop a UIButton object over the UIImageView. Then in the storyboard editor, just make the UIButton of Custom type, rendering the UIButton invisible. Then handle the click event just like you would for any button.

Ahmed Salman Tahir
  • 1,783
  • 1
  • 17
  • 26
cevaris
  • 5,671
  • 2
  • 49
  • 34