UIImageView触摸事件

时间:2010-09-23 05:09:49

标签: iphone uiimageview touch

我想在触摸名为(image1)的uiimageview时调用我的按钮点击事件.Imageview放在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事件。内部按钮点击事件计时器正在调用alphabutt(按钮点击事件),时间间隔为2.0.alphabutt每2.0调用一次。我想要当我触摸uiimageview(image1)然后只调用按钮点击事件(alphabutt)时执行此动画。 如何为imageview编写触摸事件...

请简要说明一些代码来执行uiimageview触摸事件......

4 个答案:

答案 0 :(得分:59)

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

答案 1 :(得分:56)

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

}

答案 2 :(得分:13)

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

答案 3 :(得分:6)

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