objective-c - For 循环变量总是评估为真

标签 objective-c ios

烦人的新手问题。一旦我触摸了这件作品,这个变量 isPlayerTouchingAnotherPlayer 就会被设置为 true。我几乎可以肯定,我知道为什么,但我找不到在日志中显示此信息以进行确认的方法。我可能可以通过为每个对象设置不同的标志号来做到这一点,但我希望有另一种方法。

问题是 piece 是一个对象,它也位于 p1Array 中,所以当我触摸它时它会撞到自己并且 isPlayerTouchingAnotherPlayer被评估为真。

有没有办法打印出以某种方式触摸的对象的 View 或图像名称以确认这一点?此外,是否有办法以某种方式避免这种烦人的冲突。

for (int i = 0; i <[p1Array count]; i++) {
    UIImageView *tempP1;
    tempP1 =[p1Array objectAtIndex:i];

    if (CGRectIntersectsRect(piece.frame, tempP1.frame)) {
        NSLog(@"selected piece: %@, touched piece: %@ ", piece, tempP1);
        isPlayerTouchingAnotherPlayer = TRUE;
    }
}

最佳答案

为什么不使用快速枚举并跳过您对检查不感兴趣的 ImageView 。

for (UIImageView *imageView in p1Array) {
    if (imageView == piece)
        continue;

    if (CGRectIntersectsRect(imageView.frame, piece.frame)) {
       // do whatever
    }
}

您似乎已经打印出您提供的代码示例中涉及的对象的名称。如果您想打印出对象的特定属性,您可以这样做。

关于objective-c - For 循环变量总是评估为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9386175/

相关文章:

ios - 是否可以访问 xcode 中的所有通知

ios - 从 MyTableViewController 中的方法 'viewDidLoad' 获取 MyTableViewCell?

ios - 为什么Nsmutablearray失去了值(value)?

ios - 如何将 Couchbase 服务器部署到 Amazon Web Services?

ios - 错误 : Bundle only contains bitcode-marker

objective-c - 以毫秒为单位获取当前日期

iOS XCode 突然控制台垃圾邮件 : [default] IOConnectCallMethod (kIOHIDEventServiceFastPathUserClientCopyEvent): 0xe00002f0 (copySpec = (null))

ios - 在 UITableView 中更改自定义部分 View 的标题

iphone - TableView 中的更多单元格

ios - 长按模式下的 Collection View 时,如何移动 Collection View 单元格并在 iOS 中重新排列它们?