I am new to iPhone development. can any one let me know what is CGMakePoint(x,y) and what x, y stands for and how to get CGPoint x and y value for a image.?
CGMakePoint is a function that returns a CGPoint structure initialized with the given coordinates.
Not sure what you mean by the "CGPoint x and y value for a image". The position of a UIImage in a view? You use image.frame to get the rectangle (CGRect) of the images's position within the parent view, and image.frame.origin to get the top/right coordinate.
Related
I am Trying to Resize MkCircle Through finger touch. How can I start this? can any one suggest me?
Thanks for advance.
I draw the custom circle above the map view with same radius look like circle.
Finding edge point of MKcirlce
convert center point CLLocationCoordinate2D to MKMapPoint
MKMapPoint centerMapPoint = MKMapPointForCoordinate(centerCoordinate)
Calculate MKMapSize of the circle and get width
double circleMapSize = mkCircle.boundingMapRect.size.width
3.create a point on the perimeter of the circle.
MKMapPoint leftMostPoint = MKMapPointMake(centerMapPoint.x - circleMapSize/2.0, centerMapPoint.y)
Now get CLLocationCoordinate2D point from leftMostPoint
CLLocationCoordinate2D leftMostCoordinateOnCircle = MKCoordinateForMapPoint(leftMostPoint)
So now i have center point and edge point then convert it to (x,y) of ios coordinates.
CGPoint myPoint = [self.MapView convertCoordinate:originalCoordinate toPointToView:self.MapView];
CGPoint edgePoint = [self.MapView convertCoordinate:originalCoordinate toPointToView:self.MapView];
then i calculate distance b/w those two points using pythagoras theorem
int distnce1=sqrt(powf(edgePoint.x- myPoint.x, 2) + powf(edgePoint.y-myPoint.y, 2));
int finalWidth=2*distnce1;
Then i set distance to custom circle width and height
after pich Gesture end again i try to get edge lattitude and longitude of custom circle by using circle maths
float x = circleView.frame.size.width/2 * sin(0)+circleView.center.x;
float y = circleView.frame.size.width/2 * cos(0)+circleView.center.y;
convert x,y into coordinates
CLLocationCoordinate2D select_coordinate = [self.MapView convertPoint:point toCoordinateFromView:self.view];
then i find distance b/w new coordinates and center point of MKCircle then change the radius of MKCirlce equal to Distance.
One approach which might be simplest is to actually draw the circle yourself above the map view, on a separate transparent view which responds to touch events. Then, when drawing is complete, hide that view but add an actual MKCircle to the map view with the same graphical settings.
Typically it's easier to handling drawing/gestures outside of the map view for things like this.
I have a UIImage that has a transparent background. When rotating this image, I'd like to find the bounding box around the graphic (ie the nons transparent part, if you rotate it in a UIImageView, it will find the bounding box around the entire UIImage including the transparent part).
Is there an Apple library that might do this for me? If not, does anyone know how this can be done?
If I understood your questions correctly, you can retrieve the frame (not bounds) of uiimageview then get the individual cgpoints and explicitly transform these points to get a transformed rectangle. Because in Apple's documentation it says: You can operate on a CGRect structure by calling the function CGRectApplyAffineTransform. This function returns the smallest rectangle that contains the transformed corner points of the rectangle passed to it. Transforming points 1 by 1 should avoid this auto-correcting behavior.
CGRect originalFrame = UIImageView.frame;
CGPoint p1 = originalFrame.origin;
CGPoint p2 = p1; p2.x += originalFrame.width;
CGPoint p3 = p1; p3.y += originalFrame.height;
//Use the same transformation that you applied to uiimageview here
CGPoint transformedP1 = CGPointApplyAffineTransform(p1, transform);
CGPoint transformedP2 = CGPointApplyAffineTransform(p2, transform);
CGPoint transformedP3 = CGPointApplyAffineTransform(p3, transform);
Now you should be able to define a new rectangle from these 3 points (4th one is optional because width and height can be calculated from 3 points. One point to note is that you cannot store this new rectangle in a cgrect because cgrect is defined by an origin and a size so its edges are always parallel to x and y axis. Apple's cgrect definition does not allow rotated rectangles to be stored.
Is it possible to get this? If so, can anyone please tell me how?
Get the four points of the frame of your view (view.frame)
Retrieve the CGAffineTransform applied to your view (view.transform)
Then apply this same affine transform to the four points using CGPointApplyAffineTransform (and sibling methods of the CGAffineTransform Reference)
CGPoint topLeft = view.bounds.origin;
topLeft = [[view superview] convertPoint:topLeft fromView:view];
CGPoint topRight = CGPointMake(view.bounds.origin.x + view.bounds.width, view.bounds.origin.y);
topRight = [[view superview] convertPoint:topRight fromView:view];
// ... likewise for the other points
The first point is in the view's coordinate space, which is always "upright". Then the next statement finds the point that point corresponds to in the parent view's coordinate space. Note for an un-transformed view, that would be equal to view.frame.origin. The above calculations give the equivalent of the corners of view.frame for a transformed view.
I have a buttonsthat I add on a UIImageView. With a method when the user touch the screen
the UIImageView will rotate, I want to know if there is a way to get the new position of the button after the rotation is done.
Right now I'm getting all the time the original position with this method :
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Xposition : %f", myButton.frame.origin.x);
NSLog(#"Yposition : %f", myButton.frame.origin.y);
}
Thanks,
This is a tricky question. Referring to the UIView documentation on the frame property it states:
Warning: If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.
So the trick is finding a workaround, and it depends on what exactly you need. If you just need an approximation, or if your rotation is always a multiple of 90 degrees, the CGRectApplyAffineTransform() function might work well enough. Pass it the (untransformed) frame of the UIButton of interest, along with the button's current transform and it will give you a transformed rect. Note that since a rect is defined as an origin, width and height, it can't define a rectangle with sides not parallel to the screen edges. In the case that it isn't parallel, it will return the smallest possible bounding rectangle for the rotated rect.
Now if you need to know the exact coordinates of one or all of the transformed points, I've written code to compute them before, but it's a bit more involved:
- (void)computeCornersOfTransformedView:(UIView*)transformedView relativeToView:(UIView*)parentView {
/* Computes the coordinates of each corner of transformedView in the coordinate system
* of parentView. Each is corner represented by an independent CGPoint. Doesn't do anything
* with the transformed points because this is, after all, just an example.
*/
// Cache the current transform, and restore the view to a normal position and size.
CGAffineTransform cachedTransform = transformedView.transform;
transformedView.transform = CGAffineTransformIdentity;
// Note each of the (untransformed) points of interest.
CGPoint topLeft = CGPointMake(0, 0);
CGPoint bottomLeft = CGPointMake(0, transformedView.frame.size.height);
CGPoint bottomRight = CGPointMake(transformedView.frame.size.width, transformedView.frame.size.height);
CGPoint topRight = CGPointMake(transformedView.frame.size.width, 0);
// Re-apply the transform.
transformedView.transform = cachedTransform;
// Use handy built-in UIView methods to convert the points.
topLeft = [transformedView convertPoint:topLeft toView:parentView];
bottomLeft = [transformedView convertPoint:bottomLeft toView:parentView];
bottomRight = [transformedView convertPoint:bottomRight toView:parentView];
topRight = [transformedView convertPoint:topRight toView:parentView];
// Do something with the newly acquired points.
}
Please forgive any minor errors in the code, I wrote it in the browser. Not the most helpful IDE...
How to Get the X and Y axis of UIImage, I have one images which are randomly changes
it's position so how to get the image current x and y position so i can match with
another image x and y position.I have to get the position of Image without any touch
on screen. please suggest some solution.
Thank You.
You can get the frame of any view by accessing its frame property. Within that frame struct are a CGPoint origin and a CGSize size value. The origin is probably what you're looking for. Note that it is expressed in terms of relative position of the view within its superview.
For example, the following will print the origin coordinate of a view called imageView within its superview:
CGPoint origin = imageView.frame.origin;
NSLog(#"Current position: (%f, %f)", origin.x, origin.y);