well here is my code :
- (CGPoint)randomPointSquare {
CGRect frame = [[self view] frame];
//CGFloat rand_x = ((float)arc4random() / (float)UINT_MAX) * (float)_window.frame.size.width;
NSInteger side = arc4random() / (UINT_MAX/4);
CGFloat offset = 0;
switch(side) {
case 0: /* top */
offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.width;
return CGPointMake(offset, -10);
case 1: /* bottom */
offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.width;
return CGPointMake(offset, frame.size.height-150);
case 2: /* left */
offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.height;
return CGPointMake(-10, offset);
default:
case 3: /* right */
offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.height;
return CGPointMake(frame.size.width+170, offset);
}
}
-(void)createNewImageView {
UIImage * image = [UIImage imageNamed:@"abouffer_03.png"];
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];
}
SO as you can see there is a UIView animation, but now I want to change and replace it by a Timer. But I don’t know how to do it . Well, if you know how to do it, don’t forget that I want that the imageView is created at a random position and then it moves to the center of the screen. thank you
Rather than writing all the code for you, I’ll give you some pointers that might help you change it (the best way of learning, I think).
First, when you create the position in the call to
randomPointSqaure, store the point created as an instance variable.Then set the location of the imageView to be this
Calculate how far the object is from the point you want it to be (240, 160) and divide that by the amount of time you want it to move over. For example, if your location is (0, 160) then it will be travelling 240 pixels. If you want it over 10 seconds, it will be 24 pixels per second. Save this in an instance variable as the “delta”
Now create a timer that calls a method to update the position
And then create the
updatePositionmethodThis should hopefully give you some ideas on how you can do it with a Timer instead of animations. This is how a game would perform the same thing – you just update the positions of the objects in small little steps and then check for collisions etc each time.