iphone编程:UIImageView和动画的碰撞检测
我在ViewDid中有一个动画,如下所示iphone编程:UIImageView和动画的碰撞检测,iphone,objective-c,ios,animation,collision-detection,Iphone,Objective C,Ios,Animation,Collision Detection,我在ViewDid中有一个动画,如下所示 -(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 = CGRec
-(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];
}];
}
}
-(void)视图显示:(BOOL)动画
{
对于(int i=0;i<100;i++){
p=arc4random_uniform(320)%4+1;//全局整数
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
延迟:i*0.5f
选项:UIViewAnimationCurveLinear
动画:^{
animatedView.frame=结束帧;
}完成:^(布尔完成){
[动画视图从SuperView移除];
}];
}
}
它只是从屏幕顶部创建小正方形,然后移动到底部。
我还有一个UIImageView,它由x轴上的加速计控制。目标不接触已设置动画的对象。就像一个简单的比赛游戏。但是,我无法找到如何检测imageView和动画之间的冲突?首先,您需要将所有动画视图对象保存在一个数组中,以检查与图像视图的冲突。因此,在.h文件中创建一个NSArray来保存动画视图
NSMutableArray animatedViews;
然后在viewDidLoad中初始化它
animatedViews = [[NSMutableArray alloc] init];
然后,“更改代码”视图将出现并添加一行
[self.view addSubview:animatedView];
[animatedViews addObject:animatedView];
然后创建一个函数来检查冲突,它需要一个用于定时计时器的参数
-(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
}
}
}
首先,需要将所有动画视图对象保存在一个数组中,以检查与图像视图的冲突。因此,在.h文件中创建一个NSArray来保存动画视图
NSMutableArray animatedViews;
然后在viewDidLoad中初始化它
animatedViews = [[NSMutableArray alloc] init];
然后,“更改代码”视图将出现并添加一行
[self.view addSubview:animatedView];
[animatedViews addObject:animatedView];
然后创建一个函数来检查冲突,它需要一个用于定时计时器的参数
-(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
}
}
}
此测试中存在问题:
if(CGRectIntersectsRect(theView.frame,yourImageView.frame)
必须检查视图的presentationLayer:
CGRect movingFrame = [[theImage.layer presentationLayer] frame];
if (CGRectIntersectsRect(movingFrame, panier.frame)) {
// the image collided
}
此测试中存在问题:
if(CGRectIntersectsRect(theView.frame,yourImageView.frame)
必须检查视图的presentationLayer:
CGRect movingFrame = [[theImage.layer presentationLayer] frame];
if (CGRectIntersectsRect(movingFrame, panier.frame)) {
// the image collided
}
谢谢你的解决方案。我完全按照你说的做了。我认为这很有意义。但是,没有任何改变。我仍然无法捕捉碰撞。哎呀,我将时间间隔写入了
5.0
(五)秒错误,您是否将其更改为0.5
以在半秒后运行。您需要检查presentationLayer,因为这是在动画期间更新的一个。检查实际的视图帧根本没有帮助,因此此答案是错误的。@您能建议正确的方法吗?谢谢您的解决方案。我这样做了正是你所说的。我认为这很有意义。但是,没有任何改变。我仍然无法捕捉碰撞。哎呀,我将时间间隔写入了5.0
(五)秒,您是否错误地将其更改为0.5
以在半秒后运行。您需要检查presentationLayer,因为这是在动画期间更新的一个。检查实际的视图帧根本没有帮助,因此此答案是错误的。@直到您能建议正确的方法为止?