ios图片轮播加定时器

头文件

//

//  pagingImageInScrollView.h

//  ZJQpaging-image-scrollview

//

//  Created by 郑敬勤 on 16/8/7.

//  Copyright © 2016 郑敬勤. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface pagingImageInScrollView : UIView

@property(strong,nonatomic) NSArray * imageArray;


@property(strong,nonatomic) NSTimer * timer;



+(instancetype)pagingImageInScrollViewNIBloding;

@end


M文件

//

//  pagingImageInScrollView.m

//  ZJQpaging-image-scrollview

//

//  Created by 郑敬勤 on 16/8/7.

//  Copyright © 2016 郑敬勤. All rights reserved.

//


#import "pagingImageInScrollView.h"


@interface pagingImageInScrollView ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;


@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;


@end


@implementation pagingImageInScrollView

//加载xib将会自动执行

-(void)awakeFromNib{

    

    self.scrollView.pagingEnabled=YES;

    self.scrollView.showsVerticalScrollIndicator=NO;

    self.scrollView.showsHorizontalScrollIndicator=NO;

//    self.pageControl.center=CGPointMake(self.center.x, self.center.y-30);

    //设置颜色

    //未被选中时点的颜色

    self.pageControl.pageIndicatorTintColor=[UIColor grayColor];

    //选中时的点得颜色

    self.pageControl.currentPageIndicatorTintColor=[UIColor redColor];

  //  self.pageControl.backgroundColor=[UIColor clearColor];

    self.scrollView.delegate=self;

    

    

    

}

//当一个UIView获得它的frame的时候,该方法会自动调用一次,在这里我们可以给UIVIEW的所有SUBVIEW针对当前UIView的新的FRAME,做重新界面设置

-(void)layoutSubviews{

    

    [super layoutSubviews];

    CGFloat width =  self.frame.size.width;

    CGFloat height =  self.frame.size.height;

    //self.scrollView.frame=self.bounds;

    

    self.pageControl.center=CGPointMake(self.center.x, (self.center.y)+60);

    for (int i=0; i<_imageArray.count; i++) {

        self.scrollView.subviews[i].frame=CGRectMake(i*width, 0, width, height);

        

    }

    self.scrollView.contentSize=CGSizeMake(width*_imageArray.count, 0);

    [self addTimer];

    

    

    

}

//移动页面

-(void)nextPage{

    NSInteger page=self.pageControl.currentPage;

    if (page==self.imageArray.count-1) {

        page=0;

    }

    else{

        page++;

        

    }

    _scrollView.contentOffset=CGPointMake(page*(_scrollView.frame.size.width), 0);

   //NSLog(@"%@",NSStringFromCGPoint(_scrollView.contentOffset));

    

}

-(void)addTimer{

    

    self.timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];

}


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

    //重复赋值给imageArray时,先移除上一次的imageView

    NSUInteger cnt = self.scrollView.subviews.count;

    

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

        [self.scrollView.subviews[i] removeFromSuperview];

    }

    

   

    for (int i=0; i<imageArray.count; i++) {

   UIImageView *view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:imageArray[i]]];

        [_scrollView addSubview:view];

    }

    self.pageControl.numberOfPages=imageArray.count;

     _imageArray=imageArray;

    

}


+(instancetype)pagingImageInScrollViewNIBloding{

    

   return  [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil]lastObject];

}


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

    

    self.pageControl.currentPage= (int)(_scrollView.contentOffset.x/self.scrollView.frame.size.width+0.5);

}

@end


xib是个很简单的xib



然后只要在哪里使用导入重新定义就可以了;

implementation imgCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    

    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

       pagingImageInScrollView *view=[pagingImageInScrollView pagingImageInScrollViewNIBloding];

        view.imageArray=@[@"1",@"2",@"3",@"4"];

        view.frame=CGRectMake(0, 0, main_size.width,150);

        [self addSubview:view];

    }

    return self;

}

@end

我这是在重写cell中定义的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值