作者:朱克锋
邮箱:zhukefeng@iboxpay.com
转载请注明出处:http://blog.youkuaiyun.com/linux_zkf
页面滚动指示器是IOS应用中随处可见的分页方式,如拉卡拉、盒子支付等应用的首页就是这种页面指示器和其它方式相结合的表现,下面我给出一个简单的页面指示器的实现代码,供大家一些学习
头文件声明:
#import <UIKit/UIKit.h>
@interface PageTestViewController :UIViewController <UIScrollViewDelegate>
{
IBOutlet UIScrollView *scrollView;
UIPageControl *pageControl;
}
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
@end
部分关键代码:
@synthesize scrollView,pageControl;
- (void)viewDidLoad
{
[superviewDidLoad];
scrollView = [[[UIScrollViewalloc] initWithFrame:CGRectMake(0.0f,0.0f, 320.0f,400.0f)] autorelease];
scrollView.contentSize =CGSizeZero;
scrollView.pagingEnabled =YES;
scrollView.delegate =self;
[self.viewaddSubview:scrollView];
for (int i = 0; i < 3; 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, 400.0f);
[scrollViewaddSubview:iv];
[iv release];
}
[self.viewaddSubview:scrollView];
pageControl.numberOfPages =3;
pageControl.currentPage =0;
[pageControladdTarget:selfaction:@selector(pageScroll::)forControlEvents:UIControlEventValueChanged];
}
- (void) pageScroll: (UIPageControl *) pctrl
{
int whichPage = pctrl.currentPage;
[UIViewbeginAnimations:nilcontext:NULL];
[UIViewsetAnimationDuration:0.3f];
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
scrollView.contentOffset =CGPointMake(320.0f * whichPage, 0.0f);
[UIViewcommitAnimations];
}
- (void) scrollViewDidScroll: (UIScrollView *) aScrollView
{
CGPoint offset = aScrollView.contentOffset;
pageControl.currentPage = offset.x /320.0f;
}