UICollectionViews 里面的 UISlider 导致收不到触摸事件
UISlider inside UICollectionViews leads does not receive touch events
目前我正在努力接收通过 xib 布局 + UICollectionView 布局的 UISlider 的触摸。
我的包含 UISlider 的布局看起来像这样:
UICollectionView 本身工作得很好。使用 UISlider 的布局与其他几个视图一起布局。
目前我体验到 UISlider 根本不会对任何用户交互做出反应。我可以在 collectionView 中滚动,但是当我尝试更改滑块的值时,它会保持原样。
我在 SO 上找到了一些解决方案,例如:
How do you stop UITapGestureRecognizer from catching EVERY tap?
但解决方案似乎并不完整或对我没有帮助,因为我对 ios 和触摸处理还很陌生。
如有任何帮助,我将不胜感激!
创建一个新的 .swift
文件。随意命名并粘贴此代码:
import UIKit
class YourClassName: UICollectionViewCell {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
for subview in subviews {
if !subview.isHidden && subview.isUserInteractionEnabled && subview.point(inside: convert(point, to: subview), with: event) {
return true
}
}
return false
}
}
然后,单击您的集合视图单元格并转到身份检查器,在显示 'class' 的地方输入您之前创建的 class 名称。希望这有效!
编辑
下面是他们的个人解决方案,但这里是一个通用解决方案,适用于需要将其纳入其代码的任何其他人:)
首先,当我测试你的解决方案时,它没有按预期工作,但我试了一下。
我应该提到的第一件事是,我正在使用 Multi-OS-Engine,所以对我来说正确的代码是用纯 Java 编写的,但它也应该适用于您的swift-solution.
毕竟这是对我有用的解决方案,我用命中测试替换了测试中的点:
import org.moe.natj.general.Pointer;
import org.moe.natj.general.ann.Owned;
import org.moe.natj.general.ann.RegisterOnStartup;
import org.moe.natj.objc.ObjCRuntime;
import org.moe.natj.objc.ann.ObjCClassName;
import org.moe.natj.objc.ann.Selector;
import apple.coregraphics.struct.CGPoint;
import apple.uikit.UICollectionViewCell;
import apple.uikit.UIEvent;
import apple.uikit.UIView;
@org.moe.natj.general.ann.Runtime(ObjCRuntime.class)
@ObjCClassName("UICustomCollectionViewCell")
@RegisterOnStartup
public class UICustomCollectionViewCell extends UICollectionViewCell {
protected UICustomCollectionViewCell(Pointer peer) {
super(peer);
}
/**
* @noinspection JniMissingFunction
*/
@Owned
@Selector("alloc")
public static native UICustomCollectionViewCell alloc();
/**
* @noinspection JniMissingFunction
*/
@Selector("init")
public native UICustomCollectionViewCell init();
@Override
public UIView hitTestWithEvent(CGPoint point, UIEvent event) {
for (UIView subview : subviews()) {
if (!subview.isHidden() &&
subview.isUserInteractionEnabled()) {
return subview.hitTestWithEvent(convertPointToView(point, subview), event);
}
}
return super.hitTestWithEvent(point, event);
}
}
感谢您的帮助!没有它,我就无法解决困扰我好几天的这个问题。也许您想编辑您的答案以也使用命中测试。
目前我正在努力接收通过 xib 布局 + UICollectionView 布局的 UISlider 的触摸。
我的包含 UISlider 的布局看起来像这样:
UICollectionView 本身工作得很好。使用 UISlider 的布局与其他几个视图一起布局。
目前我体验到 UISlider 根本不会对任何用户交互做出反应。我可以在 collectionView 中滚动,但是当我尝试更改滑块的值时,它会保持原样。
我在 SO 上找到了一些解决方案,例如:
How do you stop UITapGestureRecognizer from catching EVERY tap?
但解决方案似乎并不完整或对我没有帮助,因为我对 ios 和触摸处理还很陌生。
如有任何帮助,我将不胜感激!
创建一个新的 .swift
文件。随意命名并粘贴此代码:
import UIKit
class YourClassName: UICollectionViewCell {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
for subview in subviews {
if !subview.isHidden && subview.isUserInteractionEnabled && subview.point(inside: convert(point, to: subview), with: event) {
return true
}
}
return false
}
}
然后,单击您的集合视图单元格并转到身份检查器,在显示 'class' 的地方输入您之前创建的 class 名称。希望这有效!
编辑
下面是他们的个人解决方案,但这里是一个通用解决方案,适用于需要将其纳入其代码的任何其他人:)
首先,当我测试你的解决方案时,它没有按预期工作,但我试了一下。
我应该提到的第一件事是,我正在使用 Multi-OS-Engine,所以对我来说正确的代码是用纯 Java 编写的,但它也应该适用于您的swift-solution.
毕竟这是对我有用的解决方案,我用命中测试替换了测试中的点:
import org.moe.natj.general.Pointer;
import org.moe.natj.general.ann.Owned;
import org.moe.natj.general.ann.RegisterOnStartup;
import org.moe.natj.objc.ObjCRuntime;
import org.moe.natj.objc.ann.ObjCClassName;
import org.moe.natj.objc.ann.Selector;
import apple.coregraphics.struct.CGPoint;
import apple.uikit.UICollectionViewCell;
import apple.uikit.UIEvent;
import apple.uikit.UIView;
@org.moe.natj.general.ann.Runtime(ObjCRuntime.class)
@ObjCClassName("UICustomCollectionViewCell")
@RegisterOnStartup
public class UICustomCollectionViewCell extends UICollectionViewCell {
protected UICustomCollectionViewCell(Pointer peer) {
super(peer);
}
/**
* @noinspection JniMissingFunction
*/
@Owned
@Selector("alloc")
public static native UICustomCollectionViewCell alloc();
/**
* @noinspection JniMissingFunction
*/
@Selector("init")
public native UICustomCollectionViewCell init();
@Override
public UIView hitTestWithEvent(CGPoint point, UIEvent event) {
for (UIView subview : subviews()) {
if (!subview.isHidden() &&
subview.isUserInteractionEnabled()) {
return subview.hitTestWithEvent(convertPointToView(point, subview), event);
}
}
return super.hitTestWithEvent(point, event);
}
}
感谢您的帮助!没有它,我就无法解决困扰我好几天的这个问题。也许您想编辑您的答案以也使用命中测试。