UIScrollView 与 UIPageView 的联合使用
{
UIScrollView * scrollView;
UIPageControl * pageControl;
// BOOL pageControlIsChangingPage;
NSMutableArray * images;
}
//- (void)changePage:(id)sender;
- (void)setupPage;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor blackColor];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 410, 320, 36)];
images = [NSMutableArray arrayWithObjects:@"h1.jpeg",@"h2.jpeg",@"h3.jpeg",@"h4.jpeg",@"h5.jpeg",@"h6.jpeg",@"h7.jpeg",@"h8.jpeg", nil];
[self.view addSubview:scrollView];
[self.view addSubview:pageControl];
[self setupPage];
}
////多余的方法
//- (void)changePage:(id)sender
//{
//
// NSLog(@"%s",__func__);
// CGRect frame = scrollView.frame;
//
// frame.origin.x = frame.size.width * pageControl.currentPage;
// frame.origin.y = 0;
//
// [scrollView scrollRectToVisible:frame animated:YES];
//
// pageControlIsChangingPage = YES;
//
//}
- (void)setupPage
{
NSLog(@"%s",__func__);
scrollView.delegate = self;
[scrollView setBackgroundColor: [UIColor blackColor]];
//
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite ;
// //
// scrollView.clipsToBounds = YES;
//
// scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
// //
// scrollView.directionalLockEnabled = YES;
//
//隐藏滚动条
scrollView.alwaysBounceVertical = NO;
scrollView.alwaysBounceHorizontal = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
NSInteger nimages = 0;
CGFloat cx = 0 ;//定义下一幅图片的的坐标
//循环导入图片
for(NSString * imagePath in images)
{
// UIImageView * imageView = [[[UIImageView alloc] initWithFrame:CGRectZero]autorelease];
//
// [imageView setBackgroundColor: [UIColor colorWithRed:.6 green:.6 blue:.6 alpha:1.0]];
//
// UIImage * image = [UIImage imageNamed:imagePath];
//
// [imageView setImage:image];
UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imagePath]];
CGRect rect = scrollView.frame;
rect.size.height = scrollView.frame.size.height;
rect.size.width = scrollView.frame.size.width;
rect.origin.x = cx;
rect.origin.y = 0;
imageView.frame = rect;
imageView.contentMode = UIViewContentModeScaleAspectFill;
[scrollView addSubview:imageView];
cx += scrollView.frame.size.width;
nimages ++;
}
// //不必要的事件
// [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
pageControl.currentPage = 0;
pageControl.numberOfPages = nimages;
pageControl.tag = 0;
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}
//scrollViewkai
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
NSLog(@"%s",__func__);
// if(pageControlIsChangingPage)
// {
// return;
// }
//画面拖动超过百分之五十进行切换
//返回page的值
CGFloat pageWidth = _scrollView.frame.size.width;
//函数名: floor 功 能: 返回小于或者等于指定表达式的最大整数
//函数名: ceil 功 能: 返回大于或者等于指定表达式的最小整数
int page = floor((_scrollView.contentOffset.x - pageWidth/2)/pageWidth)+1;
pageControl.currentPage = page;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"%s",__func__);
// pageControlIsChangingPage = NO;
}
- (void)dealloc
{
[images release];
[scrollView release];
[pageControl release];
[super dealloc];
}