Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-06-2011, 02:56 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 30
Anonymous Username is on a distinguished road
Default Not sure what to call it, but I could use help with it haha

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?
Anonymous Username is offline   Reply With Quote
Old 11-06-2011, 07:47 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Anonymous Username View Post
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:delayptions: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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 11-06-2011 at 07:49 PM.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 405
17 members and 388 guests
7twenty7, Alex-alex, Apptronics RBC, baja_yu, dre, gwelmarten, ipodphone, jeroenkeij, jleannex55, matador1978, mbadegree, n00b, pbart, reficul, Retouchable, Sami Gh, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:22 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0