新浪微博客户端(9)-实现版本新特性的ViewPager
"DJNewFeatureViewController.m"
#import "DJNewFeatureViewController.h" #define NEW_FEATURE_NUMS 4 @interface DJNewFeatureViewController() <UIScrollViewDelegate> @property (nonatomic,weak) UIPageControl *pageControl; @end @implementation DJNewFeatureViewController - (void)viewDidLoad { // 1.创建一个UIScrollView UIScrollView *scrollView = [[UIScrollView alloc] init]; scrollView.frame = self.view.bounds; CGFloat scrollW = scrollView.width; CGFloat scrollH = scrollView.height; scrollView.delegate = self; // 2.设置scrollview的属性 // contentSize设置内容范围,高度为0代表高度不能滑动 scrollView.contentSize = CGSizeMake(scrollW * NEW_FEATURE_NUMS, 0); // 设置scrollView可以被分页 scrollView.pagingEnabled = YES; // 去除水平滚动条 scrollView.showsHorizontalScrollIndicator = NO; // 去除弹簧效果造成的黑边 scrollView.bounces = NO; // 2.添加4个UIImageView for (int i = 0; i < NEW_FEATURE_NUMS; i++) { UIImageView *imageView = [[UIImageView alloc] init]; imageView.size = scrollView.size; imageView.y = 0; imageView.x = i * scrollW; NSString *imageName = [NSString stringWithFormat:@"new_feature_%d",i+1]; imageView.image = [UIImage imageNamed:imageName]; [scrollView addSubview:imageView]; } [self.view addSubview:scrollView]; // 3.添加pageControl UIPageControl *pageControl = [[UIPageControl alloc] init]; // 设置pageControl的个数 pageControl.numberOfPages = NEW_FEATURE_NUMS; pageControl.width = 100; pageControl.height = 40; pageControl.centerX = scrollW * 0.5; pageControl.centerY = scrollH - 50; // 设置pageControl选中和未选中的颜色 pageControl.pageIndicatorTintColor = DJColor(189, 189, 189); pageControl.currentPageIndicatorTintColor = DJColor(253,98, 42); // 禁止点击pagecontrol的小按钮 pageControl.userInteractionEnabled = NO; [self.view addSubview:pageControl]; self.pageControl = pageControl; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { double page = scrollView.contentOffset.x / scrollView.width; self.pageControl.currentPage = (int)(page + 0.5); // DJLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset)); } @end
最终效果: