两幅图像碰撞问题
这是我的代码:
-(void)collision {
if(CGRectIntersectsRect(ball.frame,center.frame)) {
center.alpha=0.1;
}
}
-(void)viewDidLoad {
[super viewDidLoad];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:7.0f];
[ball setCenter:CGPointMake(200, 100)];
[UIView commitAnimations];
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(collision) userInfo:nil repeats:YES];
}
我的问题是,当viewDidLoad“center.alpha=0.1”但“center”和“ball”尚未碰撞时,我不知道为什么,我认为这是由于动画。
Here is my code :
-(void)collision {
if(CGRectIntersectsRect(ball.frame,center.frame)) {
center.alpha=0.1;
}
}
-(void)viewDidLoad {
[super viewDidLoad];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:7.0f];
[ball setCenter:CGPointMake(200, 100)];
[UIView commitAnimations];
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(collision) userInfo:nil repeats:YES];
}
My problem is that when viewDidLoad "center.alpha=0.1" but "center" and "ball" have not collided yet , I don't know why, I think it is due to the animation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管动画需要 7 秒,但
[ball setCenter:CGPointMake(200, 100)];
会立即设置,因此- (void)collision
可能会将您的 alpha 设置为动画中“球”与“中心”相交之前的 0.1。您可以使用 NSTimer 来慢慢改变“球”的中心,而不是 UIView 动画。
Although the animation takes 7 seconds,
[ball setCenter:CGPointMake(200, 100)];
is set immediately and because of that- (void)collision
probably sets your alpha to 0.1 before "ball" intersects with "center" in the animation.Instead of UIView animations you could use a NSTimer to slowly change the center of "ball".
您计划在 viewDidLoad 结束时执行该行 0.01 秒后调用碰撞。但视图尚未显示,因此显示视图可能需要超过 0.01 秒的时间。
尝试
viewDidAppear
话虽如此,我认为您还不清楚 iOS 中动画的用途。这些不是用于计算碰撞检测 - 它们只是用于在设定的时间内将物体从起点移动到终点。我建议你阅读有关动画系统的 Apple 文档。
You are scheduling the call to collision 0.01 seconds after the line is executed at the end of viewDidLoad. But the view hasn't been displayed yet and so it could take longer than 0.01 seconds to display the view.
Try
viewDidAppear
Having said that, I think you are not clear on the purpose of the animtions in iOS. These are not for calculating collision detection - they are just for moving things from a start point to an end point over a set time. I would suggest you read the Apple docs on the animation system.