zoukankan      html  css  js  c++  java
  • iOS图片轮播

    基于ScrollView的图片播放

    • ScrollView的方法
    • NSTime的循环
    • UIPageControl的运用
    • 委托方法

    基于iphone5 未做屏幕的适配

    import "ViewController.h"
    
    @interface ViewController ()<UIScrollViewDelegate>
    @property(nonatomic,strong) UIScrollView *scrollView;       //声明一个UIScrollView
    @property(nonatomic, strong)UIPageControl *pageControl;     //声明一个UIPageControl
    @property (nonatomic, strong) NSTimer *timer;
    @end
    
    @implementation ViewController
    //设置ScrollView
    -(UIScrollView*)scrollView{
        if(_scrollView==nil){
        	//初始化位置
            _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, 320, 530)]; 
            self.scrollView.contentSize=CGSizeMake(4*_scrollView.bounds.size.width, 0);
            self.scrollView.pagingEnabled=YES;
            self.scrollView.bounces=NO;        
            self.scrollView.showsHorizontalScrollIndicator=NO;
            self.scrollView.showsVerticalScrollIndicator=NO;
            //委托
            self.scrollView.delegate = self;
            [self.view addSubview:_scrollView];        
        }
        return _scrollView;
    }
    
    //分页设置
    -(UIPageControl*)pageControl{
        if(_pageControl==nil){
            _pageControl=[[UIPageControl alloc]init];
            _pageControl.numberOfPages=4;       
            _pageControl.bounds=CGRectMake(0, 0, 150, 20);
            //设置中心
            _pageControl.center=CGPointMake(self.view.center.x, self.scrollView.frame.size.height+30); 
            _pageControl.pageIndicatorTintColor=[UIColor redColor];
            _pageControl.currentPageIndicatorTintColor=[UIColor blackColor];
            [self.view addSubview:_pageControl];
            //页数改变
            [_pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];        
        }
        return _pageControl;
    }
    
    //页数改变事件
    -(void)pageChange:(UIPageControl*)pageCtrl{ 
    	//页数改变同时变更scrollView
        [self.scrollView setContentOffset:CGPointMake(pageCtrl.currentPage*self.scrollView.bounds.size.width, 0) animated:YES];
    }
    
    //scrollView的contentOffset 改变
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{    
        CGFloat scrollviewW =  scrollView.frame.size.width;
        CGFloat x = scrollView.contentOffset.x;
        int page = (x + scrollviewW / 2) /  scrollviewW;
        self.pageControl.currentPage=page;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad]; 
        for (int i=0; i<4; i++) {
            NSString *imageName= [NSString  stringWithFormat:@"%02d",i+1]  ;
            UIImage *image=[UIImage  imageNamed:imageName];     
            UIImageView *imageView =[[UIImageView alloc]initWithFrame:self.scrollView.bounds];
            imageView.image=image;
            [self.scrollView addSubview:imageView ];
        }
        //循环    
        [self.scrollView.subviews enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) {
            CGRect  frame = imageView.frame;
            //索引变化的时候改变坐标位置
            frame.origin.x=frame.size.width*idx;
            imageView.frame=frame;
            
        }];
        //默认从第一个
        self.pageControl.currentPage=0;
        [self startTimer];    
    }
    
    //绑定上timer
    - (void)startTimer
    {
        self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];   
    }
    
    //更新timer
    - (void)updateTimer
    {   
        int page = (self.pageControl.currentPage + 1) % 4;
        self.pageControl.currentPage = page;   
        [self pageChange:self.pageControl];
    }
     
    @end
    
    

  • 相关阅读:
    html5新增元素和废除元素
    html5本地存储
    第四章表单与文件笔记新增属性
    第五章canvas
    lable中for的作用
    第四章表单与文件学习内容
    第三章html5的结构
    html5的全局属性
    正则表达式—升华
    微前端
  • 原文地址:https://www.cnblogs.com/tonyY/p/4755763.html
Copyright © 2011-2022 走看看