iOS图片轮播

YBImageLoop.h


#import

#import

@interface YBImageLoop : NSObject


@property (nonatomic ,strong)NSArray *imageArray; //图片名字数组

@property (nonatomic ,strong)UIPageControl *pageControl;

//构造方法


- (instancetype)initWithScrollViewFrame:(CGRect)rect andContainerView:(UIView *)containerView;

//处理数据

- (void)setImageArray:(NSArray *)imageArray;

//开启定时器

- (void)startTimerWithTime:(NSTimeInterval)time;


@end





YBImageLoop.m


#import "YBImageLoop.h"



@interface YBImageLoop (){

    NSTimer *_timer;

}


@property (nonatomic ,strong)UIScrollView *scrollView;


@property (nonatomic ,strong)NSArray *imageNameArray; //图片名字数组

@property (nonatomic ,assign)NSTimeInterval time;

@property (nonatomic ,assign)BOOL isTimer; //判断定时器是否在viewController中创建过


@end


@implementation YBImageLoop



#define SCROLLWIDTH self.scrollView.frame.size.width

#define SCROLLHEIGHT self.scrollView.frame.size.height

- (instancetype)initWithScrollViewFrame:(CGRect)rect andContainerView:(UIView *)containerView

{

    self = [super init];

    if (self) {

        self.scrollView = [[UIScrollView alloc]initWithFrame:rect];

        self.scrollView.bounces = NO;

        self.scrollView.pagingEnabled = YES;

        self.scrollView.showsHorizontalScrollIndicator = NO;

        self.scrollView.showsVerticalScrollIndicator = NO;

        //        sv.contentOffset = CGPointMake(rect.size.width, 0);

        [containerView addSubview:self.scrollView];


        

    }

    return self;

}


- (void)setImageArray:(NSArray *)imageArray {

    _imageArray = imageArray;

    self.imageNameArray = imageArray;

    self.scrollView.delegate = self;

    self.scrollView.contentOffset = CGPointMake(SCROLLWIDTH, 0);

    self.scrollView.contentSize = CGSizeMake((imageArray.count+2) * SCROLLWIDTH, 0);

    

    for (NSInteger i = 1; i <= self.imageNameArray.count; i ++) {

        UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(i * SCROLLWIDTH, 0,SCROLLWIDTH , SCROLLHEIGHT)];

        

        iv.image = [UIImage imageNamed:self.imageNameArray[i-1]];

        //        NSLog(@"%@",self.imageNameArray[i-1]);

        [self.scrollView addSubview:iv];

    }

    

    UIImageView *firstIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCROLLWIDTH, SCROLLHEIGHT)];

    firstIV.image = [UIImage imageNamed:[self.imageNameArray lastObject]];

    [self.scrollView addSubview:firstIV];

    

    

    

    UIImageView *lastIV = [[UIImageView alloc]initWithFrame:CGRectMake((self.imageNameArray.count+1)*SCROLLWIDTH, 0, SCROLLWIDTH, SCROLLHEIGHT)];

    

    lastIV.image = [UIImage imageNamed:[self.imageNameArray firstObject]];

    [self.scrollView addSubview:lastIV];

    BOOL ret = [self.scrollView.delegate respondsToSelector:@selector(scrollViewDidScroll:)];

    NSLog(@"%d",ret);

    

}

 

- (void)startTimerWithTime:(NSTimeInterval)time {

    _timer = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(timeRun) userInfo:nil repeats:YES];

    [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];

    self.time = time;

    self.isTimer = YES;

    

}


static int i = 1;

- (void)timeRun {

    CGFloat offsetX = self.scrollView.contentOffset.x;

    i = offsetX/SCROLLWIDTH;

    i++;

    if (i == self.imageNameArray.count + 1) {

        i = 1;

        self.scrollView.contentOffset = CGPointMake(-1, 0);

    }

    [self.scrollView setContentOffset:CGPointMake(i * SCROLLWIDTH, 0) animated:YES];

    

}

 

- (void)removeTimer {

    [_timer invalidate];

    _timer = nil;

}



 


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

    CGFloat offset_x = scrollView.contentOffset.x;

    CGFloat page = offset_x/SCROLLWIDTH;

    

    self.pageControl.currentPage = page -1;

    

//    NSLog(@"page = %f",page);

    if (self.imageNameArray.count+1 == page) {

        scrollView.contentOffset = CGPointMake(SCROLLWIDTH, 0);

    }

    else if (page == 0) {

        scrollView.contentOffset = CGPointMake(SCROLLWIDTH*self.imageNameArray.count, 0);

    }

    

    

    

}

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

    [self removeTimer];

}


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

    if (self.isTimer == YES) {

        [self startTimerWithTime:self.time];

    }

    

}


@end






YBPictureViewController.m


#import "YBPictureViewController.h"

#import "YBImageLoop.h"

@interface YBPictureViewController ()

@property (nonatomic ,strong)YBImageLoop *loop;


@end


@implementation YBPictureViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColor orangeColor];

    

    

    [self settingImageLoop];


    [self settingPageControl];

    

    

}



-(void)settingImageLoop {

    

    

    self.loop = [[YBImageLoop alloc]initWithScrollViewFrame:CGRectMake(20, 100, self.view.width - 40, self.view.width * 400/750) andContainerView:self.view];

    

    self.loop.imageArray = @[@"img_01",@"img_02",@"img_03",@"img_04",@"img_05"];

    

    [self.loop startTimerWithTime:2];

    

}



-(void)settingPageControl {

    

    UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 300, self.view.width, 20)];

    

    pageControl.pageIndicatorTintColor = [UIColor grayColor];

    

    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];

    

    pageControl.numberOfPages = 5;

    

    [self.view addSubview:pageControl];

    

    self.loop.pageControl = pageControl;

    

    

}

  

 

@end


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值