I have a UIImageView that I am able to move on my screen. However, what I would like to know is the center point of the UIImageView after the user has stopped moving it. My UIImageView is located inside my main view.
I have tried using:
CGPoint centerPoint = _imageView.center;
but unfortunately, this value remains the same regardless of where I move the UIImageView on the screen. How can I determine the center point of the UIImageView that is dynamic due to the fact that the user will be moving the UIImageView on the screen?
You can do
CGRect frame = _imageView.frame;
And then do some math with the x coord, y coord, height, and width to get the center with respect to your main view.
Related
I need to resize an UImageView dynamically
for that I need to find the UImageview's absolute rect on screen, x,y is what i need actually.
because my ImageView is inside another view, if i query the ImageView frame, i get x,y of it to be 0,0
how do I find the absolute rect on screen ?
try using
CGRect targetFrame = [self.view convertRect:imageView.frame fromView:imageView];
I have a UIImageView that I am able to move in my View by dragging it. The UIImageView is contained inside my main UIView. What I am trying to determine is the center point (CGPoint) of the UIImageView after the user has stopped dragging it. I have the following code:
CGRect frame = _imageView.frame;
CGPoint newCenterPoint = _imageView.center;
NSLog(#"The old center point for the UIImageView is: %#", NSStringFromCGPoint(newCenterPoint));
newCenterPoint.x = frame.origin.x + frame.size.width/2;
newCenterPoint.y = frame.origin.y + frame.size.height/2;
NSLog(#"The new center point for the UIImageView is: %#", NSStringFromCGPoint(newCenterPoint));
With my above code, I notice that the values for newCenterPoint are not updated. Can anyone see what it is I am doing wrong, and what I can do to find the center of the UIImageView after the user has finished moving it?
Thanks in advance to all who reply.
When you have finished moving the view, both the frame and center properties have already been updated. So you don't need to do any calculation, you just read the value.
What you code does is to print the new value of center, then calculate the center from the frame. They are, and should be, the same thing.
I have an draggable UIImageView that at some point changes to another UIImageView frame. Problem is I need to make the transformation using the same clicked point in the first UIImageView. I simply do this:
_firstImageView.frameSize = _secondImageView.frameSize;
But the frame changes from the _firstImageView origin. I need to do the transformation from the point I clicked inside the _firstImageView:
CGPoint clickedPoint = [sender locationInView:self.view];
I had tried the layer.anchorPoint but that makes the imageView disappear, don't know why (I did first a conversionPoint from self.view to the _firstImageView reference system)
EDIT e.g for better explanation of problem:
I have a uiimage1 frame with height of 100. And another with 50.
If I click in the point (y=90) of uiimage1 and start dragging, there's an test intersection that I make and if intersects it changes that UIImage1 to the frame of UIImage2. But since the click was on y=90 and UIImage2 only has max y-height of 50, it changes the frame size by the UIImage1 origin. I continue dragging with the click point outside the new frame (that is only y-height=50 and point click is y=90). My question is: Can I change the frame not by its origins but by that point clicked position?
Thanks in advance
+(CGRect)getUpdatedFrame:(CGRect)frame byChangingCenterTo:(CGPoint)newCenter
{
float width = frame.size.width;
float height = frame.size.height;
return CGRectMake(newCenter.x-width/2, newCenter.y-height/2, width, height);
}
+(CGPoint)centerForRect:(CGRect)rect
{
return CGPointMake( rect.origin.x+rect.size.width/2 , rect.origin.y+rect.size.height/2 );
}
I have a UIImageView which contains a scaled image. The image size is 3264 x 2448 and the UIImageView contentMode is set to AspectFit. The UIImageView's size is 320 x 427. I have the following view hierarchy:
WorkspaceView : UIView
-> UIImageView (Contains the image)
-> A UIView which contains a UITapGestureRecognizer
I am currently trying to get the coordinate from a touch based on the UIImageView (which has a large XY coordinate space - 2448). The UIView raises a touch based on its own view, not its superview. It has encapsulated the UITapGestureRecognizer so that it raises a delegate call.
How do I get the scaled XY touch point when given a coordinate inside of the UIView? I have tried the following code and it doesn't seem to work:
- (void)lineView:(LineView *)aLineView requestsDistanceForLine:(Line *)line {
// aLineView is the subview
// line contains the startpoint and endpoint of the aLineView - it's the model
// capturedImageView is the local instance of the scaled UIImageView
CGPoint startPoint =[self convertPoint:[line startPoint] toView:self.capturedImageView];
}
The startPoint ends up being too small. Any thoughts?
It turns out someone has already solved this problem. Here's the link to the github repository for it.
https://github.com/nubbel/UIImageView-GeometryConversion
I understand that in order to get the x or y coordinates of a UI Element you can use something like button.center.y or self.view.frame.size.height to get the height of your UIView). But, if I understand all this correctly, all of this is within the bounds of a UIView.
How do you obtain the same information in terms of the iPhone screen itself?
Maybe something like:
screen.view.frame.size.height
Thanks in advance.
EDIT: My UIElements are on a UIScrollView.
Convert the point from your view's coordinate system to the enclosing window's coordinate system, and then convert that point from the window to the screen.
Use -[UIView convertPoint:toView:] and -[UIWindow convertPoint:toWindow:]
UIWindow* myWindow = myView.window;
CGPoint pointInWindow = [myView convertPoint:pointInMyView toView:myWindow];
// note: toView:nil also works
CGPoint pointInScreen = [myWindow convertPoint:pointInWindow toWindow:nil];
By the way, an important point: view properties like center and frame are relative to the view's superview's coordinate system. So, to convert a view's center to the screen:
CGPoint pointInWindow = [myView.superview convertPoint:myView.center toView:nil];
CGPoint pointInScreen = [myView.window convertPoint:pointInWindow toWindow:nil];