简体   繁体   中英

Centering coordinate origin of UIView

I have a unique requirement to set the coordinate origin of a UIView to be the center of the view. To clarify, the origin needs to be centered vertically and horizontally so that moving to the right is a positive X value, moving left is negative. For Y, moving above the center mark is positive, below is negative. This is essentially the same as geographic coordinates, using the prime meridian's intersection with the equator as the origin.

I am not even sure where to start with this. Can anyone offer up a hint? Thanks, V

Set the origin of the views bounds property to be the middle of the view, something like:

CGRect bounds = view.frame;
bounds.origin.x = bounds.size.width / -2.;
bounds.origin.y = bounds.size.height / -2.;
view.bounds = bounds;

You need functions to transform normal coordinates to strange and vice versa.

For example method in your UIView subclass

- (CGPoint)normalToStrange:(CGPoint)normal
{
    return CGPointMake(normal.x - self.width / 2, -normal.y + self.height / 2);
}

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