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-17-2010, 03:41 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 36
svenJ is on a distinguished road
Default Connect scrollview with pageControl

Hello,

I've created a scrollView in which a user browse through images. It snaps like I want to because I enabled setPagingEnabled. I also added a pageControl but this doesn't interact with my scrollview (yet). I don't know how to do this. If somebody can point me in the right direction that would be much appreciated

Below is the code I have until now:

games.h
Code:
#import <UIKit/UIKit.h>

@interface Games : UIViewController {
	UIScrollView *scrollView;
	UIImageView *imageView;
	UIPageControl *pageControl;
}

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIPageControl *pageControl;

@end
games.m
Code:
#import "Games.h"

@implementation Games

@synthesize scrollView, imageView, pageControl;

- (void) loadView {
	UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"]];
	self.imageView = tempImageView;
	
	UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	view.autoresizesSubviews = YES;
	self.view = view;
	[view release];

	int aantalPaginas = 3;
	int totaleBreedte = aantalPaginas * 320;
	
	[self.scrollView removeFromSuperview];
	scrollView = [[UIScrollView alloc] init];
	scrollView.frame = CGRectMake(0, 45, 320, 346);
	[scrollView setContentSize:CGSizeMake(totaleBreedte, 346)];
	[scrollView setPagingEnabled:YES];
    [scrollView setShowsHorizontalScrollIndicator:NO];
	[scrollView setShowsVerticalScrollIndicator:NO];
	[scrollView setScrollsToTop:NO];
	[self.view addSubview:scrollView];
	
	[self.scrollView addSubview:imageView];
	
	pageControl = [[UIPageControl alloc] init];
	pageControl.frame = CGRectMake(0, 390, 320, 15);
	pageControl.numberOfPages = 3;
	pageControl.currentPage = 0;
	[self.view addSubview:pageControl];
}

- (void)viewDidLoad {
	[super viewDidLoad];

	UINavigationBar *navBar= [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
	navBar.tintColor = [UIColor blackColor];
	
	UILabel *lblTitle  = [[UILabel alloc]initWithFrame:CGRectMake(0,-1,320,43)];
	lblTitle.text = @"Master Games";
	lblTitle.backgroundColor = [UIColor clearColor];
	lblTitle.font = [UIFont boldSystemFontOfSize:20.0];
	lblTitle.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
	lblTitle.textAlignment = UITextAlignmentCenter;
	lblTitle.textColor =[UIColor whiteColor];
	[navBar addSubview: lblTitle];
	[lblTitle release];
	
	[self.view addSubview:navBar];
    [navBar release];							   
}

- (void)dealloc {
	[super dealloc];
	[pageControl release];
	[scrollView release];
	[imageView release];
}

@end
I don't think it's needed but just in case; here's the appDelegate:

.h:
Code:
#import <UIKit/UIKit.h>

@interface JVHGamesAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
	UITabBarController *tabBarController;
	
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end
.m
Code:
#import "JVHGamesAppDelegate.h"

@implementation JVHGamesAppDelegate

@synthesize window, tabBarController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
	[application setStatusBarStyle:UIStatusBarStyleBlackOpaque];
	[window addSubview:[tabBarController view]];
	
    [window makeKeyAndVisible];
}

- (void)dealloc {
    [window release];
    [super dealloc];
}

@end
THANKS IN ADVANCE!!!
ps: I'd like and try to do as much code, if not all, in xcode without the use if IB. I'm also learning and it'll be a great way
svenJ is offline   Reply With Quote
Old 11-17-2010, 04:40 AM   #2 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 168
Grinarn is on a distinguished road
Default

Check out the PageControl Sample from Apple. Everything is described there in detail.
__________________
DiabetesPlus - Diabetes-Management for the iPhone/iPod (Website)
InsulinPlus - Insulin-dosage-calculator for the iPhone/iPod
Grinarn is offline   Reply With Quote
Old 11-17-2010, 05:09 AM   #3 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 36
svenJ is on a distinguished road
Default

Quote:
Originally Posted by Grinarn View Post
Check out the PageControl Sample from Apple. Everything is described there in detail.
I did, I just don't get it working..

I had to rebuild a bit to make it fit in my app and what I have is:

games.h
Code:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface Games : UIViewController {
	UIScrollView *scrollView;
	UIImageView *imageView;
	UIPageControl *pageControl;

	BOOL pageControlUsed;
}

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIPageControl *pageControl;

- (IBAction)changePage:(id)sender;

@end
games.m
Code:
#import "Games.h"

@interface Games (PrivateMethods)
- (void)scrollViewDidScroll:(UIScrollView *)sender;
@end

@implementation Games

@synthesize scrollView, imageView, pageControl;

- (void) loadView {
	UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"]];
	self.imageView = tempImageView;
	
	UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	view.autoresizesSubviews = YES;
	self.view = view;
	[view release];

	int aantalPaginas = 11;
	int totaleBreedte = aantalPaginas * 320;
	
	[self.scrollView removeFromSuperview];
	scrollView = [[UIScrollView alloc] init];
	scrollView.frame = CGRectMake(0, 45, 320, 346);
	[scrollView setContentSize:CGSizeMake(totaleBreedte, 346)];
	[scrollView setPagingEnabled:YES];
    [scrollView setShowsHorizontalScrollIndicator:NO];
	[scrollView setShowsVerticalScrollIndicator:NO];
	[scrollView setScrollsToTop:NO];
	[self.view addSubview:scrollView];
	
	[self.scrollView addSubview:imageView];
	
	pageControl = [[UIPageControl alloc] init];
	pageControl.frame = CGRectMake(0, 390, 320, 15);
	
	pageControl.numberOfPages = aantalPaginas;
	pageControl.currentPage = 0;
	
	[self.view addSubview:pageControl];
}

- (void)viewDidLoad {
	[super viewDidLoad];

	UINavigationBar *navBar= [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
	navBar.tintColor = [UIColor blackColor];
	
	UILabel *lblTitle  = [[UILabel alloc]initWithFrame:CGRectMake(0,-1,320,43)];
	lblTitle.text = @"Master Games";
	lblTitle.backgroundColor = [UIColor clearColor];
	lblTitle.font = [UIFont boldSystemFontOfSize:20.0];
	lblTitle.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
	lblTitle.textAlignment = UITextAlignmentCenter;
	lblTitle.textColor =[UIColor whiteColor];
	[navBar addSubview: lblTitle];
	[lblTitle release];
	
	[self.view addSubview:navBar];
    [navBar release];							   
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    if (pageControlUsed) {
        return;
    }
    
    // Switch the indicator when more than 50% of the previous/next page is visible
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    pageControlUsed = NO;
}

- (IBAction)changePage:(id)sender {
    int page = pageControl.currentPage;
    
    // update the scroll view to the appropriate page
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [scrollView scrollRectToVisible:frame animated:YES];
    
    // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
    pageControlUsed = YES;
}

- (void)dealloc {
	[super dealloc];
	[pageControl release];
	[scrollView release];
	[imageView release];
}

@end
I'm not getting any errors. It just doesn't work.. Any tips?
He doesn't call the changePage function. I know because I build a NSLog(@"hello"); and I'm not getting anything in my console.. I assumed this function is part of the pageControl, and that it's called whenever you tap on to the left or right of the bullets. But I get a feeling I might be mistaken

Last edited by svenJ; 11-17-2010 at 05:13 AM.
svenJ is offline   Reply With Quote
Old 11-23-2010, 09:35 AM   #4 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 36
svenJ is on a distinguished road
Default

Anybody? I'm sure there are lots of people on here who can point me to the right direction?
svenJ is offline   Reply With Quote
Reply

Bookmarks

Tags
connect, pagecontrol, scrollview, view

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: 376
7 members and 369 guests
apatsufas, Kirkout, lzwasyc, MarkC, Sami Gh, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

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