您的位置:首页 > 其它

实现图片轮播

2015-10-15 11:07 267 查看
//

//  ViewController.m

//  图片轮播

//

//  Created by weibiao 

//  Copyright © 2015年 weibiao. All rights reserved.

//

#import "ViewController.h"

#define KimageCount  5

@interface
ViewController ()<UIScrollViewDelegate>

@property (nonatomic,
strong) UIScrollView *scrollView;

@property (nonatomic,
strong) UIPageControl *pageControl;

@property (nonatomic,
strong) NSTimer *timer;//增加时钟

@end

@implementation ViewController

//scrollView懒加载

-(UIScrollView *)scrollView {

    if (!_scrollView) {

    _scrollView = [[UIScrollView
alloc] initWithFrame:CGRectMake(10,
20,
300, 130)];

        _scrollView.backgroundColor = [UIColor
grayColor];

        [self.view
addSubview:_scrollView];

        

        //设置滚动尺寸

        _scrollView.contentSize =
CGSizeMake(KimageCount *
300,
0);//0表示可滚动的范围为0,既不可y方向滚动

        

        //取消弹簧效果

        _scrollView.bounces =
NO;

        

        //取消滚动条

        _scrollView.showsHorizontalScrollIndicator =
NO;

        _scrollView.showsVerticalScrollIndicator =
NO;

        

        //设置分页

        _scrollView.pagingEnabled =
YES;

        

        //设置代理

        _scrollView.delegate =
self;

        

    }

    return
_scrollView;

}

-(UIPageControl *)pageControl {

    if (!_pageControl) {

        //分页控件

        _pageControl = [[UIPageControl
alloc] init];

        

        //总页数

        _pageControl.numberOfPages =
KimageCount;

        

        //控件尺寸

        CGSize size = [_pageControl
sizeForNumberOfPages:KimageCount];

        

        _pageControl.bounds =
CGRectMake(0,
0, size.width, size.height);

        _pageControl.center =
CGPointMake(self.view.center.x,
130);

        

        //设置颜色

        _pageControl.pageIndicatorTintColor = [UIColor
redColor];

        _pageControl.currentPageIndicatorTintColor = [UIColor
blueColor];

        

        [self.view
addSubview:_pageControl];

        

        //添加监听方法

        [_pageControl
addTarget:self
action:@selector(pageChanged:)
forControlEvents:UIControlEventValueChanged];

    }

    return
_pageControl;

}

//实现监听方法

-(void)pageChanged:(UIPageControl *)page {

   
//根据参数页数,调整轮播中的图片

    CGFloat x = page.currentPage*self.scrollView.bounds.size.width;

    [self.scrollView
setContentOffset:CGPointMake(x,
0) animated:YES];

}

- (void)viewDidLoad {

    [super
viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    for (int i =
0; i < KimageCount; i++) {

        NSString *imageName = [NSString
stringWithFormat:@"img_0%d",i+1];

        UIImage *image = [UIImage
imageNamed:imageName];

        UIImageView *imageView = [[UIImageView
alloc] initWithFrame:self.scrollView.bounds];

        imageView.image = image;

        [self.scrollView
addSubview:imageView];

    }

    //计算imageView的位置

    [self.scrollView.subviews
enumerateObjectsUsingBlock:^(
UIImageView *imageView, NSUInteger idx,
BOOL *stop) {

        CGRect frame = imageView.frame;

        frame.origin.x = idx * frame.size.width;

        imageView.frame = frame;

    }];

    

   
//初始轮播显示页数

    self.pageControl.currentPage =
0;

    

//    NSLog(@"%@",self.scrollView.subviews);

    

    //调用时钟

    [self
startTimer];

}

-(void)startTimer {

//    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];

    self.timer = [NSTimer
timerWithTimeInterval:2.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];

   
//添加到运行循环中后,此时轮播中的图片不支持拖动,解决方法是设置scrollView的代理方法

    [[NSRunLoop
currentRunLoop] addTimer:self.timer
forMode:NSRunLoopCommonModes];

}

//支持拖动的scrollView的拖动方法

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {//拖动开始

   
//停止时钟,注意停止后,不会在进行时钟,必须调用设置的startTimer方法

    [self.timer
invalidate];

}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    [self
startTimer];

}

-(void)updateTimer {

    NSLog(@"%ld",self.pageControl.currentPage+1);

    int page = (self.pageControl.currentPage +
1) % KimageCount;

    self.pageControl.currentPage = page;

    //调用监听方法

    [self
pageChanged:self.pageControl];

}

#pragma mark -- UIScrollView的代理方法

//修改当前的页数(小点)

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    int page =
self.scrollView.contentOffset.x /
self.scrollView.bounds.size.width;

    self.pageControl.currentPage = page;

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: