0

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];
        }
    }
}
David Owen
  • 159
  • 1
  • 10

1 Answers1

1

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;
}
David Owen
  • 159
  • 1
  • 10
  • Glad you figured it out. Don't put "solved" in your title though - click the check mark next to your answer to let others know it's resolved. – Carl Veazey Oct 02 '12 at 01:36
  • The word solved in the title is while pass the 22 hours for the page let me check my own answer as the correct answer, now, there is a better way to do that? – David Owen Oct 02 '12 at 19:39
  • Sorry, what I meant was to do that once you were able to accept. Been too sleep deprived lately :) – Carl Veazey Oct 02 '12 at 19:54