OGeek|极客世界-中国程序员成长平台

标题: iphone - 创建我的第一个 iPhone 应用程序.. 重新定位 block [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:54
标题: iphone - 创建我的第一个 iPhone 应用程序.. 重新定位 block

所以我有一个在 iPhone 内置加速度计的帮助下移动的 block ,另一个随机出现在屏幕上的 block 。我正在尝试使用 if 语句来确定移动 block 是否轻敲或触摸不动的目标 block ,如果这样做,它将随机重新定位到屏幕上的另一个位置。除了确定两个坐标在任何给定点是否相等之外,一切正常。到此为止了..

编辑: *所以我删除了 xx & yy 变量并用 self.xVar 和 self.yVar 替换它们,这似乎有点工作,但非常粗略并停止了

edit2: **所以它删除 xx 和 yy 确实有帮助,但它只适用于 2-3 次点击,然后停止。

edit3 **意识到使用相同的 x OR y 变量并不是解决此问题的正确方法..

-(void) accelerometerUIAccelerometer *)accelerometer didAccelerateUIAcceleration *)acceleration
{
...

    //Make the new point
    CGPoint buttonNewCenter = CGPointMake(newX,newY);
    Button.center = buttonNewCenter;


    int xx = self.xVar;
    int yy = self.yVar;

    if (newX == xx || newY == yy) {
        int randX = arc4random() % 320;
        int randY = arc4random() % 548;

        CGPoint randNewPlace = CGPointMake(randX, randY);
        Rand.center = randNewPlace;
    }

}

... ...

- (void)viewDidAppearBOOL)animated
{
    [self awakeaccelerometer];

    int randX = arc4random() % 320;
    int randY = arc4random() % 548;

    CGPoint randNewPlace = CGPointMake(randX, randY);
    Rand.center = randNewPlace;

    self.xVar = (randX+15);
    self.yVar = (randY+15);


}

因此,打开函数随机确定目标 block 的位置,而另一个 block 根据加速度计在屏幕上自由移动。我正在尝试确定 if self.xVar || self.yVar == newX || newY 在任何给定时间。提前致谢!



Best Answer-推荐答案


我不确定您为什么要查看两个点是否在同一个位置,而不是查看两个矩形是否相交。点非常小。如果你想做的是查看一个点是否在一个矩形内,你可以使用CGRectContainsPoint。 (参见苹果网站 here 上的讨论。)如果您想查看两个矩形是否相交(我认为您正在尝试这样做),请使用 CGRectIntersectsRect。 (您可能需要先调用 CGRectMake。)

如果你真的想做数学,那么检查 (self.x+self.width

关于iphone - 创建我的第一个 iPhone 应用程序.. 重新定位 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15671673/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4