touchesBegan in UIView not being called
Asked Answered
A

4

9

I know that this question must have been answered plenty of times already, but I have no idea why the touchesBegan: is not called in my UIView.

Before I added it in my app, I made a trial project to do some tests. What I did was subclass a UIView where the touchesBegan: is implemented and add this as a subview of another view. The superview has a button which shows the subclassed UIView once clicked. Everything works as expected.

However, when I did the same thing in my existing app, the touchesBegan: method, as well as the touchesMoved:, were never called. Can anyone explain what could be preventing the methods from being called?

Autotoxin answered 10/7, 2012 at 11:14 Comment(2)
is the user interaction enabled for your view?Hyla
When I enabled the user interaction, it still doesn't recognize the touches. Still though, in my trial project I didn't even enabled the user interaction, yet it still recognize the touches.Autotoxin
A
11

I just found the answer to this. I realised that the superview of the UIView has the userInteraction set to disabled (which is the default value). So when I enabled the userInteraction of the superview, the touches are now recognised.

Autotoxin answered 12/7, 2012 at 9:10 Comment(0)
F
8

In my case background = UIColor.clearColor() was the reason why touchesBegan was not called. Its obviously not called on transparent elements.

Flyback answered 8/12, 2014 at 22:8 Comment(1)
I spent 2 hours of debugging until I found this answer! Thanks man!Banditry
E
1

I was struggling on this problem for a while... and i figure it out. I think that the touchesBegan override function listen on the main view: the first one on the storyboard tree of the ViewController! I would love to post an image of my storyboard to be more clear, but i can't! ;)

Anyway, IF you have subviews, they may covers the main view... in this manner the touch wont "reach" the main view.

To avoid this you'll have to set the Background property in the Attribute Inspector to Default for ALL THE SUBVIEWS. In this way the background will be transparent and the touch will be able to reach the main view.

If you don't want to set the background to transparent there is something you can do:

-Add an outlet of your last view (with background set)

-Add a tap gesture recognizer to it

-Handle the tap disposing the keyboard

ES.

In properties:

//The last view's outlet
@IBOutlet weak var ContainerView: UIView!

In viewDidLoad():

let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
self.ContainerView.addGestureRecognizer(tap)

Outside:

func handleTap(recognizer: UITapGestureRecognizer){
    self.view.endEditing(true)
}
Epiphenomenalism answered 2/10, 2015 at 9:31 Comment(0)
F
0

For me the issue was that Touches weren't being triggered when my Stack View was embedded inside of a Scroll View. I did check that User Interaction was Enabled, but still, it was not detecting touch. Perhaps there's a way to detect Touch inside of a Scroll View, but I'm too lazy to figure it out and it is not needed for my use case, so I just removed the Scroll View and touches are detected again!

Furgeson answered 22/1 at 21:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.