iPhone编程:一个UIImageView和动画的碰撞检测 [英] iphone programming: collision detection of a UIImageView and an animation

查看:195
本文介绍了iPhone编程:一个UIImageView和动画的碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在viewDidAppear动画图所示。

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];
                         }];
    }
}

它只是简单地创建一个从屏幕上方的小方块,并移动到了谷底。
我还具有由在x轴上的加速度控制的的UIImageView。这样做的目的是不是触摸动画的对象。像一个简单的赛车游戏。不过,我无法找出如何在ImageView的和动画之间的碰撞检测?

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?

推荐答案

首先,你需要所有的动画视图对象保存在一个数组检查与图像视图碰撞。所以在你.h文件中创建一个NSArray持有动画意见

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;

再viewDidLoad中初始化

then in viewDidLoad initialize it

animatedViews = [[NSMutableArray alloc] init];

然后改变你code viewWillAppear中,并添加一行

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
        }
    }
}

然后添加viewWillAppear中此行安排定时调用后,每半秒调用函数来检查碰撞

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];

这篇关于iPhone编程:一个UIImageView和动画的碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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