As promised, here is a UIScrollView tutorial. I’ve gone ahead and made it extremely simple
. Should be a nice change from some of the other more complicated tutorials I’ve seen online.
As the scroll view we are going to create is very simple, it is perhaps worthwhile to checkout Apple’s documentation on this object so you can go ahead and add your own custom features.
Apple UIScrollView Class Reference
Enjoy the tutorial
. I’ve also attached a copy of the updates I made to the Dapp exported code files so you can just browse the code if you wish.
And just to be extra nice to those that just want to have a quick look at some of the code without downloading or watching anything
.
HomeView.h
// Parent View Controller @class HomeViewController; @interface HomeView : UIView { HomeViewController *refParentViewController; UIScrollView *ourScrollView; } - (id)initWithParentViewController:(HomeViewController *)parent; @end |
HomeView.m
// Header File #import "HomeView.h" // Parent View Controller #import "HomeViewController.h" // Please add the QuartzCore.framework to the project #import <QuartzCore/QuartzCore.h> // Private Methods @interface HomeView() - (void)loadRectangleView1; - (void)loadRectangleView2; - (void)loadRectangleView3; - (void)loadScrollView; @end @implementation HomeView #pragma mark - #pragma mark Memory Management - (void)dealloc { [ourScrollView release]; [super dealloc]; } #pragma mark - #pragma mark Initialization - (id)initWithParentViewController:(HomeViewController *)parent { if ((self = [super init])) { // Update this to initialize the view with your own frame size [self setFrame:CGRectMake(0, 0, 320, 460)]; // Assign the reference back to the parent view controller refParentViewController = parent; // Set the view background color [self setBackgroundColor:[UIColor lightGrayColor]]; // Our load scrollview method [self loadScrollView]; } return self; } #pragma mark - #pragma mark Load Subview Methods - (void)loadScrollView { ourScrollView = [[UIScrollView alloc] init]; // Now to populate and configure our scroll view // 416 is the height of our view that is leftover, after you take away the navigation bar (44 pixels) and status bar (20 pixels) [ourScrollView setFrame:CGRectMake(0, 0, 320, 416)]; // Now we need to add our rectangle views as subviews of our scroll view. [self loadRectangleView1]; [self loadRectangleView2]; [self loadRectangleView3]; // Now we need to set our content size for our scroll view. Which can be any size and is not limited by our scroll view frame size. [ourScrollView setContentSize:CGSizeMake(320, 500)]; [self addSubview:ourScrollView]; } - (void)loadRectangleView1 { UIView *rectangleView1 = [[[UIView alloc] init] autorelease]; [rectangleView1 setBackgroundColor:[UIColor greenColor]]; rectangleView1.layer.cornerRadius = 10; rectangleView1.layer.borderColor = [[UIColor blackColor] CGColor]; rectangleView1.layer.borderWidth = 1; [rectangleView1 setFrame:CGRectMake(110, 37, 100, 100)]; [ourScrollView addSubview:rectangleView1]; } - (void)loadRectangleView2 { UIView *rectangleView2 = [[[UIView alloc] init] autorelease]; [rectangleView2 setBackgroundColor:[UIColor greenColor]]; rectangleView2.layer.cornerRadius = 10; rectangleView2.layer.borderColor = [[UIColor blackColor] CGColor]; rectangleView2.layer.borderWidth = 1; [rectangleView2 setFrame:CGRectMake(110, 196, 100, 100)]; [ourScrollView addSubview:rectangleView2]; } - (void)loadRectangleView3 { UIView *rectangleView3 = [[[UIView alloc] init] autorelease]; [rectangleView3 setBackgroundColor:[UIColor greenColor]]; rectangleView3.layer.cornerRadius = 10; rectangleView3.layer.borderColor = [[UIColor blackColor] CGColor]; rectangleView3.layer.borderWidth = 1; [rectangleView3 setFrame:CGRectMake(110, 356, 100, 100)]; [ourScrollView addSubview:rectangleView3]; } |
To your app success!,
Cliff
P.S. Technology is only a small part of your app success. There is the business, sales and marketing that you need to think of too :). Which is why I also provide a bunch of free education on how to succeed on the App Store - http://kerofrog.com :).
