Iphone 向xcode中的映像添加事件时出错
我想向图像中添加一个事件。但如果我添加这一点,则意味着它将发出警告:UIImage可能不会响应控制事件的addtarget操作。如何更正此代码 这是我的密码 UIImageView*图像 UIImage*image1=[UIImage ImageName:@“left_arrow.png”]; image=[[UIImageView alloc]initWithImage:image1]; frame=CGRectMake(100410,30,30)Iphone 向xcode中的映像添加事件时出错,iphone,objective-c,xcode,Iphone,Objective C,Xcode,我想向图像中添加一个事件。但如果我添加这一点,则意味着它将发出警告:UIImage可能不会响应控制事件的addtarget操作。如何更正此代码 这是我的密码 UIImageView*图像 UIImage*image1=[UIImage ImageName:@“left_arrow.png”]; image=[[UIImageView alloc]initWithImage:image1]; frame=CGRectMake(100410,30,30) 您需要在UIImageView的顶部创建一个
您需要在UIImageView的顶部创建一个透明的自定义UIButton。该按钮将处理事件。UIImageView可以做到这一点
UIButton *btn = [[[UIButton alloc] initWithFrame:imageView.frame] autorelease];
btn.buttonType=UIButtonTypeCustom;
[btn addTarget:...];
如果不想使用额外的按钮,可以向imageView添加一个
UITapgestureRecognitizer
像这样:
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonPressed2:)] autorelease];
[imageView addGestureRecognizer:tapGesture];
[imageView setUserInteractionEnabled:YES];
但这更像是UIControlEventTouchDown
事件
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonPressed2:)] autorelease];
[imageView addGestureRecognizer:tapGesture];
[imageView setUserInteractionEnabled:YES];