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"
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.
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?
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...
<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
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.
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?
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 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 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.
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.