【发布时间】:2011-05-14 19:59:23
【问题描述】:
我正在编写一个方法,它应该接收一个点:'in_centre',它将位于当前视图的坐标系中(也作为另一个参数传递:'target')
我需要将此点转换为窗口坐标。
文档说我应该通过将这里的第二个参数设置为 nil 来做到这一点:
centerPoint = [target convertPoint:in_centre toView:nil]; // get in Window Coords
NSLog(@"passed in (vc): %@", NSStringFromCGPoint(in_centre));
NSLog(@"centerPoint_wc: %@", NSStringFromCGPoint(centerPoint));
但是输出显示它什么也没做。
2010-11-24 07:32:45.815 ChordWheel[3195:207] 传入 (vc): {150, 150}
2010-11-24 07:32:45.816 ChordWheel[3195:207] centerPoint_wc: {150, 150}
这是错误的——它应该是 iPhone 屏幕的中心:{160, 240}
发生了什么事?
编辑:谢谢你的建议——我试过了,但还是不高兴:
- (id) initWithFrame: (CGRect) theFrame
target: (id) p_target
actionPlayChord: (SEL) p_actionPlayChord
actionSettings: (SEL) p_actionSettingsClick
{
target = p_target;
actionPlayChord = p_actionPlayChord;
self = [super initWithFrame: theFrame];
if ( ! self )
return nil;
CGPoint centre = CGPointMake(theFrame.size.width / 2.0,
theFrame.size.height / 2.0);
CGPoint c_wc = [self convertPoint: centre toView: nil];
CGPoint c_wc2 = [self convertPoint: centre toView: self.window];
NSLog(NSStringFromCGRect(theFrame));
NSLog(NSStringFromCGPoint(centre));
NSLog(NSStringFromCGPoint(c_wc));
NSLog(NSStringFromCGPoint(c_wc2));
2010-11-24 14:33:55.202 ChordWheel[3452:207] {{10, 90}, {300, 300}}
2010-11-24 14:33:55.202 ChordWheel[3452:207] {150, 150}
2010-11-24 14:33:55.203 ChordWheel[3452:207] {150, 150}
2010-11-24 14:33:55.204 ChordWheel[3452:207] {150, 150}
【问题讨论】:
-
你可能会看到奇怪的行为是
target是 nil,因为在 nil 上调用结构返回方法的返回值是未定义的。
标签: iphone ios view coordinates