UICollectionViews内的UISlider引线不会接收触摸事件

时间:2018-04-07 20:58:39

标签: ios interface uicollectionview uislider

目前,我正在努力接触UISlider,它是通过xib布局+ UICollectionView布局的。

我的包含UISlider的布局看起来像这样:

the UISlider layout

UICollectionView本身运行得非常好。 UISlider的布局与其他几个视图一起布局。

目前我体验到UISlider根本不对任何用户交互作出反应。我可以在collectionView中滚动,但是当我尝试更改滑块的值时,它就会保持不变。

我在SO上找到了一些解决方案,例如:

Pass UICollectionView touch event to its parent UITableViewCell

How do you stop UITapGestureRecognizer from catching EVERY tap?

但是解决方案似乎并不是真正完整或对我有帮助,因为我对ios和触摸处理都很陌生。

我很感激任何帮助!

2 个答案:

答案 0 :(得分:1)

创建新的.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”输入您之前创建的类名称。希望这有效!

修改

他们的个人解决方案如下所示,但这是一个通用的解决方案,适合任何需要在其代码中使用它的人:)

答案 1 :(得分:1)

首先,当我测试你的解决方案时,它没有按预期工作,但我尝试了一点。

我应该首先提到的是,我正在使用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);
    }

}

感谢您的帮助!没有它我就不会解决这个问题了几天。也许你想编辑你的答案也使用命中测试。