iOS:如何确定是否将按钮拖动到指定区域

时间:2013-11-05 10:15:08

标签: ios objective-c uibutton

我有一个游戏的想法,用户将硬币(我的情况下的按钮)拖到黄色图像(背景为黄色的UIView图像)上,以满足他们想要计算的随机数量。我的问题出现了,一旦被拖动我必须找出硬币的位置。目前我的黄色框的坐标是359 x,376 y,宽375,高593.

-(IBAction)dragged_out:(id)sender withEvent: (UIEvent *) event
{
    UIButton *selected = (UIButton *)sender;    
    selected.center = [[[event allTouches] anyObject] locationInView:self.view];
}

这是我创建的方法,允许用户拖动我的硬币。任何建议都会有所帮助,因为我的想法已经不多了。还有一个问题是我的角钱和镍图像在移动时拖得非常平滑,但是当拖动我的四分之一图像时它非常不稳定。这与图像本身有关吗?我正在使用.png格式的图片。

谢谢,

Ryan W

2 个答案:

答案 0 :(得分:2)

试试这个,

if (CGRectIntersectsRect(selButton.frame, your_area_frame)) {

      // button is within the area.

} else {

     // Button is not is the area.
}

答案 1 :(得分:0)

如果您想知道 UIButton *选择的位置,可以使用

xcoordinate =  selected.frame.origin.x;
ycoordinate =  selected.frame.origin.y;