Objective-C:CGRect .frame 相交/包含问题 [英] Objective-C: Issue with CGRect .frame intersect/contains

查看:46
本文介绍了Objective-C:CGRect .frame 相交/包含问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 UIImageView 位于水平屏幕的中心,当用户单击按钮时,另一个位于屏幕外的 UIImageView 从右侧滑入.我只是想检测被带到屏幕上的视图何时与两个静态视图发生冲突.问题是当我运行我的代码并检查 CGRect 框架时,它们是从视图开始的地方返回的,而不是结束的地方,无论我将框架调用放在我的方法中的哪个位置,或者即使我将它们放在方法之外的一个单独的方法中.我对 Obj-C 有点陌生,我知道核心动画在单独的线程上运行,我猜这就是我获得起始值的原因.(如果我在这里错了,请纠正我).

I have two UIImageViews located about center of a horizontal screen and when the user clicks a button another UIImageView, located off screen, slides in from the right. I'm am just trying to detect when the view being brought onto the screen collides with the two static views. The problem is when I run my code and check the CGRect frames they are returned from where the views start, not end, regardless of where I place the frame calls in my method or even if I place them outside the method in a separate one. I'm a bit new to Obj-C and I understand that Core Animation runs on a separate thread and I am guessing that is why I am getting the starting values. (Correct me if I am wrong here).

所以,我想这里的问题是当一个项目是静态的而另一个项目是动画时,我如何检测碰撞.这是我的代码(随意清理):

So, I guess the question here is how do I detect collisions when one item is static and another is animated. Here's my code (feel free to clean it up):

- (IBAction)movegirl 
{ 

   //disabling button
   self.movegirlbtn.enabled = NO;

   //getting graphics center points
   CGPoint pos = bushidogirl.center;
   CGPoint box1 = topbox.center;
   CGPoint box2 = bottombox.center;

   //firing up animation
   [UIView beginAnimations: nil context: NULL]; 
   //setting animation speed
   [UIView setAnimationDuration:2.0    ];

   //setting final position of bushidogirl, then resetting her center position
   pos.x =  260;
   bushidogirl.center = pos;

   //running animations
   [UIView commitAnimations];

   //playing gong sound
   [self.audioplayer play];

   //enabling button
   self.movegirlbtn.enabled = YES;


   [self collisiondetection: bushidogirl : topbox];
}

- (void)collisiondetection:(UIImageView *)item1 : (UIImageView *)item2 
{

   CGRect item1frame = item1.frame;
   CGRect item2frame = item2.frame;

   NSLog(@"Item1 frame = %d and Item 2 frame = %d", item1frame, item2frame);
}

推荐答案

怎么样:

if (CGRectIntersectsRect(item1frame, item2frame))
{
   // collision!!
}

这篇关于Objective-C:CGRect .frame 相交/包含问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆