新浪微博客户端(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
复制代码

 

最终效果:

 



如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
posted @   夜行过客  阅读(445)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示