纯代码 封装banner轮播控件
//
// 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
- 自定义控件(16)---代码方式组合系统控件,并封装自定义控件思想
- 广告栏(自动轮播,无限循环)-图片轮播控件Banner的简单使用总结
- 自定义开发一个android轮播图控件BannerImg
- 第三方控件Banner实现轮播
- [置顶] Android图片轮播控件ConvenientBanner的简单使用
- Android (github开源项目1)首页轮播图控件----banner
- app引导功能界面+广告轮播控件,两句代码搞定
- 轮播控件——BGABanner
- BGABanner-Android 引导、图片轮播控件
- XBanner支持图片无限轮播控件
- Android实现一个丝滑的自动轮播控件实例代码
- 安卓轮播图控件ConvenientBanner的使用
- [置顶] Android图片轮播控件ConvenientBanner的简单使用
- 简单易用的轮播图控件Banner
- delphi的文件夹监控控件,直接封装成可视化的控件,拖一下即可代码。核心为:ReadDirectoryChangesW
- banner轮播无缝滚动 jq代码
- Android图片轮播控件---com.youth.banner.Banner
- 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)
- XBanner、FlyBanner支持图片无限轮播控件