您的位置:首页 > 编程语言

纯代码 封装banner轮播控件

2015-09-28 19:20 211 查看

//

//  BannerScroll.h

//  scrolltext

//

//  Created by Tony on 15/9/28.

//  Copyright (c) 2015年 Tony. All rights reserved.

//


#import <UIKit/UIKit.h>

@class BannerScroll;


//声明妥托

@protocol BannerScrollDelegate <NSObject>


//必须实现的方法

@required

-(void)bannerScrollBtnClick:(BannerScroll*)bannerScroll url:(NSString*)rul;


@end


@interface BannerScroll : UIView

-(instancetype)initWithFrame:(CGRect)frame;

+(instancetype)bannerScrollWithFrame:(CGRect)frame;

//广告数组,重写set方法,并在里面初始化

@property(nonatomic,strong)NSArray *imageArray;

//广告点击回调

@property(nonatomic)id<BannerScrollDelegate> delegate;


@end




//

//  BannerScroll.m

//  scrolltext

//

//  Created by Tony on 15/9/28.

//  Copyright (c) 2015年 Tony. All rights reserved.

//


#import "BannerScroll.h"


@interface BannerScroll ()


@property(nonatomic,strong)UIScrollView *scrollView;


@property(nonatomic,strong)UIPageControl *pageControl;


@property(nonatomic,strong)NSTimer *timer;



@property(nonatomic)int index;


@property(nonatomic)CGFloat scends;

@end


@implementation BannerScroll


- (instancetype)initWithFrame:(CGRect)frame

{

    if (self=[super initWithFrame:frame]) {

        self.frame=frame;

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

         [self addSubview:self.scrollView];

    }

    return self;

}


+(instancetype)bannerScrollWithFrame:(CGRect)frame

{

    return [[self alloc] initWithFrame:frame];

}

-(void)setImageArray:(NSArray *)imageArray

{

    _imageArray=imageArray;

    self.scends=3.0;

    self.pageControl.numberOfPages=self.imageArray.count;

    [self initSubViews];

}



//初始化子控件

-(void)initSubViews

{

    //1.往里面添加广告

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

        

        CGRect rect=self.frame;

        rect.origin.x=i*rect.size.width;

        UIButton *btnImg=[[UIButton alloc] init];

        btnImg.frame=rect;

        btnImg.tag=i;

        [btnImg setBackgroundImage:[UIImage imageNamed:self.imageArray[i]] forState:UIControlStateNormal];

        [btnImg addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

        [self.scrollView addSubview:btnImg];

        if (i==1) {

            btnImg.backgroundColor=[UIColor yellowColor];

        }else if(i==2){

            btnImg.backgroundColor=[UIColor blackColor];

        }

    }

    self.pageControl.frame=CGRectMake(0, self.frame.size.height-30, self.frame.size.width, 30);

    self.pageControl.tintColor=[UIColor grayColor];//普通点的颜色

    self.pageControl.currentPageIndicatorTintColor=[UIColor redColor];//选中的颜色

    [self addSubview:self.pageControl];

    

    //2.设计滚动区域

    self.scrollView.contentSize=CGSizeMake(self.frame.size.width*self.imageArray.count, 0);

    //3.滚动到边界

    self.scrollView.pagingEnabled=YES;

    //4去掉滚动条

    self.scrollView.showsHorizontalScrollIndicator=NO;

    //5设置个数

    self.pageControl.numberOfPages=self.imageArray.count;

    //6.计时器启动

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

    //7获取当前的消息循环对像

    NSRunLoop *runLoop=[NSRunLoop currentRunLoop];

    //改变self.time的对象优化级

    [runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];

    

    [self bringSubviewToFront:self.pageControl];

    

}


//定时器事件

-(void)scrollImage

{

    NSLog(@"定时器动了");

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

        self.index=0;

    }else

    {

        self.index++;

    }

    

    [self.scrollView setContentOffset:CGPointMake(self.index*self.frame.size.width, 0) animated:YES];


}


//在滚动的时候计算页码,然后赋值

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    self.index=(scrollView.contentOffset.x+self.frame.size.width/2)/self.frame.size.width;

    self.pageControl.currentPage=self.index;

}

//开始拖拽的时候 停止计时器,不让滚动了

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    //停掉计时器

    [self.timer invalidate];

    self.timer=nil;

}


//停止拖拽的时候 重新开始计时器

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

{

    //重新启动一个计时器

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

    

    //获取当前的消息循环对像

    NSRunLoop *runLoop=[NSRunLoop currentRunLoop];

    //改变self.timer对像的优化级

    [runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];

}



-(void)btnClick:(UIButton*)sender

{

    if ([self.delegate  respondsToSelector:@selector(bannerScrollBtnClick:url:)]) {

         NSString*dict=self.imageArray[sender.tag];

        [self.delegate bannerScrollBtnClick:self url:dict];

    }

}



@end





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