convertPoint:toView:似乎没有工作

Kin*_*rew 16 iphone coordinates

我有一个带有自定义TableCellViews的TableView,它上面有UILabel和UIButtons.当其中一个按钮被录音时,我想显示一个描述按钮文本的"工具提示".

除了当我尝试将UIButton的中心坐标转换为rootView(UIView)的坐标时,大多数都工作正常.

这是代码:

- (void) fancyLabelButtonPressed: (UIButton *) button {
  CGPoint btnPoint = button.center;   // x=200.5 y=27.5
  CGPoint rootViewPoint = [button convertPoint:btnPoint toView:rootView];
  // rootViewPoint -> x=390.5 y=197.5
  CGPoint pointToUse = CGPointMake(btnPoint.x +20, rootViewPoint.y - 23); // Hack to get it close
}
Run Code Online (Sandbox Code Playgroud)

怎么能rootViewPoint.x=390.5当我在纵向视图?!?通过使用按钮中的x和rootViewPoint中的y,我接近它应该是什么,但它只是一个黑客.

有谁看到我做错了什么?或者,还有更好的方法?

Jas*_*oco 50

这是因为您正在从错误的视图转换点.该center属性实际上位于按钮的superview的坐标系中,无论是什么,所以当你将它转换为rootView时,你必须从那里转换它.所以:

rootViewPoint = [[button superview] convertPoint:btnPoint toView:rootView];
Run Code Online (Sandbox Code Playgroud)

这应该会给你你想要的东西.