• 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
    
    

  • 相关阅读:
    删除表
    删除表格的行或者列
    给word中的表格增加行或者列
    向word中插入表格
    设置图片的对齐方式
    day19作业
    Python入门day19——叠加多个装饰器、yield、三元表达式、生成式、函数的递归调用
    day18作业
    Python入门day18——有参装饰器
    Python入门day18——迭代器生成器
  • 原文地址:https://www.cnblogs.com/tonyY/p/4755763.html
Copyright © 2020-2023  润新知