两个图像碰撞,声明问题
我有一个带有图像“imageView”的方法:
- (void)createNewImageView {
// Get the view's frame to make it easier later on
UIImage *image = [UIImage imageNamed:@"abouffer_03.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// Add it at a random point
[imageView setCenter:[self randomPointSquare]];
[[self view] addSubview:imageView];
// Animate it into place
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:8.0f];
[imageView setCenter:CGPointMake(240, 160)];
[UIView commitAnimations];
[imageView release];
}
和另一个图像“viewToRotate”(在.h和界面生成器中定义的IBOutlet)
我想检查与此方法的冲突:
- (void)myRunloop
{
// check collision
if( CGRectIntersectsRect(imageView.frame, viewToRotate.frame) )
{
viewToRotate.alpha=0.2;
}
}
但xcode总是给我错误:“imageView未声明”我不知道如何解决这个问题。我不想在这个方法中再次定义它。
I have a method with an image "imageView":
- (void)createNewImageView {
// Get the view's frame to make it easier later on
UIImage *image = [UIImage imageNamed:@"abouffer_03.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// Add it at a random point
[imageView setCenter:[self randomPointSquare]];
[[self view] addSubview:imageView];
// Animate it into place
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:8.0f];
[imageView setCenter:CGPointMake(240, 160)];
[UIView commitAnimations];
[imageView release];
}
and another image "viewToRotate" (IBOuutlet defined in .h and interface builder)
And I want to check the collision with this method :
- (void)myRunloop
{
// check collision
if( CGRectIntersectsRect(imageView.frame, viewToRotate.frame) )
{
viewToRotate.alpha=0.2;
}
}
But xcode always give me the error :"imageView undeclared" and I don't know how to solve this . I don't wanna define it again in this method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上,当您在线设置 imageView 的框架时,我刚刚经历过类似的问题
,imageView 在内存中的该点上得到解决,只是由于处于动画块中,它向您显示 imageView 正在移动的过渡朝向目的地,但实际上视图的坐标是与目的地位置一起分配的,您可以通过在代码行之后记录坐标来确认它。如果您确实需要以类似的方式执行此操作,您可以使用计时器而不是动画块并为自己设置动画。但我通过使用animationDelegate 规避了这个问题。只需将动画委托设置为 self 并定义animationDidStopSelector 即可。当动画结束时,animationDidstopSelector 将被触发,因此对象到达其最终目的地,或者您可以说存在碰撞(在 UI 上)。希望有帮助
这是一个代码示例:
您的animationDidStopSelector将类似于:
I just'd been through a similar problem actually when you set the frame of the imageView on line
the imageView gets settled at that point in the memory, It's just that due to being in animation block it's showing you the transition that imageView is being moved towards the destination but in actual the view's coordinates are assigned with the destination location, you can confirm it by logging coordinates right after the line of code. If you really need to do this the similar way, you can use the timer instead of the animation block and animate yourself. But I circumvent this problem by using the animationDelegate. Just set animation delegate to self and define animationDidStopSelector and you're up. You're animationDidstopSelector will get fired when the animation's ended and hence the object reached it's final destination or you can say there's a collision (on UI). Hope that helps
Here's a code sample:
your animationDidStopSelector will be just like:
imageView
是函数createNewImageView
的局部变量,因此如果您已将
imageView
声明为一个 IBOutlet你可以这样设置图像
imageView
is a local variable to the functioncreateNewImageView
and hence cannot be accessed bymyRunloop
If you have declared
imageView
as anIBOutlet
you can set the image like this
在接口(.h 文件)中声明如下
并在您的
createNewImageView()
方法中。使用In Interface (.h file) declare like this
And In your
createNewImageView()
method.Use the您可以为图像视图分配一个标签,以便稍后可以在其他方法中找到它:
You can assign a tag to the image view so you can find it later in the other method: