UIImageView触摸事件

我想调用我的button点击事件,同时触摸命名为(image1)的uiimageview。放置在uiview(view1)中的图像视图。

这是我的代码:

- (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++; } 

以上是我的代码,同时点击buttonclicked事件被调用的button。在buttonclick事件中,定时器正在调用alphabutt(button点击事件),时间间隔为2.0.alphabutt每2.0调用一次。我想这样做animation时,我触摸uiimageview(image1),然后只有它调用button单击事件(alphabutt)。 我怎么能写imageview触摸事件… Plz帮助我做到这一点…

PLZ简要说明一些代码执行uiimageview触摸事件…

谢谢你….(我在等你的回复)Renya

你可以使用通过UITapGestureRecognizer添加到UIImageView addGestureRecognizer

片段:

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

 - (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer { NSLog(@"%@", [gestureRecognizer view]); } 

在xib中启用你的图像视图用户交互(或者通过代码,如果你正在以编程方式添加它),并使用UIResponder方法touchesBegan,touchesMoved,touchesEnded等来检测图像视图上的触摸:

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; if ([touch view] == yourImageView) { //add your code for image touch here } } 

在这里我向你展示了两个在Swift中实现的可能性:

第一个可能性

 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) } } 

第二可能性

 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.") } } } 

你也可以在UIImageView拖放一个UIButton对象。 然后在故事板编辑器中,制作Customtypes的UIButton ,使UIButton不可见。 然后像处理任何button一样处理click事件。