11-17-2010, 03:41 AM
#1 (permalink )
Registered Member
Join Date: Oct 2010
Posts: 36
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
11-17-2010, 04:40 AM
#2 (permalink )
Registered Member
Join Date: Oct 2009
Posts: 168
Check out the PageControl Sample from Apple. Everything is described there in detail.
11-17-2010, 05:09 AM
#3 (permalink )
Registered Member
Join Date: Oct 2010
Posts: 36
Quote:
Originally Posted by
Grinarn
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 .
11-23-2010, 09:35 AM
#4 (permalink )
Registered Member
Join Date: Oct 2010
Posts: 36
Anybody? I'm sure there are lots of people on here who can point me to the right direction?
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80