2012-09-30 31 views
0

我有子视图的视图,这些子视图的UIView的子类,在本例中的子类被称为ESDcelda可可触摸则hitTest withEvent可以识别UIView子类

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

现在我triyng知道什么样的UIView我指向与以下方法的touchEvents,但在日志指针“Vista”只识别自我类或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]; 
     } 
    } 
} 

回答

1

解决,有

  1. 在主视图中仅留下了interpects触摸,并且其中是它的代码的步骤。

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. 在UIView子类称为ESDCelda我重写pointInside:withEvent方法来了解当前单点触摸是认为,“跨”是,让我知道变量如果触摸位于视图上,“seleccionada”指示视图是否突出显示,“conten”是指向superview的指针,“seleccion:”是突出显示其自身视图的方法。

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; 
} 
+0

高兴你理解了它。不要把“解决”放在标题中 - 点击答案旁边的复选标记,让别人知道它已经解决。 –

+0

在标题中解决这个词是通过页面22小时让我检查我自己的答案作为正确的答案,现在有更好的方法来做到这一点? –

+0

对不起,我的意思是一旦你能够接受,就做到这一点。最近太被剥夺了睡眠:) –