zoukankan      html  css  js  c++  java
  • iOS-UIScrollView+UIPageControl简单实现

    #import "MJViewController.h"
    #import "RootViewController.h"


    @interface MJViewController () <UIScrollViewDelegate>
    @property (strong, nonatomic) UIScrollView *scrollView;
    @property (strong, nonatomic) UIPageControl *pageControl;
    @property (strong, nonatomic) UIButton *nextBt;
    @end

    @implementation MJViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
       
        //初始化视图
        NSArray *array = [NSArray arrayWithObjects:[UIColor redColor],[UIColor blueColor],[UIColor yellowColor],[UIColor purpleColor],[UIColor whiteColor], nil];
        
     
        
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 80, self.view.frame.size.width-20, self.view.frame.size.height-100)];
        self.scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        self.automaticallyAdjustsScrollViewInsets = NO;//恢复scrollview偏移
        self.scrollView.delegate = self;
        self.scrollView.pagingEnabled = YES; //分页属性
        self.scrollView.showsHorizontalScrollIndicator= NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.contentSize = CGSizeMake((self.view.frame.size.width-20)*[array count], self.scrollView.frame.size.height); //内容范围
        
        [self.view addSubview:self.scrollView];
        
        self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(10,self.view.frame.size.height-50, self.view.frame.size.width-20, 20)];
    //    self.pageControl.backgroundColor = [UIColor grayColor];
        self.pageControl.numberOfPages = array.count;
        self.pageControl.currentPage = 0;
        [self.view addSubview:self.pageControl];
        
        for (int i = 0; i<array.count; i++) {
            
            UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(i*self.scrollView.frame.size.width+5, 5, self.scrollView.frame.size.width-10, self.scrollView.frame.size.height-10)];
            imageview.backgroundColor = array[i];
            [self.scrollView addSubview:imageview];
            
            if (i==array.count-1) {
                
               imageview.userInteractionEnabled = YES;
           
           self.nextBt = [[UIButton alloc]initWithFrame:CGRectMake(0,0,200, 44)];
            _nextBt.alpha = 0.5;
            _nextBt.backgroundColor = [UIColor darkGrayColor];
            [_nextBt setTitle:@"开启致富之旅" forState:UIControlStateNormal];
            [_nextBt setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
            [_nextBt addTarget:self action:@selector(gotoAction) forControlEvents:UIControlEventTouchUpInside];
            [imageview  addSubview:self.nextBt];
            }
        }
    }
    -(void)gotoAction{
        
        

        RootViewController *rootVC = [[RootViewController alloc]init];
        [self presentModalViewController:rootVC animated:YES];
    }
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{

        if ([scrollView isMemberOfClass:[UITableView class]]) {
            
        }else{
        
            
            //在scrollViewDidScroll内实现监听contentOffset内容偏移量;根据contentOffset计算当前属于哪一页;
            int index = fabs(scrollView.contentOffset.x)/scrollView.frame.size.width;//当前是第几个视图
            self.pageControl.currentPage = index;
        }
    }

    然后在代理里面记得实现:

    #pragma mark - 引导页
    - (void)setIntroductionViewController
    {
        //读取沙盒数据
        NSUserDefaults * settings1 = [NSUserDefaults standardUserDefaults];
        NSString *key1 = [NSString stringWithFormat:@"is_first"];
        NSString *value = [settings1 objectForKey:key1];
        if (!value) {//如果没有数据
            //***
            //引导页
            introductionVC = [[DMIntroductionViewController alloc]init];
            [UIApplication sharedApplication].delegate.window.rootViewController = introductionVC;

        }
        
    }
    @end

  • 相关阅读:
    Syn Bot /OSCOVA 基础教程(2)
    Syn Bot /OSCOVA 介绍(1)
    如何访问阿里云内网数据库
    Winform项目中的Settings.settings与App.config
    WinForm项目开发傻瓜教程
    C++读取BMP文件
    boost异步tcp通信技术练习
    lex/flex 学习笔记 一
    流数据解析中高位地址转换的性能分析
    bash随笔
  • 原文地址:https://www.cnblogs.com/linxiu-0925/p/5421540.html
Copyright © 2011-2022 走看看