Obtain location coordinates from UIButton
I'm trying to obtain the coordinates of a UIButton that is in a subview of the mainviewcontroller. I can return them relative to the subview with the code below but how do I return the UIButton location as it relates to the mainviewcontroller?
ScrollingAppDelegate *appDelegate = (ScrollingAppDelegate *)[[UIApplicat开发者_StackOverflowion sharedApplication] delegate]; CGPoint canXY = appDelegate.globalcanButtonReference.frame.origin; NSLog(@"%f %f",canXY.x,canXY.y);
Assuming you don't have a reference to all of the views, this should work:
CGPoint buttonOrigin = appDelegate.globalcanButtonReference.frame.origin;
CGPoint superViewOrigin = appDelegate.globalcanButtonReference.superview.frame.origin;
CGPoint pointInMainViewController = CGPointMake(buttonOrigin.x + superViewOrigin.x, buttonOrigin.y + superViewOrigin.y);
精彩评论