IOS 滚动页面UIPageControl实例

作者:朱克锋

邮箱:[email protected]

转载请注明出处:http://blog.csdn.net/linux_zkf


#define BASEHEIGHT 300.0f

#define NPAGES 3


@interface TestViewController : UIViewController <UIScrollViewDelegate>

{

IBOutlet UIPageControl *pageControl;

UIScrollView *sv;

}

@end


@implementation TestViewController

- (void) pageTurn: (UIPageControl *) aPageControl

{

int whichPage = aPageControl.currentPage;

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:0.3f];

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

sv.contentOffset = CGPointMake(320.0f * whichPage, 0.0f);

[UIView commitAnimations];

}

//UIScrollView代理方法

- (void) scrollViewDidScroll: (UIScrollView *) aScrollView

{

CGPoint offset = aScrollView.contentOffset;

pageControl.currentPage = offset.x / 320.0f;

}


- (void) viewDidLoad

{

// 创建UIScrollView ,设置代理

sv = [[[UIScrollView alloc] 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;

// 加载图片页面

for (int i = 0; i < NPAGES; i++)

{

NSString *filename = [NSString stringWithFormat:@"test%d.png", i+1];

UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]];

iv.frame = CGRectMake(i * 320.0f, 0.0f, 320.0f, BASEHEIGHT);

[sv addSubview:iv];

[iv release];

}

[self.view addSubview:sv];

pageControl.numberOfPages = 3;

pageControl.currentPage = 0;

[pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];

}

你可能感兴趣的:(ios,null,interface)