轮播图封装

//

//  JKlunbotu.h

//  JKqyApp

//

//  Created by dllo on 16/1/6.

//  Copyright © 2016 dllo. All rights reserved.

//


#import <UIKit/UIKit.h>



@protocol JKlunbotuDelegate <NSObject>


- (void)jklunbotuWithStr:(NSInteger)str;


@end



@interface JKlunbotu : UIView


- (instancetype)initWithFrame:(CGRect)frame;

@property (nonatomic, assign)id <JKlunbotuDelegate>delegate;

@property (nonatomic, retain)NSMutableArray *lunboArr;

@end

//

//  JKlunbotu.m

//  JKqyApp

//

//  Created by dllo on 16/1/6.

//  Copyright © 2016 dllo. All rights reserved.

//


#import "JKlunbotu.h"

#import "UIImageView+WebCache.h"

#import "FIrstpageFind.h"

#define WIDGHT [[UIScreen mainScreen] bounds].size.width

#define WIDTHSIXP 375.000000

#define HEIGHTSIXP 667.000000

#define HEIGHT  [[UIScreen mainScreen] bounds].size.height

@interface JKlunbotu ()<UIScrollViewDelegate>

@property (nonatomic, retain)UIScrollView *srollview;

@property (nonatomic, retain)UIPageControl *panC;

@property (nonatomic, assign)NSInteger previouspage;

@property (nonatomic, retain)NSTimer *timer;

@end


@implementation JKlunbotu

- (void)dealloc {

    

    _delegate = nil;

    _timer = nil;

    [_lunboArr release];

    [super dealloc];

}

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        [self createview];

    }

    return self;

}

- (void)setLunboArr:(NSMutableArray *)lunboArr

{

    if (_lunboArr != lunboArr) {

        [_lunboArr release];

        _lunboArr = [lunboArr retain];

        

    }

       //  刷新scrollview

    [self reloadData];

//    //  创建计时器

    [self kaishitimer];

    

}


- (void)kaishitimer

{

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAct) userInfo:nil repeats:YES];

    //加入主循环池中

    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

    _timer = timer;


    

}

- (void)timerAct

{

    if (self.lunboArr.count > 1) {

        self.panC.currentPage = (self.previouspage + 1) % self.lunboArr.count;

        [self panCact];

        [UIView animateWithDuration:.5 animations:^{

            

            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

            self.srollview.contentOffset = CGPointMake(self.srollview.frame.size.width * (self.panC.currentPage + 1), 0);

        }];

        

    }

}

- (void)reloadData

{

    //page 页数

    self.panC.numberOfPages = self.lunboArr.count;

    //scrollView

    self.srollview.contentSize = CGSizeMake(WIDGHT * (self.lunboArr.count + 2), 0);

    [self addImagetosrollv];

}

- (void)addImagetosrollv

{

   

    for (NSInteger i = 0;i < self.lunboArr.count + 2; i++) {

        //创建一个imageView scroll

        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(WIDGHT * i , 0, WIDGHT, 170 / HEIGHTSIXP * HEIGHT)];

        imageV.userInteractionEnabled = YES;


        //此处重点:多加两张图(最后一张和第一张)

        if (0 == i) {

            //第一张图 的位置 最后一张

            //            imageV.image = [self.imagelunboArr lastObject];

            FIrstpageFind *find = [self.lunboArr lastObject];

//            NSLog(@"%@", find.picUrl);


            [imageV sd_setImageWithURL:[NSURL URLWithString:find.picUrl] placeholderImage:[UIImage imageNamed:@"2.jpg"]];

        }else if(self.lunboArr.count + 1 == i) {

            //最后一张的位置 铺第一张图

            //            imageV.image = self.imageArr[0] ;

            

            FIrstpageFind *find = [self.lunboArr firstObject];

//            NSLog(@"%@", find.picUrl);


            [imageV sd_setImageWithURL:[NSURL URLWithString:find.picUrlplaceholderImage:[UIImage imageNamed:@"2.jpg"]];

        }else {

            //i - 1: scroll是从 1 开始的 不是 0

            //            imageV.image = self.imageArr[i - 1];

             FIrstpageFind *find = [self.lunboArr objectAtIndex:i- 1];

//            NSLog(@"%@", find.picUrl);


            [imageV sd_setImageWithURL:[NSURL URLWithString:find.picUrl] placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];

//            //添加标题


        }

        

        //添加手势

        UITapGestureRecognizer *imageTap  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapAct)];

        [imageV addGestureRecognizer:imageTap];

        [imageTap release];

        

        

        [self.srollview addSubview:imageV];

        [imageV release];

        

    }

    


}

- (void)imageTapAct

{

    [self.delegate jklunbotuWithStr:self.panC.currentPage];

    

}


- (void)createview

{

    //创建srollview

    self.backgroundColor = [UIColor whiteColor];

    [self createScrollView];

    [self createPageControll];

    

}

- (void)createScrollView

{

    


    self.srollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDGHT, 170/ HEIGHTSIXP * HEIGHT)];

    self.srollview.showsHorizontalScrollIndicator = NO;

    self.srollview.delegate = self;

    self.srollview.pagingEnabled = YES;

    self.srollview.contentOffset = CGPointMake(WIDGHT, 0);

    [self addSubview:self.srollview];

    [self.srollview release];


    

}

- (void)createPageControll

{

    self.panC = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.bounds.size.height - 20, 200/ WIDTHSIXP * WIDGHT, 20 / HEIGHTSIXP * HEIGHT )];

    self.panC.backgroundColor = [UIColor clearColor];

    self.panC.currentPageIndicatorTintColor = [UIColor orangeColor];

    self.panC.pageIndicatorTintColor = [UIColor whiteColor];

    [self.panC addTarget:self action:@selector(panCact) forControlEvents:UIControlEventValueChanged];

    [self addSubview:self.panC];

    [self.panC release];

    

}

//设置页随点动

- (void)panCact

{

    // 记住每一次改变后的图片前一页是哪一张

    self.previouspage = self.srollview.contentOffset.x / self.bounds.size.width;

    [self.srollview setContentOffset:CGPointMake(self.bounds.size.width * (self.panC.currentPage), 0)];

}

// 设置点随页动

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    //设置 点随页动

    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;

    if (0 == index) {

        self.srollview.contentOffset = CGPointMake(self.frame.size.width * (self.lunboArr.count + 1) , 0);

        self.panC.currentPage = self.lunboArr.count + 1;

    }

    else if (self.lunboArr.count + 1 == index) {

        self.srollview.contentOffset = CGPointMake(self.frame.size.width  , 0);

        self.panC.currentPage = 0;

    } else {

        self.panC.currentPage = index - 1;

    }


}


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

{

    [self kaishitimer];

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    [self.timer invalidate];

//    self.timer = nil;

}



/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/


@end



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值