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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值