0

Currently I'm struggling with receiving touch on a UISlider which is layed out via a xib layouts + a UICollectionView.

My layout which does contain the UISlider does look like this:

the UISlider layout

The UICollectionView itself works perfectly fine. The layout with the UISlider is layed out with several other views.

Currently I experience the UISlider not to react to any user interaction at all. I can scroll inside the collectionView, but when I try to change the value of the slider it just stays as it is.

I found some solutions on SO, like:

Pass UICollectionView touch event to its parent UITableViewCell

How do you stop UITapGestureRecognizer from catching EVERY tap?

but the solutions seem not to be really complete or helpful to me, since I'm quite new to ios and the touch handling.

I'd appreciate any help!

TheWhiteLlama
  • 1,276
  • 1
  • 18
  • 31

2 Answers2

1

Create a new .swift file. Name it whatever you want and paste in this code:

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

    }


}

Then, click on your collection view cell and go to the identity inspector and where it says 'class' input the class name you created earlier. Hope this works!

EDIT

Their personal solution is below, but here is a general solution for anyone else who needs to fit this in their code :)

George
  • 25,988
  • 10
  • 79
  • 133
  • 1
    Hi! Thanks for the answer, please also have a look on the solution that did it for my special case below :-) – TheWhiteLlama Apr 07 '18 at 23:40
  • @TheWhiteLlama Glad it worked. This can just be used as a general solution, and your one is more specific ;) – George Apr 08 '18 at 11:43
  • @TheWhiteLlama I originally got this code for my UIView so I could click through it to objects behind – George Apr 08 '18 at 11:45
1

First when I tested your solution it did not work as expected, but I tried a little bit around.

First thing I should mention is, that I'm using Multi-OS-Engine, so the code that did the right thing for me is written in pure Java, but it should also work with your swift-solution.

This is the solution which worked for me after all, I replaced the point inside test by the hit test:

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);
    }

}

Thanks for your help! Without it I would not have solved this issue I have had for days. Maybe you want to edit your answer to also use hit test.

TheWhiteLlama
  • 1,276
  • 1
  • 18
  • 31