2011-08-15 64 views
0

我知道别人也提过类似的问题,但我查了一下,他们的问题和我的不一样。UIImageView的子类可以响应触摸事件吗?

下面是从customImageView

@implementation KeysTest 

- (id)initWithFrame:(CGRect)frame { 
self = [super initWithFrame:frame]; 
if (self) { 
    [self.userInteractionEnabled:YES]; 
self.image = [UIImage imageNamed:@"whitekey.gif"]; 
} 
return self; 
} 


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
NSLog(@"test!"); 
} 

代码出于某种原因,它不承认userInteractionEnabled方法。此外,codeSense功能对touchesBegan方法不起作用。

+1

可能是拼写错误,但第6行应该是self.userInteractionEnabled = YES或[self setUserInteractionEnabled:YES] – Sam

回答

1

我不确定您的代码示例能否正确编译。但评论者Sam是正确的,[self.userInteractionEnabled:YES]是不正确的功能。这需要更改为

self.userInteractionEnabled = YES;