Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-10-2009, 12:49 PM   #1 (permalink)
Developer & Translator
 
Kroupy's Avatar
 
Join Date: Dec 2008
Posts: 264
Kroupy is on a distinguished road
Default Detect touch on visible part of UIView

Hi

a question concerning UIViews here:

I've got an UIView with for example a triangle in it. It is colored. The rest of the UIView is transparent, like always.

Can I via touchBegan and so on detect whether a touch was inside the UIView inside the visible part (the triangle) or inside the non-visible part (the transparent surroundings of the triangle)?

Like

"if touch was inside the triangle color it red, if it was inside the UIView but outside the triangle color it green"

Any idea?

Thanks
Kroupy is offline   Reply With Quote
Old 06-10-2009, 12:55 PM   #2 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 20
Witch_King is on a distinguished road
Default

get a button, make it custom type in IB and then spread it on full View and then put it behind the view "there is an option in one of the menu item of IB" and then just connect your touch events to that view.
Witch_King is offline   Reply With Quote
Old 06-10-2009, 01:00 PM   #3 (permalink)
Developer & Translator
 
Kroupy's Avatar
 
Join Date: Dec 2008
Posts: 264
Kroupy is on a distinguished road
Default

Thank you for your answer!

So when I miss the triangle the button gets touched instead or what?

Don't get it.

I DID understand how to DO what you wrote but I don't get it how to detect whether the touch was INSIDE or OUTSIDE the triangle....

Can you help again?

Thanks
Kroupy is offline   Reply With Quote
Old 06-10-2009, 02:24 PM   #4 (permalink)
Developer & Translator
 
Kroupy's Avatar
 
Join Date: Dec 2008
Posts: 264
Kroupy is on a distinguished road
Default

Let's get it a bit more complex:

I have two UIViews laying on top of each other. So both have the same frame and bound.

Both UIViews have a UIImage inside (a triangle for instance). A red one and a green one.

So you can imagine it:



but putting them both on one view, they would look like this:



so. The user sees the second image. Once again, they (the UIVIews) having such sizes that they lie on top of the other UIView, so they have NOT seamless borders! How can I determine or detect via code WHICH triangle got touched?

Thanks!
Kroupy is offline   Reply With Quote
Old 06-10-2009, 04:41 PM   #5 (permalink)
Developer & Translator
 
Kroupy's Avatar
 
Join Date: Dec 2008
Posts: 264
Kroupy is on a distinguished road
Default

no idea?
Kroupy is offline   Reply With Quote
Old 06-10-2009, 04:48 PM   #6 (permalink)
Tutorial Author
 
Steaps's Avatar
 
Join Date: Oct 2008
Location: Ontario, Canada
Posts: 464
Steaps is on a distinguished road
Default

Not sure about triangles, but you can detect if the touch is inside a CGRect. I said this because you said "for example, a triangle".

BTW: Colour fail with those pictures .
Steaps is offline   Reply With Quote
Old 06-10-2009, 04:52 PM   #7 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 20
Witch_King is on a distinguished road
Default

Hey Kroupy,

I am on work so let me get back to my place and then I will reply you (after finding out how to do that)
Witch_King is offline   Reply With Quote
Old 06-10-2009, 04:53 PM   #8 (permalink)
Developer & Translator
 
Kroupy's Avatar
 
Join Date: Dec 2008
Posts: 264
Kroupy is on a distinguished road
Default

what do you mean with "colour fail"?

Well, CGRect would be easier, would it, just give it a range, like touch.x has to be between 10px and 120px but, you see my triangle isn't plain. It needn't be a triangle or a "normal" form. It might be a random area...
Kroupy is offline   Reply With Quote
Old 06-10-2009, 04:54 PM   #9 (permalink)
Developer & Translator
 
Kroupy's Avatar
 
Join Date: Dec 2008
Posts: 264
Kroupy is on a distinguished road
Default

Thanks Witch_King, you're great! Don't hurry, just Don't forget ;-)
Kroupy is offline   Reply With Quote
Old 06-11-2009, 05:31 AM   #10 (permalink)
Developer & Translator
 
Kroupy's Avatar
 
Join Date: Dec 2008
Posts: 264
Kroupy is on a distinguished road
Default

push - it's important for me ,-)
Kroupy is offline   Reply With Quote
Old 06-11-2009, 12:38 PM   #11 (permalink)
Developer & Translator
 
Kroupy's Avatar
 
Join Date: Dec 2008
Posts: 264
Kroupy is on a distinguished road
Default

Quote:
Originally Posted by Witch_King View Post
Hey Kroupy,

I am on work so let me get back to my place and then I will reply you (after finding out how to do that)
how long are you at work ;-) xD?
Kroupy is offline   Reply With Quote
Old 06-11-2009, 03:26 PM   #12 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 20
Witch_King is on a distinguished road
Wink might be the answer to your question

Quote:
Originally Posted by Kroupy View Post
how long are you at work ;-) xD?
Dear Kroupy,
apologies for the delay!

<you might already be knowing this, in that case it will help others.>
Basically, the UIResponder us the class that responds to any event generated, and events gets forwarded up on the Responder Chain until it gets an object that can respond to the event or the event is ignored.

Responder Chain:
Any class that inherits from UIResponder can be called as responder.

here is the hierarchy : < format : parent->subclass>

UIRespoder->UIView/UIViewController->UIControl

so, first chance is given to the view to respond to the event. second to the ViewController and third to the parent View so on and so forth.

back to your question:
You need to detect the touch in the specific area of the view. to do it, you have two ways<if someone knows better please educate me>:

- make that shape an object (UIView) and have it respond to the events, so you can detect the event in that area and forward the event to that object who knows that to do or to its controller.

to detect touches/taps you have four methods:
touchesBegan: withEvent:
touchesCancelled: :withEvent: ...and so on look at the documentation online.

-the second way is the painful way which I am sure is not feasible

there is a method that return the position/location of the touch within the view
UItouch is the class and the method is -(CGPoint)locationInView:<UIView>

you can track the touches by CGPoint and do some math there to see if its inside your view or not. I am sure there is a better way but unfortunately I could find only few with so much time.

P S Also, please read the definition of "what is touch/tap/gesture", its important
Witch_King is offline   Reply With Quote
Old 06-11-2009, 03:32 PM   #13 (permalink)
Developer & Translator
 
Kroupy's Avatar
 
Join Date: Dec 2008
Posts: 264
Kroupy is on a distinguished road
Default

Thank you for your answer!

I know how to detect touches in general. I can get the CGPoint of the touch.

But my problem is, that my shapes arent rectangles!

How can a triangle for instance or ANY other shape represent an UIView?

Quote: make that shape an object (UIView) and have it respond to the events

yes, but if I have a triangle UIViewImage and I make an UIView out of it, it gets the rectangle AROUND the triangle doesn't it?

Can I give an UIView a custom shape? For instance a circle? or a triangle? anything OTHER than a rectangle?

Sorry but I didn't get the point or the answer for my question out of your answer.

Simply: How can I Quote: make that shape an object Unquote ?

Thanks!
Kroupy is offline   Reply With Quote
Old 06-11-2009, 03:46 PM   #14 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 20
Witch_King is on a distinguished road
Exclamation

Quote:
Originally Posted by Kroupy View Post
Thank you for your answer!

I know how to detect touches in general. I can get the CGPoint of the touch.

But my problem is, that my shapes arent rectangles!

How can a triangle for instance or ANY other shape represent an UIView?

Quote: make that shape an object (UIView) and have it respond to the events

yes, but if I have a triangle UIViewImage and I make an UIView out of it, it gets the rectangle AROUND the triangle doesn't it?

Can I give an UIView a custom shape? For instance a circle? or a triangle? anything OTHER than a rectangle?

Sorry but I didn't get the point or the answer for my question out of your answer.

Simply: How can I Quote: make that shape an object Unquote ?

Thanks!
from your question it looked liked you were asking about events. So your question is basically centered towards graphics and core drawing.

In this case just use Quartz library it has some shapes ready that you can draw or you can define your own shape and being it a Subclass of UIView it would respond to any event like ur normal view.

Unfortunately I am not familiar with the graphics/drawing so can not help in that direction.
Witch_King is offline   Reply With Quote
Old 06-11-2009, 03:55 PM   #15 (permalink)
Developer & Translator
 
Kroupy's Avatar
 
Join Date: Dec 2008
Posts: 264
Kroupy is on a distinguished road
Default

so UIView can have different shapes than rectangles? I'm working on a map! And you know, a map hasn't got a shape that is "available inside the library" ;-) It is a map, which is devided into small parts of that map, like "states". And each state has to be a different UIVIew, but every UIVIew looks different. So you definitely know, that UIVIews can have custom shapes?
Kroupy is offline   Reply With Quote
Old 06-11-2009, 04:02 PM   #16 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 20
Witch_King is on a distinguished road
Default try out

Quote:
Originally Posted by Kroupy View Post
so UIView can have different shapes than rectangles? I'm working on a map! And you know, a map hasn't got a shape that is "available inside the library" ;-) It is a map, which is devided into small parts of that map, like "states". And each state has to be a different UIVIew, but every UIVIew looks different. So you definitely know, that UIVIews can have custom shapes?
see, machine don't differentiate between circles and rectangles, it understands the language of classes and numbers. if you can subclass the UIView with having a your choice of shape in it, I don't see any problem with it. the best way to find out is to try out, won't take long to draw a small shape and see if it works or not. If you take my word, it must.

I will anyway try and let you know how it goes.
Witch_King is offline   Reply With Quote
Old 06-11-2009, 04:14 PM   #17 (permalink)
Developer & Translator
 
Kroupy's Avatar
 
Join Date: Dec 2008
Posts: 264
Kroupy is on a distinguished road
Default

I looked up and got it the "normal" way! I think it is not a problem to give it the shape of a polygon or anything geometrical. BUT, my map is different. It is a random shape! The only way that I can bring it into the app is via PNG file, where everything that doesn't belong to the map is just transparent.

You understand my problem ?

Again: Shapes arent the problem, the problem is a random shape, that is NOT "normal". Can it get its shape from a png? so that when I click inside the transparent part, "touchesBegan" WONT get called? Or at least "locationinView" would be NULL?

I hope you undestand me, it is hard to explain...Just look at a normal map, or a map ONLY of one state for instance Florida. It is a totally random shape. You canNOT draw it. ..
Kroupy is offline   Reply With Quote
Old 06-11-2009, 05:00 PM   #18 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 20
Witch_King is on a distinguished road
Default

Quote:
Originally Posted by Kroupy View Post
I looked up and got it the "normal" way! I think it is not a problem to give it the shape of a polygon or anything geometrical. BUT, my map is different. It is a random shape! The only way that I can bring it into the app is via PNG file, where everything that doesn't belong to the map is just transparent.

You understand my problem ?

Again: Shapes arent the problem, the problem is a random shape, that is NOT "normal". Can it get its shape from a png? so that when I click inside the transparent part, "touchesBegan" WONT get called? Or at least "locationinView" would be NULL?

I hope you undestand me, it is hard to explain...Just look at a normal map, or a map ONLY of one state for instance Florida. It is a totally random shape. You canNOT draw it. ..
I dont think there is a way to break up an image in objects... because if you load it as a .png and want to detect the events on different parts responding with different events you have to break it up in objects. you can go for different gestures and they should convey the intention.

or if you can get the touch coordinates and check to see what part it is on, for that you have to do the math for maps coordinates and map them to correspond to different events. this is a very hardcoded solution and ugly as well. try to post another question with this issue. becasue it is not clear from the first question you posted.
Witch_King is offline   Reply With Quote
Old 06-11-2009, 05:03 PM   #19 (permalink)
Developer & Translator
 
Kroupy's Avatar
 
Join Date: Dec 2008
Posts: 264
Kroupy is on a distinguished road
Default

thanks, I'll try.
Kroupy is offline   Reply With Quote
Old 07-29-2009, 11:51 PM   #20 (permalink)
Registered Member
 
Join Date: Jul 2009
Location: North America
Posts: 53
shimmy is on a distinguished road
Default detecting touch in custom UIView

Can you explain in more detail how to make a custom view respond to touches? Do you just add the -(void) touchesBegan... etc methods to the class? Do you have to have some kind of UIResponder ivar? And in IB how do you hook it up to the controller so that actions will occur when you touch it?

[quote=Witch_King;91829]Dear Kroupy,
- make that shape an object (UIView) and have it respond to the events, so you can detect the event in that area and forward the event to that object who knows that to do or to its controller.

to detect touches/taps you have four methods:
touchesBegan: withEvent:
touchesCancelled: :withEvent: ...and so on look at the documentation online.
shimmy is offline   Reply With Quote
Old 08-08-2010, 03:57 AM   #21 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 1
jerryhu is on a distinguished road
Default

hope this article can help you
[iPhone] detecting a hit in a transparent area Jack of Spades in Wonderland
jerryhu is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 334
8 members and 326 guests
chiataytuday, givensur, ipodphone, jbro, mer10, mtl_tech_guy, Punkjumper, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,881
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:12 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0