轮播图封装
2016-01-26 19:46
246 查看
//
// 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.picUrl]
placeholderImage:[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
// 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.picUrl]
placeholderImage:[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
相关文章推荐
- LIS最长上升子序列模板
- XTREE随笔
- Matlab里面的指针
- xen事件通道机制及其实例
- 色键算法的关键点
- node.js REPL
- Android 的代码模板
- HDU1232 畅通工程【并查集】
- 音乐播放器单例
- HBase-5.提高HBase客户端的读写性能方法
- The Java™ Tutorials — Generics :Generic Methods and Bounded Type Parameters 泛型方法和受限类型参数
- 感谢2015,期待2016!
- HBase-4.HBase内部机制
- Git的使用--打tag
- Python数据分析、展示
- ajax--html5上传文件file api +FormData
- 小远设计qq1611532647-淘宝创意主图
- IE6设置li的float:left,不能自适应宽的解决方法
- SetRegistryKey
- docker 数据共享,数据复制