gpt4 book ai didi

ios - 如何支持跨各种对象的触摸移动?

转载 作者:行者123 更新时间:2023-11-28 17:46:07 24 4
gpt4 key购买 nike

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

enter image description here

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

提前致谢

最佳答案

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

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

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

将该自定义类设置为容器 View 的类。然后,在容器 View 上实现各种 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]];
}
}
}

关于ios - 如何支持跨各种对象的触摸移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5853792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com