简体   繁体   中英

iphone programming: collision detection of a UIImageView and an animation

I have an animation in viewDidAppear shown below

-(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. 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?

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

NSMutableArray animatedViews;

then in viewDidLoad initialize it

animatedViews = [[NSMutableArray alloc] init];

then change you code viewWillAppear and add a line

[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

[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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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