create uiimageView near and outside the screen sides
Hi everyone I'm french so scuse me for my english. well what I want to do is to create every second a new image at a开发者_C百科 random position, but a random position outside the screen, and(very important) near of the screen sides(the upper side or left side or right or down). like that when I want to animate them, there will be created outside the screen but just around the sides. How can I do this please ?
So basically, my understanding about your question is that you one goal: You want to animate an object via translation. So the animation goes from outside the visible screen to the viewable.
Here's an approach: 1. You need to determine what are the limits of your "Outside" screen. Depending on the size of your UIImageView frame, 100 px padding is fine I think. So that would be x: (-100, 420), y: (-100, 580).
Generate random number. arc4random() is better than rand() in this case.
Create a method that will be called by an NSTimer in which the UIImageView's are created.
Don't forget to properly manage your UIImageView's. Frequent creating objects without releasing will cause your app to crash. If possible, reuse them instead of creating them each time the method is called.
What this code does is use random:min:max
to generate a random x and y value between the desired min and max range. In the onTimer
function we allocate a new UIImageView
object and then set its center property to be a random location off the screen. If the generated random number inside the if statment is less than 5 then the view will be added to either to the left or top of the screen, otherwise it will be added to the right or bottom.
/*****************************************************************************
Generates a pseudo random CGPoint point value within the min and max range.
*****************************************************************************/
-(CGFloat)rand:(CGFloat)min:(CGFloat)max{
float difference = max - min;
return (((CGFloat) rand()/(CGFloat)RAND_MAX) * difference) + min;
}
-(void)viewDidLoad{
//setup a timer property in your view controller header file
timer = [NSTimer timerWithTimeInterval:1.0
target:self
selector:@selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
-(void)onTimer{
UIImagView *myRandomImageView;
myRandomImageView = [[UIImageView alloc] initWithImage:@"MyRandomImage.png"];
if([rand:1:10] > 5)
//put the image view to top or left side of the screen
myRandomImageView.center = CGPointMake([rand:-100,-200], [rand:-100,-200]);
else
//put the image view to the bottom or right side of the screen
myRandomImageView.center = CGPointMake([rand:420,520], [rand:580,680]);
//dont forget that the values I have eneter here to be passed to the rand:min:max
//function are meant to serve as a guide and that you should enter your own values to
//achieve the desired random view positioning off screen.
[self.view addSubview:myRandomImageView];
[myRandomImageView release]
}
精彩评论