[Cpp]-(void) viewDidLoad
{
[Super viewDidLoad];
UIView * view1 = [[UIView alloc] initWithFrame: CGRectMake (10.0f, 100366f, 300366f, 100366f)];
View1.backgroundColor = [UIColor blackColor];
UIView * view2 = [[UIView alloc] initWithFrame: CGRectMake (301_f, 201_f, 501_f, 501_f)];
View2.backgroundColor = [UIColor whiteColor];
[View1 addSubview: view2];
[Self. view addSubview: view1];
// ConverRect: toView
CGRect rect1 = [view1 convertRect: CGRectMake (30366f, 20366f, 50366f, 50366f) toView: view2]; // convert the frame in view1 to the frame in view2.
CGPoint point1 = [view1 convertPoint: CGPointMake (30366f, 20366f) toView: view2]; // converts a point in view1 to a point in view2
NSLog (@ "rect1 :( % f, % f)", rect1.origin. x, rect1.origin. y, rect1.size. width, rect1.size. height );
NSLog (@ "point1 :( % f, % f)", point1.x, point1.y );
// ConverRect: fromView
CGRect rect2 = [view2 convertRect: CGRectMake (30366f, 20366f, 50366f, 50366f) fromView: view1]; // convert the frame in view1 to the frame in view2 (same as above)
CGPoint point2 = [view2 convertPoint: CGPointMake (30366f, 20366f) fromView: view1]; // converts a point in view1 to a point in view2 (same as above)
NSLog (@ "rect2 :( % f, % f)", rect2.origin. x, rect2.origin. y, rect2.size. width, rect2.size. height );
NSLog (@ "point2 :( % f, % f)", point2.x, point2.y );
// Special
CGRect rect = [view1 convertRect: CGRectMake (30366f, 20366f, 50366f, 50366f) toView: nil]; // The frame in view1 is converted to a frame based on window coordinates.
NSLog (@ "rect :( % f, % f)", rect. origin. x, rect. origin. y, rect. size. width, rect. size. height );
[View1 release];
[View2 release];
}
-(Void) viewDidLoad
{
[Super viewDidLoad];
UIView * view1 = [[UIView alloc] initWithFrame: CGRectMake (10.0f, 100366f, 300366f, 100366f)];
View1.backgroundColor = [UIColor blackColor];
UIView * view2 = [[UIView alloc] initWithFrame: CGRectMake (301_f, 201_f, 501_f, 501_f)];
View2.backgroundColor = [UIColor whiteColor];
[View1 addSubview: view2];
[Self. view addSubview: view1];
// ConverRect: toView
CGRect rect1 = [view1 convertRect: CGRectMake (30366f, 20366f, 50366f, 50366f) toView: view2]; // convert the frame in view1 to the frame in view2.
CGPoint point1 = [view1 convertPoint: CGPointMake (30366f, 20366f) toView: view2]; // converts a point in view1 to a point in view2
NSLog (@ "rect1 :( % f, % f)", rect1.origin. x, rect1.origin. y, rect1.size. width, rect1.size. height );
NSLog (@ "point1 :( % f, % f)", point1.x, point1.y );
// ConverRect: fromView
CGRect rect2 = [view2 convertRect: CGRectMake (30366f, 20366f, 50366f, 50366f) fromView: view1]; // convert the frame in view1 to the frame in view2 (same as above)
CGPoint point2 = [view2 convertPoint: CGPointMake (30366f, 20366f) fromView: view1]; // converts a point in view1 to a point in view2 (same as above)
NSLog (@ "rect2 :( % f, % f)", rect2.origin. x, rect2.origin. y, rect2.size. width, rect2.size. height );
NSLog (@ "point2 :( % f, % f)", point2.x, point2.y );
// Special
CGRect rect = [view1 convertRect: CGRectMake (30366f, 20366f, 50366f, 50366f) toView: nil]; // The frame in view1 is converted to a frame based on window coordinates.
NSLog (@ "rect :( % f, % f)", rect. origin. x, rect. origin. y, rect. size. width, rect. size. height );
[View1 release];
[View2 release];
} Output result:
Conclusion:
1. A convert xxx to B/B convert xxx from A converts the Coordinate System in view A to the coordinate system in View B;
2. When A convert xxx to B and B is nil, convert the Coordinate System in view A to the coordinate system of the window.