我有一个带有子视图的视图,这些子视图是 UIView 的子类,在示例中子类称为 ESDcelda
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIImage *img = [UIImage imageNamed:@"lgrey091.gif"];
[self setBackgroundColor:[UIColor colorWithPatternImage:img]];
ESDcelda *cel1 = [[ESDcelda alloc] initWithTipo:1];
[cel1 setFrame:CGRectMake(100, 100, cel1.frame.size.width, cel1.frame.size.height)];
[self addSubview:cel1];
cel1 = [[ESDcelda alloc] initWithTipo:2];
[cel1 setFrame:CGRectMake(300, 100, cel1.frame.size.width, cel1.frame.size.height)];
[self addSubview:cel1];
}
return self;
}
现在我想知道我用以下方法用touchEvents指向什么样的UIView,但在日志中指针“vista”只能识别self类或UIView类,有什么方法可以识别子类“celdaSel “?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
[self perfTouch:touch];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
[self perfTouch:touch];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
[self perfTouch:touch];
}
-(void)perfTouch:(UITouch *)touch
{
CGPoint punto = [touch locationInView:self];
UIView *vista = (ESDcelda *)[self hitTest:punto withEvent:nil];
if (![vista isKindOfClass:[self class]])
{
celdaSel = (ESDcelda *)vista;
[celdaSel seleccion:YES];
}
else
{
if (celdaSel != nil)
{
[celdaSel seleccion:NO];
}
}
}