简体   繁体   中英

Trying to find new center point of UIImageView after it is moved in iOS

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM