图片轮播 自动轮播

1.ViewController

//
//  ViewController.m
//  006-图片轮播器
//
//  Created by LTOVE on 15/10/2.
//  Copyright (c) 2015年 LTOVE. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *zhishiqi;
//创建一个用来引用计时器的属性
@property (nonatomic,strong) NSTimer *time;



@end

@implementation ViewController
//即将开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//    停止计时器  计时器 一旦停止 不可复生
    [self.time invalidate];
    self.time = nil;
}
//拖拽完毕的方法
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
//    启动计时器
    self.time = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
}
//实现滚动方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    计算当前滚动到了第几页
//    1  获取滚动的x方向的偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    offsetX += scrollView.frame.size.width * 0.5;
//    当前第几页索引
    int pag = offsetX/scrollView.frame.size.width;
//    讲页码设置给zhishiqi;
    self.zhishiqi.currentPage = pag;
}


- (void)viewDidLoad {
    [super viewDidLoad];
//    动态创建UIImageView添加到UIScrollView中
//    循环创建五个UIImageView
    CGFloat imgW = 300;
    CGFloat imgH = 130;
    CGFloat imgY = 0;
    for (int i= 0; i<5; i++) {
        UIImageView *imgView = [UIImageView new];
//        拼接图片名
        NSString *imgName = [NSString stringWithFormat:@"img_%02d",i+1];
//        设置uiImageView中的图片
        imgView.image = [UIImage imageNamed:imgName];
        CGFloat imgX = imgW * i;
//        设置imgView的fram
        imgView.frame = CGRectMake(imgX, imgY, imgW, imgH);
        
//        加载到UIScrollView中
        [self.scrollView addSubview:imgView];
        CGFloat maxW = self.scrollView.frame.size.width * 5;
        self.scrollView.contentSize = CGSizeMake(maxW, 0);
//        实现分页效果
        self.scrollView.pagingEnabled = YES;
//        UIScrollView就是每一页的大小
    }
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
//    设置UIPageControl的总页数
    self.zhishiqi.numberOfPages = 5;
    
//    指定默认是0页
    self.zhishiqi.currentPage = 0;
    
//    创建一个计时器空间NSTimer
    self.time = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
    
}

//自动滚动播放图片的方法
//y因为创建计时器的时候指定的是间隔1秒   所以下边的方法  每一秒执行一次
-(void)scrollImage
{
    
//    每执行一次   图片切换到下一页
//    1   获取当前的页码
    NSUInteger page = self.zhishiqi.currentPage;
//    2 判断页码是否到了最后一页  如果到了最后  设置页码为0  回到第一页
//    如果没有到第一页 让页码加1
    if (page == self.zhishiqi.numberOfPages-1) {
        page = 0;
    }else{
        page++;
    }
//    3 每页的宽度  * (页码+1)   计算下一页的offsetX
    CGFloat offsetX = page *self.scrollView.frame.size.width;
    NSLog(@"%f",offsetX);
//    4 设置 offset
    [self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
//    如果已经到第最后了  那么就滚动到第一页
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

2.UICollectionView

//
//  JTGRunLoopView.m
//  JTG
//
//  Created by LTOVE on 15/11/23.
//  Copyright (c) 2015年 LTOVE. All rights reserved.
//

#import "JTGRunLoopView.h"

#import "JTGRunLoopList.h"
#import "JTGRunLoopCell.h"

#import "JTGDataTooll.h"


#import "MJExtension.h"

@interface JTGRunLoopView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>


@property (nonatomic,strong) NSTimer * time;
@property (nonatomic,assign) NSUInteger  page;

@end

@implementation JTGRunLoopView

//static NSString * const reuseIdentifier = @"Cell";

- (NSArray *)dataArry
{
    if (!_dataArry) {
        _dataArry = [NSArray array];
    }
    return _dataArry;
}

- (instancetype)init
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(screenW, screenW / 3.16);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    清空行距
    layout.minimumLineSpacing = 0;
        self.delegate = self;
        self.dataSource = self;
    [self loadData];
    return [self initWithFrame:CGRectMake(0, 0, screenW, screenW / 3.16) collectionViewLayout:layout];
}


- (void)loadData
{
    [JTGDataTooll LoadRunLoopPicsWithsuccess:^(NSArray *items) {
        self.dataArry = items;
//        JTGRunLoopList *li = [items firstObject];
//        NSLog(@"%@",li.keyValues );
        [self reloadData];
        [self setUpPadgeCount];
        [self startTimer];
    } failure:^(NSError *error) {
        
    }];
}

- (void)startTimer
{
self.time = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
}
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArry.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JTGRunLoopCell *cell = [JTGRunLoopCell cellWithCollectionView:collectionView indexPath:indexPath];
    JTGRunLoopList *dataModel = self.dataArry[indexPath.row];
    cell.dateModel = dataModel;
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.page = scrollView.contentOffset.x / scrollView.width + 0.5;
    if ([_delegatel respondsToSelector:@selector(runLoop:)]) {
        [_delegatel runLoop:(JTGRunLoopView *)scrollView];
    }
}



- (void)setUpPadgeCount
{
    if ([_delegatel respondsToSelector:@selector(runLoop:count:)]) {
        [_delegatel runLoop:self count:self.dataArry.count];
    }

}

- (void)scrollImage
{
    if (self.page == self.dataArry.count - 1) {
        self.page = 0;
    }else{
        self.page ++;
    }
    CGFloat offsetX = self.page * self.width;
    
    [self setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.time invalidate];
    self.time = nil;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self startTimer];
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值