1

我想创建一个 4x4 的正方形网格(每个正方形由一些空间分隔)。当用户将手指从一个正方形平移到另一个正方形时,我想突出显示正方形并在正方形之间画线(以显示它们是连接的)。

我创建了一个 GridView 自定义 UIView 和 16 个子 GridElement 自定义 UIView,布局为 4x4,我想使用平移手势来选择正方形和画线。这是正确的方法吗?

感谢您的回复。所以我尝试了你的建议。现在,我创建了 UIControl 的各个网格正方形子类,并且包含视图是它们操作的目标。我的包含视图是 UIView 的子类。包含视图接收到 UIControlEventTouchDown 事件但没有接收到 UIControlEventTouchDragExit 或 (Enter)

- (void) sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    [super sendAction:action to:target forEvent:event]; 
}

- (void) sendActionsForControlEvents:(UIControlEvents)controlEvents {
    [super sendActionsForControlEvents:controlEvents];
}

- (BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    return YES;
}

- (BOOL) continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    return YES;
}

在我的 ContainerView 我有

[gridElement addTarget:self action:@selector(elementSelected:)forControlEvents:UIControlEventTouchDragEnter];
4

1 回答 1

2

This will only select the first square once the finger has moved enough to be considered a pan- you may need to add another recogniser to pick up the first touch.

If I were doing this, I'd approach it differently. You could make your individual grid squares UIControl subclasses, and make your containing view the target for their actions. UIControls already respond to events such as touch down inside, touch drag entered, touch up inside etc. you can pass these events to the containing view and highlight/draw lines as appropriate as the user moves their finger around.

于 2012-09-16T06:25:36.713 回答