iphone – UIImageView作为按钮

iphone – UIImageView作为按钮,第1张

概述我试图使UIImageView接受动作,并在每次点击UIImageVie时触发它,但是我很难让它上班,请帮助我 但这里是我在做什么: [View setUserInteractionEnabled:YES];[View addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside] 我试图使UIImageVIEw接受动作,并在每次点击UIImageVIE时触发它,但是我很难让它上班,请帮助我

但这里是我在做什么:

[VIEw setUserInteractionEnabled:YES];[VIEw addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventtouchUpInsIDe];- (voID) myAction:(ID)sender{    NSLog(@"It works!");}
解决方法 Objective-C的

在你的vIEwDIDLoad:方法写这个:

-(voID)vIEwDIDLoad {    UIImageVIEw *imagevIEw = [[UIImageVIEw alloc]initWithFrame:CGRectMake(100.0,100.0,100.0)];    [imagevIEw setimage:[UIImage imagenamed:@"image.png"]];    [imagevIEw setUserInteractionEnabled:YES];    UITapGestureRecognizer *singleTap =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapPing:)];    [singleTap setNumberOfTapsrequired:1];    [imagevIEw addGestureRecognizer:singleTap];    [self.vIEw addSubvIEw:imagevIEw];}

然后调用你的手势方式如下:

-(voID)singleTapPing:(UIGestureRecognizer *)recognizer {    NSLog(@"image clicked");}

迅速

overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    let imageVIEw: UIImageVIEw = UIImageVIEw(frame: CGRectMake(100.0,100.0))    imageVIEw.image = UIImage(named: "image.png")    imageVIEw.userInteractionEnabled = true    let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self,action: #selector(BaseVC.singleTapPing(_:)))    singleTap.numberOfTapsrequired = 1;    imageVIEw.addGestureRecognizer(singleTap)    self.vIEw.addSubvIEw(imageVIEw)}func singleTapPing(recognizer: UIGestureRecognizer) {    print("image clicked")}
总结

以上是内存溢出为你收集整理的iphone – UIImageView作为按钮全部内容,希望文章能够帮你解决iphone – UIImageView作为按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1039376.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存