首页 > 代码库 > 新浪微博客户端(9)-实现版本新特性的ViewPager
新浪微博客户端(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
最终效果:
新浪微博客户端(9)-实现版本新特性的ViewPager
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。