简体   繁体   English

iPhone编程:UIImageView和动画的碰撞检测

[英]iphone programming: collision detection of a UIImageView and an animation

I have an animation in viewDidAppear shown below 我在viewDidAppear中有一个动画,如下所示

-(void)viewDidAppear:(BOOL)animated
{
    for (int i = 0; i < 100; i++) {

        p = arc4random_uniform(320)%4+1; //global integer

        CGRect startFrame = CGRectMake(p*50, -50, 50, 50);
        CGRect endFrame   = CGRectMake(p*50, CGRectGetHeight(self.view.bounds) + 50,
                                       50,
                                       50);

        animatedView = [[UIView alloc] initWithFrame:startFrame];
        animatedView.backgroundColor = [UIColor redColor];

        [self.view addSubview:animatedView];

        [UIView animateWithDuration:2.f
                              delay:i * 0.5f
                            options:UIViewAnimationCurveLinear
                         animations:^{
                             animatedView.frame = endFrame;
                         } completion:^(BOOL finished) {
                             [animatedView removeFromSuperview];
                         }];
    }
}

It simply creates small squares from top of the screen and moves to the bottom. 它只是从屏幕顶部创建一个小方块,然后移到底部。 I also have an UIImageView which is controlled by the accelerometer on the x axis. 我也有一个UIImageView,它由x轴上的加速度计控制。 The aim is not touching the animated object. 目的是不触摸动画对象。 like a simple race game. 就像一个简单的赛车游戏。 However I could not find out how to detect collision between the imageView and the animation? 但是我找不到如何检测imageView和动画之间的冲突?

First you need to save all the animated view objects in an array to check the collision with the image view. 首先,您需要将所有动画视图对象保存在一个数组中,以检查与图像视图的冲突。 so in you .h file create an NSArray to hold the animated views 因此,在您的.h文件中创建一个NSArray来保存动画视图

NSMutableArray animatedViews;

then in viewDidLoad initialize it 然后在viewDidLoad中初始化它

animatedViews = [[NSMutableArray alloc] init];

then change you code viewWillAppear and add a line 然后更改您的代码viewWillAppear并添加一行

[self.view addSubview:animatedView];
[animatedViews addObject:animatedView];

then create a function to check the collision, it needs a parameter for the scheduled timer 然后创建一个函数来检查冲突,它需要一个计划计时器的参数

-(void) checkCollision: (NSTimer *) theTimer{
    for(int i = 0; i < [animatedViews count]; i++) {
        UIView *theView = [animatedViews objectAtIndex:index];
        if (CGRectIntersectsRect(theView.frame, yourImageView.frame) {
           // the image collided
           // stop the timer and do your work here
        }
    }
}

then in viewWillAppear add this line to schedule the timer to call after every half a second to call the function to check collision 然后在viewWillAppear中添加此行以安排计时器每半秒调用一次以调用函数以检查冲突

[NSTimer scheduledTimerWithTimeInterval: 0.5  
                             target: self  
                           selector: @selector(checkCollision:)  
                           userInfo: nil  
                            repeats: YES];

the problem is in this test : if (CGRectIntersectsRect(theView.frame, yourImageView.frame) you must check the presentationLayer of the view : 问题是在此测试中: if (CGRectIntersectsRect(theView.frame, yourImageView.frame) ,则必须检查视图的presentationLayer:

CGRect movingFrame = [[theImage.layer presentationLayer] frame];
    if (CGRectIntersectsRect(movingFrame, panier.frame)) {           
        // the image collided            
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM