【问题标题】:UISlider inside UICollectionViews leads does not receive touch eventsUICollectionViews 线索内的 UISlider 不接收触摸事件
【发布时间】:2018-04-07 20:58:38
【问题描述】:

目前我正在努力接收通过 xib 布局 + UICollectionView 布局的 UISlider 的触摸。

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

UICollectionView 本身工作得很好。 UISlider 的布局与其他几个视图一起布局。

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

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

Pass UICollectionView touch event to its parent UITableViewCell

How do you stop UITapGestureRecognizer from catching EVERY tap?

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

如果有任何帮助,我将不胜感激!

【问题讨论】:

    标签: ios interface uicollectionview uislider


    【解决方案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
    
        }
    
    
    }
    

    然后,单击您的集合视图单元格并转到身份检查器,在显示“类”的地方输入您之前创建的类名称。希望这行得通!

    编辑

    他们的个人解决方案如下,但对于需要在他们的代码中适应此问题的任何其他人来说,这里是一个通用解决方案:)

    【讨论】:

    • 嗨!感谢您的回答,还请查看以下针对我的特殊情况的解决方案:-)
    • @TheWhiteLlama 很高兴它成功了。这可以仅用作一般解决方案,而您的解决方案更具体;)
    • @TheWhiteLlama 我最初为我的 UIView 获得了这段代码,所以我可以通过它点击到后面的对象
    【解决方案2】:

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

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

    感谢您的帮助!没有它,我将无法解决我几天来遇到的这个问题。也许您想编辑您的答案以同时使用命中测试。

    【讨论】:

      猜你喜欢
      • 2015-11-07
      • 2015-10-17
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多