How to detect Collision Between two UIImageViews belonging in two different UIViews? - iphone

I am noob in Iphone programming, so please help.
I am rotating 2 Views in opposite direction in circular way. Now I want to find out when both images are intersected.
As of now I am rotating view not Image so CGRectIntersectsRect() won't give me the desired result. Is there any other way I can get the solution i.e. intersection of both images ?
My problem is both are in different views. Any idea or suggestion or code snippet would be great to work further towards my goal.
--Edited--
I have 2 views in one view 1st image in another 2nd image. I am rotating both views in different direction circular. there will be one time when both image will be at same location. I am unable to use CGRectIntersectsRect() as both are in different views.
How do I know that both image met at location ? i.e. Collision occurred. Please forgive abt not providing clear explanation about question.

Try this method to convert the rect in the view to the rect in window coordinates, then use CGRectIntersectsRect() . Hope this works for you.
Converts a rectangle from the
receiver’s coordinate system to that
of another view.
(CGRect)convertRect:(CGRect)rect toView:(UIView *)view
Parameters rect A rectangle in the
receiver's coordinate system. view The
view that is the target of the
conversion operation. If view is nil,
this method instead converts from
window base coordinates. Otherwise,
both view and the receiver must belong
to the same UIWindow object. Return
Value The converted rectangle.

Related

How to get the correct frame of a rotated UIView (including correct position)

I have an app where I have a rectangle that the user can rotate and pan using their fingers. I'd simply like to know what the frame is of this rotated view so I can find out if it intersects another rectangular UIView (can't use the frame property because it gets invalidated when the UIView gets transformed). What's the easiest way to accomplish this?
Every UIView has a property frame which is of type CGRect.
You can access it using view.frame.
After the transform is applied you can use the bounds and center property on the view to get the orientation. It may take a little bit of calculation but i hope you can get to it easily.
Refer to this image from an answer to this question.

Is there a way to literally pause the view?

Can I pause a view for a certain amount of time? Basically, while an action is occurring, my view becomes messed up and I cannot figure out why. Everything gets shifted, and I've been looking through the code line by line for hours. So can I prevent the view from changing itself and then somehow resume it after the action is over? I really don't know if this is a stupid question, but thanks for your help!
The action is sending something to a server, for whatever reason the
entire view is shifted. After the view is shifted, I have the
coordinates outputted to the log, and it thinks the view is at 0,0,
even though it's not.
I think you might be getting mixed up in the distinction between frames & bounds?
From this link
The frame of an UIView is the rectangle, expressed as a location (x,y)
and size (width,height) relative to the superview it is contained
within.
The bounds of an UIView is the rectangle, expressed as a location
(x,y) and size (width,height) relative to its own coordinate system
(0,0).
Essentially, if you're looking at the origin coordinates of the bounds of your view, it will always be 0,0. Instead, you should look at its frame coordinates. I hope this helps you in figuring out the problem.
No, there's no general mechanism for preventing changes to a view. If the view in question is your own UIView subclass, you can of course override the methods that modify the view and effectively prevent changes that way. That'd be a lot of work, however, and I don't think the result would be worth the effort; you'll be much better off finding the real problem and fixing it. It's always easier and better to work with the framework and not against it.

Difference between view's frame and view's bound + iPhone

I want to know what is the difference between 'frame' and 'bound' property of UIView. I get the same results using both properties. I can not figure out the difference between the two..
Thanx in advance.
The frame is the view's location in its superview, using the superview's coordinate system.
The bounds is the view's location and size in its own coordinate system.
If you are getting the same results for both properties, it means that the view fills its superview, and both views have (0, 0) as the origin. Try changing the frame, and you will see it move to different positions within its superview.
This is covered in the documentation. The frame and bounds are two different coordinate systems.
I know its too late but as this a common question so I am posting a link to apple documentation to have a detailed explanation on this

iPhone: Rotating a drawing without rotating the view within which it is drawn

Anyone who has gone through the Stanford CS193P class on iTunes will recognize this from Assignment 3 (HelloPoly). For those not familiar:
I have a custom view called polyView, an instance of PolygonView, a subclass of UIView. On this view, I use CGContextDrawLinearGradient to paint a color gradient over the entire rectangular view. Then I use CGContextDrawPath to stroke and fill a polygon within the bounds of polyView. And I have a UILabel called nameLabel in the center of the view (and polygon) that displays the name of the polygon (triangle, quadrilateral, pentagon, etc.). All of this works fine, and the code to do all this is in the -(void)drawRect method of the PolygonView class.
Where I ran into trouble is with an additional requirement to rotate the polygon within the view in response to user gestures. I used CGAffineTransformRotate() in response to touchesBegan() and touchesMoved() events within the PolygonView class, and this basically works, too. But I can only rotate the entire polyView, not just the polygon drawn on it. I'm sure I could go back and recalculate the path of the polygon and redraw/fill the path in response to each touchesMoved() event, but that would be expensive and can't be the best method. How can I use CGAffineTransformRotate to rotate just the polygon, without rotating the gradient-filled view or the label in the center?
Or is there some way to create the polygon on a layer that I can place over the background polyView at the desired rotation angle?
Thanks for any help you can give a beginner here!
Duane
You can do a CGContextSaveGState(...) just before doing the rotation transformation, then drawing the polygon and restoring the drawing state with a CGContextRestoreGState(...) afterwards, so as not to affect any other drawing in the view later.

Cannot move the MKMapView from a centered position?

I am working with creating a simple iPhone app to show a map and some labels, however the MKMapView likes being centered when I put it in the interface builder.
I am very new to this, so i apologize for my ignorance.
I cannot seem to find a way to move the MKMapView from its centered position. I can scale the edges, however the scaling is mirrored on both sides and it does not let me alter the x and y value manually. I want to move the MKMapView to the top of the screen and have some labels on the bottom, however I don't want any extra space at the top.
Is there any method I can use (either within the Interface Builder OR in the actual Objective C code) which will let me move the MKMapView more freely?
Thank you,
-Serge
It sounds like you're attempting to place it in a bare window. If you want to position it on a portion of the screen somewhere you'll want to place a regular View in the window first (that could just fill the window), then put the MKMapView on that view. You'll be able to move it to wherever you'd like.