Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c CGRECT操作有问题_Objective C - Fatal编程技术网

Objective c CGRECT操作有问题

Objective c CGRECT操作有问题,objective-c,Objective C,.m: 我的烦恼: 当我打开应用程序时,"guy.png"的图像显示了出来,没有任何东西碰到牙牙。我需要帮助。另外//“”**我的加速计很不稳定,几乎不能与这个.m代码一起工作: UIImageView *ball; IBOutlet UIImageView *fang; 请提供帮助一些问题: CGRectIntersectsRect()正在返回YES,因为您正在比较对象的边界,而不是帧。对象的边界是描述对象如何查看自身尺寸的矩形。它的原点通常为(0,0),大小为视图的大小。由于两个视图的边

.m:

我的烦恼: 当我打开应用程序时,"guy.png"的图像显示了出来,没有任何东西碰到牙牙。我需要帮助。另外//“”**我的加速计很不稳定,几乎不能与这个.m代码一起工作:

UIImageView *ball;

IBOutlet UIImageView *fang;
请提供帮助

一些问题:

  • CGRectIntersectsRect()
    正在返回
    YES
    ,因为您正在比较对象的边界,而不是帧。对象的边界是描述对象如何查看自身尺寸的矩形。它的原点通常为(0,0),大小为视图的大小。由于两个视图的边界都从(0,0)开始,因此边界矩形相交

    您真正想要的是视图的框架。视图的边框表示该视图相对于其superview所占用的空间。即:

    if(CGRectIntersectsRect(ball.frame,fang.frame)){//etc.}

    框架表示视图的位置。边界表示内容在视图本身中的位置。在比较两个视图的位置时,几乎总是希望使用框架

  • 正如当前编写的,每次有交叉点时,您的代码都会添加一个新的子视图,但永远不会删除旧的子视图。由于加速计每秒可以触发多次,因此可能会将许多视图添加到视图层次中。这可能会显著影响性能,也可能是您看到如此糟糕的性能的原因。我建议改为创建一个
    UIImageView
    作为实例变量,然后使用
    hidden
    属性来控制它是否可见

  • 因此,我建议您修改代码如下:

    if(CGRectIntersectsRect(ball.bounds , fang.bounds))
    {
        UIImage *image = [UIImage imageNamed:@"endScreenImage.png"];
        UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
        [self.view addSubView:imageview];
        [imageview release];
    }
    
    交叉点代码与该用户先前的问题一致。问题解释得很好!
    if(CGRectIntersectsRect(ball.bounds , fang.bounds))
    {
        UIImage *image = [UIImage imageNamed:@"endScreenImage.png"];
        UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
        [self.view addSubView:imageview];
        [imageview release];
    }
    
    if (CGRectIntersectsRect(ball.frame, fang.frame)) {
        guyView.hidden = NO;
    }
    else {
        guyView.hidden = YES;
    }