David Owen
David Owen

Reputation: 159

cocoa touch hitTest withEvent can recognize UIView subclass

I have a view with subViews, these subViews are a subClass of UIView, in the example the subClass is called 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;
}

now I'm triyng to know what kind of UIView I am pointing with the touchEvents with the following methods but in the log the pointer "vista" only recognizes the self class or the UIView class, there is any way to recognize the subClass "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];
        }
    }
}

Upvotes: 0

Views: 601

Answers (1)

David Owen
David Owen

Reputation: 159

Solved, there are the steps

  1. In the main view only left the code that interpects the touch and where is it.

v

-(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];

}
  1. In the UIView subClass called ESDCelda I override pointInside:withEvent method to know if the current single touch is on the view, "inter" is the variable that lets me know if the touch is on the view, "seleccionada" indicates if the view is highlighted, "conten" is a pointer to the superview, and "seleccion:" is the method that highlights the view it self.

v

-(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;
}

Upvotes: 1

Related Questions