两个图像碰撞,声明问题

发布于 2024-11-17 15:06:07 字数 885 浏览 3 评论 0原文

我有一个带有图像“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清旖 2024-11-24 15:06:07

实际上,当您在线设置 imageView 的框架时,我刚刚经历过类似的问题

   [imageView setCenter:CGPointMake(240, 160)];   

,imageView 在内存中的该点上得到解决,只是由于处于动画块中,它向您显示 imageView 正在移动的过渡朝向目的地,但实际上视图的坐标是与目的地位置一起分配的,您可以通过在代码行之后记录坐标来确认它。如果您确实需要以类似的方式执行此操作,您可以使用计时器而不是动画块并为自己设置动画。但我通过使用animationDelegate 规避了这个问题。只需将动画委托设置为 self 并定义animationDidStopSelector 即可。当动画结束时,animationDidstopSelector 将被触发,因此对象到达其最终目的地,或者您可以说存在碰撞(在 UI 上)。希望有帮助
这是一个代码示例:

- (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 setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:)];
[UIView setAnimationDuration:8.0f];
[imageView setCenter:CGPointMake(240, 160)];
[UIView commitAnimations];
[imageView release];

} 

您的animationDidStopSelector将类似于:

-(void)animationDidStop:(id)sender {  
      //This is almost similar to collision detection, you can do something here 
}

I just'd been through a similar problem actually when you set the frame of the imageView on line

   [imageView setCenter:CGPointMake(240, 160)];   

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:

- (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 setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:)];
[UIView setAnimationDuration:8.0f];
[imageView setCenter:CGPointMake(240, 160)];
[UIView commitAnimations];
[imageView release];

} 

your animationDidStopSelector will be just like:

-(void)animationDidStop:(id)sender {  
      //This is almost similar to collision detection, you can do something here 
}
心意如水 2024-11-24 15:06:07

imageView 是函数 createNewImageView 的局部变量,因此

如果您已将 imageView 声明为一个 IBOutlet
你可以这样设置图像

UIImage *image = [UIImage imageNamed:@"abouffer_03.png"];
imageView.image = image

imageView is a local variable to the function createNewImageView and hence cannot be accessed by myRunloop

If you have declared imageView as an IBOutlet
you can set the image like this

UIImage *image = [UIImage imageNamed:@"abouffer_03.png"];
imageView.image = image
面如桃花 2024-11-24 15:06:07

在接口(.h 文件)中声明如下

UIImageView *imageView;

并在您的 createNewImageView() 方法中。使用

imageView = [[UIImageView alloc] initWithImage:image];

In Interface (.h file) declare like this

UIImageView *imageView;

And In your createNewImageView() method.Use the

imageView = [[UIImageView alloc] initWithImage:image];
把人绕傻吧 2024-11-24 15:06:07

您可以为图像视图分配一个标签,以便稍后可以在其他方法中找到它:

[imageView setTag:1];

// in myRunloop
UIImageView* imageView = [[self view] viewWithTag:1];

You can assign a tag to the image view so you can find it later in the other method:

[imageView setTag:1];

// in myRunloop
UIImageView* imageView = [[self view] viewWithTag:1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文