tvincent
tvincent

Reputation: 73

How to disable touch detection on a UIImage?

I am working on an app, which actually works like MSPaint (something to draw lines, etc...). I got a white UIView, basically where the user draws. On top of this UIView I set up a UIImage, which is gray, with a 0,4 alpha. I want this UIImage to be used as a blotting paper. The idea is to disable touch when the user put the palm of his hand on this area, so it's more comfortable to draw (multitouch is disabled, and with this "blotting paper" you won't draw something accidentally with your palm...)

Even if I bring the UIImage to the front, on top of the view, and even if I disable user interactions on this UIImage, it is still possible to draw on the UIView. , behind the UIImage (kind of strange!)

I do not understand what's happening, because, it seems that the image is transparent, and that the UIView "behind" is still active, even if she's overlaid by the UIImage?!

Any help/indication/idea would be much appreciated! Thank you :-)

Upvotes: 0

Views: 3795

Answers (5)

tvincent
tvincent

Reputation: 73

OK, so I managed to do what I wanted to! YAY!

I got 3 different classes :

  • StrokesViewController (UIViewController)-the view controller

  • StrokesView (UIView) - the view where the user draws the strokes.

  • BlottingPaper (UIView) - the blotting paper.

I got a XIB file "linked" to all three.

I created this new class, called "BlottingPaper", typed UIView. the .h and .m file are actually empty (I do import #import < Foundation/Foundation.h >) User interaction is enable on BlottingPaper. I do not use the exclusive touch on this class.

On the XIB file, I just put a view on top of StrokesView. I link it to BlottingPaper (modify the alpha as I want, blablabla...)

And that's it! When I put the palm of my hand on it, it doesn't draw anything on the area where my hand is, but I still can draw with my finger on the rest of the StrokesView!

Upvotes: 1

C4 - Travis
C4 - Travis

Reputation: 4492

In addition to Dancreek's response, you should be setting buvard.userInteractionEnabled = YES; so that it captures interaction.

You should also set buvard.exclusiveTouch = YES; so that buvard is the only view which will receive touch events.

When you remove buvard you should set buvard.exclusiveTouch = NO; so that other views will regain their ability to receive touches.

Upvotes: 0

Norbert Bicsi
Norbert Bicsi

Reputation: 1568

You have to disable the user interaction on the UIImageView not the UIImage and it should work.

Edit:

Or you could be sneaky and just add an empty view over it. Use the same frame size so it overlaps perfectly and thats it. You'll be able to see everything you need and it's not a subview of it so there will eb no interaction and touches will get registered but won't have any effect. :P

No better ideas unless you post some of your code...

Upvotes: 2

Dancreek
Dancreek

Reputation: 9544

You may actually want to do the opposite. When you disable user interaction or touches, the view basically becomes invisible to touches and they are passed on to the next view.

In your case you do want userInteractionEnabled because you want the view to catch those touches.

Upvotes: 4

djleop
djleop

Reputation: 697

Have you set the "userInteractionEnabled" property of the UIImage to "NO" ?

Upvotes: 4

Related Questions