开发者

collision of two images, problem with the declaration

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 erro开发者_JAVA百科r :"imageView undeclared" and I don't know how to solve this . I don't wanna define it again in this method.


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 
}


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


In Interface (.h file) declare like this

UIImageView *imageView;

And In your createNewImageView() method.Use the

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


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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜