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

  • 相关阅读:
    Bailian 2808 校门外的树(入门线段树)
    C++对运算符进行重载学习笔记
    C++构造函数和析构函数什么情况下会用
    C++ 2048游戏
    输入输出速度统计
    左偏树(可并堆) [模板]
    Luogu [P3622] [APIO2007]动物园
    Luogu [P1248] 加工生产调度
    Luogu [P2814] 家谱
    练习盲打的网站 / 练习打字的网站
  • 原文地址:https://www.cnblogs.com/linxiu-0925/p/5421540.html
Copyright © 2011-2022 走看看