Quote:
Originally Posted by Anonymous Username
I want to be able to slide an image across the bottom of the screen, and have it keep repeating to slide across the screen so you always see it... if that makes any sense. I think explaining how I've done it will help with what I'm trying to say.
In interface builder I have two imageviews both the same image and size, one that fills up the button of the screen and one left of the screen. I have a timer that moves both the images right. When the the first image leaves the screen I have another timer sending it to where the second image originally was.
And that's as far as I've got. There's a delay when the two images slide and it looks weird, also it only works for like two minutes and it kinda breaks (thats because I don't have another timer moving the second image back) I want it to look seemless which it definitely doesn't look, is there a better way to do this?
|
Yes, there's a much better way to do it. Use Core Animation.
Add an integer instance variable "step" to your class.
Then you could use code like this, which uses the UIView class method animateWithDuration:delay

ptions:animations:compl etion: to animate both views smoothly across the screen from left to right, then switch the view that's now off-screen on the right back off-screen to the left and start the process over.
The code below uses a trick of incrementing a counter, then taking the remainder of dividing it by 2.
The code
step = (step + 1) % 2;
Causes the value of step to go 0, 1, 0, 1, 0, 1... forever.
It puts the two image views into a C-style array, and indexes into the array so that it can alternate between image views easily:
Code:
- (void) animateViews;
{
CGPoint centerPoint;
UIImageView* banners[2];
banners[0] = imageView1;
banners[1] = imageView2;
//Put one of the banners centered on screen to start.
banners[step].center = CGMakePoint(self.view.center.x,
banners[step].center.y);
//switch to the other banner (alternate even/odd indexes)
step = (step+1) %2;
//Put the other banner off-screen to the left, ready to be shifted on-screen.
banners[step].center = CGMakePoint(self.view.center.x -
self.view.bounds.width,
banners[step].center.y);
[UIView animateWithDuration: 1.0
delay: 0.0
options: UIViewAnimationOptionCurveLinear
animations: ^
{
//Animate both views to the right by the whole screen width
//Once the animation is done, one view will be off-screen to
//the right, and the other will be fully on-screen.
banners[step].center = CGMakePoint(banners[step].center +
self.view.bounds.width,
banners[step].center.y);
//switch to the other banner (alternate even/odd indexes)
step = (step+1) %2;
banners[step].center = CGMakePoint(banners[step].center +
self.view.bounds.width,
banners[step].center.y);
//Switch indexes an odd number of times, so the next loop puts the other banner on-screen
step = (step+1) %2;
}
completion: ^(BOOL finished)
{
//Once the animation is finished, call our method again.
[self animateViews];
}];
}
To use this code, make both image views as wide as the screen and position them at the bottom. You can put them both on top of each other with an x position of zero. The animation code will shift them into position the first time it's run.
Disclaimer: I banged this code out in about 5 minutes, and did not even compile it, much less test it. It's intended as an illustration, not as working code. You'll likely need to fix a couple of typos, and also adapt it to your needs.