Cocoa touch cocoa touch hitTest with Event可以识别UIView子类

Cocoa touch cocoa touch hitTest with Event可以识别UIView子类,cocoa-touch,ios5,uiview,uiresponder,Cocoa Touch,Ios5,Uiview,Uiresponder,我有一个带有子视图的视图,这些子视图是UIView的一个子类,在本例中,该子类称为ESDcelda - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { UIImage *img = [UIImage imageNamed:@"lgrey091.gif"]; [self setBackgroundColor:[UIColor color

我有一个带有子视图的视图,这些子视图是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”


解决了,有步骤

  • 在主视图中,只留下了触摸屏的代码以及触摸屏的位置
  • 在名为ESDCelda I override POINTINDER:withEvent方法的UIView子类中,要知道当前单触是否在视图上,“inter”是一个变量,让我知道触是否在视图上,“SELECCIONDA”表示视图是否高亮显示,“conten”是指向superview的指针,“seleccion:”是突出显示视图本身的方法

  • 很高兴你明白了。不过,不要在标题中添加“已解决”-单击答案旁边的复选标记,让其他人知道它已解决。标题中的“已解决”一词是在通过22个小时的页面时,让我检查我自己的答案是否正确,现在,有更好的方法来做到这一点吗?对不起,我的意思是一旦你能够接受,就这样做。最近睡眠不足:)
    -(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];
            }
        }
    }
    
    -(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];
    
        if (self.celdaSel != nil)
        {
            NSLog(@"%d",self.celdaSel.elemento);
        }
    }
    
    -(void)perfTouch:(UITouch *)touch
    {
        CGPoint punto = [touch locationInView:self];
    
        [self hitTest:punto withEvent:nil];
    
    }
    
    -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    {
        BOOL inter = [super pointInside:point withEvent:event];
        if (inter)
        {
            //NSLog(@"%i, %@", inter, self);
            if (!self.selecionada)
            {
                [self seleccion:YES];
                if (self.conten.celdaSel != nil)
                    [self.conten.celdaSel seleccion:NO];
                [self.conten setCeldaSel:self];
            }
        }
        else
        {
            if (self.selecionada)
            {
                [self seleccion:NO];
                [self.conten setCeldaSel:nil];
            }
        }
        return inter;
    }