【问题标题】:UIImageView Touch EventUIImageView 触摸事件
【发布时间】:2010-09-23 05:09:48
【问题描述】:

我想在触摸名为(image1)的uiimageview时调用我的按钮单击事件。放置在uiview(view1)中的Imageview。

这是我的代码:

- (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(按钮单击事件)。每隔 2.0 调用一次 alphabutt。我想要当我触摸 uiimageview(image1) 时执行此动画,然后它只会调用按钮单击事件 (alphabutt)。 如何为 imageview 编写触摸事件...

请用一些代码简单解释一下执行uiimageview触摸事件...

【问题讨论】:

  • 请尽快回复我...
  • 请务必格式化您的代码。您的问题对 imageview 触摸和按钮触摸感到困惑(您想在这里问什么?)
  • 抱歉没有格式化代码......在我的代码里面buttonclicked事件NSTimer(myTimer)用于调用另一个按钮点击事件(alphabutt),时间间隔为2.0。而不是计时器我想通过 uiimageview(image1)touchevent 调用 alphabutt(button click event)。我该怎么做...请帮帮我...
  • 在我的代码中只有一个 uiview(view1) 并且在该视图中放置了 uiimageview(image1)...请尽快回复我...谢谢 Renya
  • Vivek Sehrawat 在 [在 iOS 中以编程方式在 UIImageView 上单击事件][1] [1] 的答案:stackoverflow.com/questions/17120476/… 对您有用:D

标签: iphone uiimageview touch


【解决方案1】:

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

sn-ps:

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

【讨论】:

    【解决方案2】:

    在 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 中提供了触摸事件的所有代码...它不起作用我应该在我的应用程序中做什么。请简单解释一下...我正在等待 请尽快回复...谢谢...
    • 您不必在应用程序委托中处理触摸...在包含图像的视图控制器中实现触摸方法...
    • 并确保您的图像视图是 'userInteractionEnabled'
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      猜你喜欢
      • 2011-10-19
      • 2010-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多