Flafla2
Flafla2

Reputation: 701

How do I make a side-scrolling iPhone app?

I am working on a side-scrolling iPhone app, and I have every resource, except the actual side scrolling part! How do I make an app so that it side-scrolls? To be more specific, I want the view of the app to scroll when a UIImageview(player) hits the right/left border of the screen.

<:EDIT:>
I am extremely sorry for all of this confusion! All I am making is a SAMPLE iPhone app only to be used in the simulator. In the app, I have a UIImaageview that moves when you touch arrow UIImageviews. PLEASE NOTE THAT ALL INFORMATION SAID BEFORE THIS SENTENCE WORKS PERFECTLY! Now, my problem is that I want the UIView to scroll only ONE picture(this means it doesn't go forever ;)). I have tried using a UIScrollView, but that didn't work. Finally, I am not planning to use Cocos2D or any other game engine besides a basic UIview. This is only a test app, not a coding extravaganza...

Upvotes: 0

Views: 7273

Answers (7)

Jim Hillhouse
Jim Hillhouse

Reputation: 39

If the image being side-scrolled is large, I'd suggest scrolled view while taking advantage of CATiledLayer. See the WWDC10 Session 104 video and the "Tiling" sample code.

BTW, there is nothing in the Apple docs that seem to prejudge the use of scroll views for utility or menus.

Upvotes: 2

willc2
willc2

Reputation: 39671

Start with a view-based application template so you can get the gist of how this works. Later you can integrate with your existing code.

Create a seamless background image 320 x 960 pixels, named "seamless.png"

Add this ivar to your view controller .h file

UIImageView *bg;

@property (nonatomic, retain) UIImageView *bg;

In your .m file add these #defines before the @implementation line, the @synthesize after.

#define kUpdateRate (1.0 / 60.0)
#define kMoveY (3.0)
#define kTopY (0.0)
#define kBottomY (480.0)
@synthesize bg;

Add this method.

-(void)scrollView; {
 float oldY = self.bg.center.y + kMoveY;
 float newY = oldY;

 if (oldY > kBottomY) {
  newY = kTopY;
  }

 self.bg.center = CGPointMake(self.bg.center.x, newY);
}

Add this to your viewDidLoad method.

 self.bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"endless.png"]];
 [self.view addSubview:self.bg];
 self.bg.center = CGPointMake(self.bg.center.x, kTopY);

 [NSTimer scheduledTimerWithTimeInterval:kUpdateRate target:self selector:@selector(scrollView) userInfo:nil repeats:YES];

When you run the app, you should have a nice scrolling background.

To sum up what happens: A timer is set up to repeatedly call scrollView:

There, the image is moved downwards until it reaches a predefined point (kBottomY), at which time its position is jumped back up to the top (kTopY) and it starts over again.

You can customize it to only scroll when you want it to or scroll the other way on your own, right?

Upvotes: 2

kgutteridge
kgutteridge

Reputation: 8981

Take a look at Cocos2D for the iPhone which also includes a choice of 2 collision engines, chipmunk and box2d

I can also recommend the oreilly book iPhone game development, which if you are new to game development will teach you how to roll a basic game framework

Upvotes: 1

Craig
Craig

Reputation: 3297

Just make the main view wider than the screen and change the bounds accordingly to show a different portion of the main view. This assumes your main view does have some kind of a limited width. It's slightly trickier if that view just goes forever. In that case you change the bounds and draw the newly revealed content.

If this doesn't make sense then I think you need to reword your question or do some experimentation to narrow down what you're asking.

Upvotes: 1

Jasarien
Jasarien

Reputation: 58448

If you're writing a game, you may benefit from a lot of the features given to you by engines such as Cocos2D.

Upvotes: 3

Nir Levy
Nir Levy

Reputation: 4740

You should use a UIScollView (http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/3393-uiscrollview-tutorial.html). Or am I missing the point?

Upvotes: 1

Ben Gottlieb
Ben Gottlieb

Reputation: 85532

Are you trying to use a UIScrollView? If so, just set your scrollView's content size to something wider than the screen. For example:

self.scrollView.contentSize = CGSizeMake(640, 480);

Upvotes: 1

Related Questions