本文介绍了在ios中以编程方式单击UIImageView上的Event的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在代码中显示的图像是代码
I am displaying a image from code here is the code
UIImageView *preArrowImage =[[UIImageView alloc]init ];
preArrowImage.image =[UIImage imageNamed:@"arrowprev.png"];
preArrowImage.frame = CGRectMake(20, 60, 10, 30);
[self.view addSubview:preArrowImage];
我想以编程方式处理preArrowImage上的触摸事件。
I want to handle the touch event on the preArrowImage programmatically.
推荐答案
Objective-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
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")
}
这篇关于在ios中以编程方式单击UIImageView上的Event的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!