1

我的应用程序中有一些这样的标签......

在此处输入图像描述

我需要做的是,当点击标签时,我只是在屏幕底部显示标签名称。分别单击每个单元格时效果很好。但即使用户单击特定标签并将手指移到另一个标签上,我也想显示更改。也就是说,一旦他按下屏幕,无论他的手指移动到哪里,我都想追踪那些地方并想显示变化。我怎样才能做到这一点?请简要说明。

提前致谢

4

1 回答 1

0

默认情况下,触摸事件仅发送到它们开始的视图。因此,最简单的方法是将所有标签放在拦截触摸事件的容器视图中,并让容器视图决定如何处理事件。

首先为容器创建一个 UIView 子类并通过覆盖来拦截触摸事件hitTest:withEvent:

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    // intercept touches
    if ([self pointInside:point withEvent:event]) {
        return self;        
    }
    return nil;
}

将该自定义类设置为容器视图的类。然后,touches*:withEvent:在您的容器视图上实现各种方法。在你的情况下,这样的事情应该有效:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    // determine which view is under the touch
    UIView* view = [super hitTest:[[touches anyObject] locationInView:self] withEvent:nil];

    // get that label's text and set it on the indicator label
    if (view != nil && view != self) {
        if ([view respondsToSelector:@selector(text)]) {
            // update the text of the indicator label
            [[self indicatorLabel] setText:[view text]];
        }
    }
}
于 2011-05-02T21:37:02.783 回答