我有两张像这样生成的图片
- (void)moveImage:(UIImageView *)image duration:(NSTimeInterval)duration
curve:(int)curve x:(CGFloat)x y:(CGFloat)y
{
// Setup the animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
// The transform matrix
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
image.transform = transform;
// Commit the changes
[UIView commitAnimations];
}
- (void)alien {
enemy =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alien.png"]];
enemy.frame = CGRectMake(-100, 20, 50, 50);
[self.view addSubview:enemy];
// Move the image
[self moveImage:enemy duration:5
curve:UIViewAnimationCurveLinear x:460 y:0.0]; }
- (void)bullet4 {
bullet =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bullet.png"]];
bullet.frame = CGRectMake(carDude.center.x, 380, 5, 10);
[self.view addSubview:bullet];
// Move the image
[self moveImage:bullet duration:3
curve:UIViewAnimationCurveLinear x:0 y:-500.0]; }
我试图在它们相交时隐藏它们。屏幕上可以同时显示很多,但我试过了,
-(void)checkCollision {
if(CGRectIntersectsRect(bullet.frame, enemy.frame)) {
}
}
当它们发生碰撞时,让图像做任何事情都没有运气。
如何让图像隐藏并检查碰撞?
最佳答案
如果您更改 View 的变换,它的框架不会改变(它甚至可能变得无效)并且应该被忽略。
来自documentation :
Warning If this property is not the identity transform, the value of the frame property is undefined and therefore should be ignored.
既然您所做的只是应用平移变换,为什么不为框架设置动画呢?
关于iphone - 检查以编程方式生成的 ImageView 的相交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12340327/