【问题标题】:cocoa touch hitTest withEvent can recognize UIView subclasscocoa touch hitTest withEvent 可以识别 UIView 子类
【发布时间】:2012-09-30 22:38:52
【问题描述】:

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

【问题讨论】:

    标签: cocoa-touch ios5 uiview uiresponder


    【解决方案1】:

    解决了,有步骤

    1. 在主视图中只留下了与触摸交互的代码以及它在哪里。

    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. 在名为 ESDCelda 的 UIView 子类中,我重写 pointInside:withEvent 方法以了解当前单次触摸是否在视图上,“inter”是让我知道触摸是否在视图上的变量,“seleccionada”表示是否突出显示视图,“conten”是指向父视图的指针,“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;
    }
    

    【讨论】:

    • 很高兴你知道了。不过,不要在标题中加上“已解决” - 单击答案旁边的复选标记,让其他人知道它已解决。
    • 标题中解决的词是,通过页面的22小时让我检查自己的答案作为正确答案,现在,有更好的方法吗?
    • 对不起,我的意思是一旦你能够接受就这样做。最近睡眠太少了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 2011-01-18
    相关资源
    最近更新 更多