UIImageView触摸事件

38
我希望在触摸名为(image1)的UIImageView时调用按钮单击事件。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事件。在buttonclick事件中,定时器会按照2.0的时间间隔调用alphabutt(Button click event)。alphabutt每隔2.0秒被调用一次。我希望在触摸uiimageview(image1)时才调用按钮点击事件(alphabutt)以执行动画效果。
如何编写用于uiimageview触摸事件的代码,请简要说明并附上示例代码...

请尽快回复我。 - Renya
请务必对您的代码进行格式化。关于imageview触摸和button触摸,您的问题有些混淆(您想在这里问什么?) - vodkhang
1
抱歉没有格式化代码...在我的代码中,按钮点击事件内使用了NSTimer(myTimer)来调用另一个按钮点击事件(alphabutt),时间间隔为2.0。我想用uiimageview(image1)的touchevent来调用alphabutt(button click event)。我该怎么做...请帮帮我... - Renya
在我的代码中,只有一个UIView(view1),并且在该视图内放置了UIImageView(image1)...请尽快回复我...谢谢。Renya - Renya
Vivek Sehrawat在在iOS中以编程方式对UIImageView进行单击事件的答案对您有用:D - Huy Hóm Hỉnh
请查看这个简单的答案:https://dev59.com/NmQn5IYBdhLWcg3wETvj#41328885 - Mohamed Jaleel Nazir
4个回答

63

您可以使用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]);
}

58

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

}

嗨...我的应用程序是基于选项卡应用程序的。我在myAppAppDelegate.m文件中提供了所有触摸事件的代码...但它不起作用,我该怎么办呢?请简要解释一下...我在等待,请尽快回复...谢谢... - Renya
你不需要在应用程序委托中处理触摸事件...请在包含图像的视图控制器中实现触摸方法... - Swapnil Luktuke
1
请确保您的图像视图设置为'userInteractionEnabled'。 - Swapnil Luktuke
请检查 Swift 版本 https://dev59.com/NmQn5IYBdhLWcg3wETvj#41328885。 - Mohamed Jaleel Nazir

15

在这里我向您展示在 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.")
        }
    }
}

6
您可以将UIButton对象拖放到UIImageView上。然后在Storyboard编辑器中,将UIButton的类型设置为Custom,使UIButton不可见。然后处理click事件,就像处理任何按钮一样。

请检查 Swift 版本 https://dev59.com/NmQn5IYBdhLWcg3wETvj#41328885。 - Mohamed Jaleel Nazir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接