UIPageControl UIScrollView 相关代码 (今日所学)
/*
Erica Sadun, http://ericasadun.com
iPhone Developer's Cookbook, 3.0 Edition
BSD License, Use at your own risk
*/
#import <UIKit/UIKit.h>
#define COOKBOOK_PURPLE_COLOR[UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
#define RSTRING(X) NSStringFromCGRect(X)
#define BASEHEIGHT284.0f
#define NPAGES3
@interface TestBedViewController : UIViewController <UIScrollViewDelegate>
{
IBOutlet UIPageControl *pageControl;
UIScrollView *sv;
}
@end
@implementation TestBedViewController
- (void) pageTurn: (UIPageControl *) aPageControl
{
int whichPage = aPageControl.currentPage;
[UIViewbeginAnimations:nilcontext:NULL];
[UIViewsetAnimationDuration:0.3f];
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
sv.contentOffset = CGPointMake(320.0f * whichPage, 0.0f);
[UIViewcommitAnimations];
}
- (void) scrollViewDidScroll: (UIScrollView *) aScrollView
{
CGPoint offset = aScrollView.contentOffset;
pageControl.currentPage = offset.x / 320.0f;
}
- (void) viewDidLoad
{
self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
self.title = @"Image Scroller";
// Create the scroll view and set its content size and delegate
sv = [[[UIScrollViewalloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, BASEHEIGHT)] autorelease];
sv.contentSize = CGSizeMake(NPAGES * 320.0f, sv.frame.size.height);
sv.pagingEnabled = YES;
sv.delegate = self;
// Load in all the pages
for (int i = 0; i < NPAGES; i++)
{
NSString *filename = [NSStringstringWithFormat:@"image%d.png", i+1];
UIImageView *iv = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:filename]];
iv.frame = CGRectMake(i * 320.0f, 0.0f, 320.0f, BASEHEIGHT);
[svaddSubview:iv];
[iv release];
}
[self.viewaddSubview:sv];
pageControl.numberOfPages = 3;
pageControl.currentPage = 0;
[pageControl addTarget:selfaction:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];
}
pageControl 设置颜色 如下
[[UIPageControlappearance] setCurrentPageIndicatorTintColor:[UIColorcolorWithRed:0 green:175.0/255.0 blue:176.0/255.0 alpha:1.0]];
//未选中状态颜色
[[UIPageControlappearance] setPageIndicatorTintColor:[UIColorgrayColor]];
浙公网安备 33010602011771号