在iOS中以编程方式单击UIImageView上的事件


95

我正在显示代码中的图像,这是代码

UIImageView *preArrowImage =[[UIImageView alloc]init ];
preArrowImage.image =[UIImage imageNamed:@"arrowprev.png"];
preArrowImage.frame = CGRectMake(20, 60, 10, 30);
[self.view addSubview:preArrowImage];

我想以编程方式处理preArrowImage上的touch事件。



Answers:


250

目标c

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

-(void)tapDetected{
    NSLog(@"single Tap on imageview");
  }

SWIFT 4.2 / 5

let preArrowImage : UIImageView // also give it frame
let singleTap = UITapGestureRecognizer(target: self, action: #selector(tapDetected))
preArrowImage.isUserInteractionEnabled = true
preArrowImage.addGestureRecognizer(singleTap)

//Action
@objc func tapDetected() {
    print("Imageview Clicked")
}

对于Swift 2,您应该替换Selector("tapDetected")为:"tapDetected:"
datayeah 2015年

默认情况下singleTap.numberOfTapsRequired = 1,快乐编码谢谢
Ravi

“让singleTap = UITapGestureRecognizer(目标:自我,行动​​:#selector(NavigationViewController.tapDetectedImage))”
Alex22,19年

18

只需UITapGesture在图像上添加一个,但要记得使其UserInteraction Enabled

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self 
    action:@selector(singleTapGestureCaptured:)];
    [preArrowImage addGestureRecognizer:singleTap];
    [preArrowImage setMultipleTouchEnabled:YES];
    [preArrowImage setUserInteractionEnabled:YES];

6

现在在Swift!

let singleTap = UITapGestureRecognizer(target: self, action: Selector("tapDetected"))
singleTap.numberOfTapsRequired = 1

preArrowImage.userInteractionEnabled = true
preArrowImage.addGestureRecognizer(singleTap)


//Action
func tapDetected() {
    println("Single Tap on imageview")
}

4

迅捷3

UIImageView上启用UserInterAction

 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)

        if let touch = touches.first {
            if touch.view == self.imgVwPostPreview { //image View property

                //Do your actions
                //self.delegate?.didClickOnCellImageView(indexPath: self.cellIndexPath!)
            }
        }
    }

3

在视图控制器中使用带有故事的故事板。
视图可以是ImageView,UIView,UILabel,TextField或其他控制器


选择一个手势识别器后,将其拖到您的控制器上。
现在它将显示在“文档大纲”和视图控制器xib的标题中。

文档输出行和Xib中的手势图标
将手势拖动到UILable
将手势拖动到UILable


在头文件中手动创建(IBAction)方法


在头文件中创建IBAction方法,然后
将其从处理程序拖动到xib中的手势图标
用手势图标链接



0

这是另一个选择:作弊。

我的意思是,横向思考。

随心所欲地设置您的UIImage,裁剪,宽高比。

用UIView覆盖它(相等的尺寸,位置等)

将背景设置为clearcolour,将类设置为UIControl。

将内部补齐事件指向您的处理程序,然后瞧。


比UIView更简单:使用按钮并删除文本。;)
Neph

0

Xamarin.iOS

UIImageView preArrowImage; // also give it frame
var singleTap = new UITapGestureRecognizer(TapDetected);
singleTap.ShouldReceiveTouch -= TapGesture_ShouldReceiveTouch;
singleTap.ShouldReceiveTouch += TapGesture_ShouldReceiveTouch;
preArrowImage.UserInteractionEnabled = true;
preArrowImage.AddGestureRecognizer(singleTap)

bool TapGesture_ShouldReceiveTouch(UIGestureRecognizer recognizer, UITouch touch)
{
    return true;
}

void TapDetected(UITapGestureRecognizer tapGestureRecognizer)
{
    print("Imageview Clicked")
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.