问题描述
我刚刚接受了iPhone开发的陈述,似乎无法找到答案,我正在寻找我想做的事情。
I am just getting stated with iPhone development and can't seem to find the answer I am looking for what I want to do.
好像我应该能够以编程方式创建一个UIImageView,然后为它的触摸功能设置一个事件处理程序。
It seems like I should be able to programmatically create a UIImageView and then set up an event handler for it's touch functions.
在c#中我会有类似的东西
in c# i would have something that looks like
按钮b =新的Button();
b.Click + =我的处理程序代码
Button b = new Button();b.Click+= my handler code
现在我有这个
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 141.0f, 151.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
myImage.userInteractionEnabled = YES;
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
如何覆盖触摸事件需要做什么?
What do I need to do to override the touch events?
谢谢
推荐答案
虽然这不是您问题的完整答案,但我认为以下解决方案可能是更好,然后做隐形按钮 - 解决方法..
只是从UIImageView子类并覆盖以下方法:
While it is not the complete answer to your question, i think the following solution might be better then doing the "invisible-button-workaround"..simply subclass from UIImageView and override the following method:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//your UIImageView has been touched :)
//event -> "A UIEvent object representing the event to which the touches belong."
//touches -> "A set of UITouch instances in the event represented by event that represent the touches in the UITouchPhaseEnded phase."
}
希望这会有所帮助......
hope this helps...
这篇关于触及UIImageView的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!