2012-09-25 173 views
-4

我有一个名为“buttonPressed”的按钮。当我按下按钮时,我需要找到该按钮的x和y坐标。你有什么想法可以帮助我吗?获取UIButton的x和y坐标

UIButton *circleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    circleButton.frame = rect; 
    circleButton.layer.cornerRadius = 8.0f; 
    [circleButton addTarget:self 
        action:@selector(buttonPressed:) 
      forControlEvents:UIControlEventTouchUpInside]; 
    [self addSubview:circleButton]; 

-(void)buttonPressed:(id)sender 
{ 

} 

回答

 - (IBAction)buttonPressed:(UIButton *)sender 
    { 
     CGPoint coordinates = sender.frame.origin; 
     CGFloat x = coordinates.x; 
     CGFloat y = coordinates.y; 
    } 
+0

哪个坐标?你想在哪里发送? – wattson12

+0

@ wattson12没关系,解决了 –

回答

5

听起来你想要的X & Y关动作​​的发送者。将发件人参数附加到您的按钮切换方法并找到发件人的框架。

- (IBAction)buttonTapped:(UIButton *)sender 
{ 
    CGPoint coordinates = sender.frame.origin; 
} 
+1

谢谢。问题是:(UIButton *)我有(id)。我没有看到:/经验教训 –