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