0

I want to make an if statement that says when a buttons center is at a certain position then something will happen. I've tried

if (button.center == CGPointMake (x,y){
    statement
}

but no go how do i do this.

4

1 回答 1

3

您正在寻找CGPointEqualToPoint

if (CGPointEqualToPoint(button.center, CGPointMake (x,y))) {
    statement
}

请注意,它的定义如下并执行精确的浮点比较:

CG_INLINE bool
__CGPointEqualToPoint(CGPoint point1, CGPoint point2)
{
  return point1.x == point2.x && point1.y == point2.y;
}
#define CGPointEqualToPoint __CGPointEqualToPoint

如果你想建立一个公差,你可以定义一个函数如下:

CG_INLINE bool
__CGPointNearToPoint(CGPoint point1, CGPoint point2, CGFloat maxAcceptableDistance)
{
    return sqrtf(powf(point2.x - point1.x, 2) + powf(point2.y - point1.y, 2)) < maxAcceptableDistance;
}
#define CGPointNearToPoint __CGPointNearToPoint
于 2013-10-09T16:08:45.330 回答