(PS:IOS新手,写的东西难免有错,请大家指正)
1.自定义一个类,用于展现轮播图片,该类叫IKEDWheelPic,该类实现了UIScrollView的继承,其中pics存的都IKEDItemInfo是类型的,暂时如下
//
// IKEDWheelPic.h
// lunbo
//
// Created by apple on 14-2-26.
// Copyright (c) 2014年 com.tyust. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface IKEDWheelPic : UIScrollView
{
//轮播里面放的就是一个一个的按钮,按钮上填充一个UIImageView
UIButton *pic;
BOOL flag;
int scrollTopicFlag;
//用于处理时间的对象
NSTimer *scrollTimer;
int currentPage;
CGSize imageSize;
UIImage *image;
}
@property(nonatomic,strong)NSArray * pics;
@end
2.理解图片循环播放有两个点,第一个点图片由最后一张继续向前滑动,应该到第一张,比如共有4张图片,继续往前拉,她的centerpoint应该等于第一幅图的center,从第一张往后拉,应该是往前移动两个界面宽度,这点理解了,基本上解决了这个问题了,代码如下
//
// IKEDWheelPic.m
// lunbo
//
// Created by apple on 14-2-26.
// Copyright (c) 2014年 com.tyust. All rights reserved.
//
#import "IKEDWheelPic.h"
#import "IKEDItemInfo.h"
@implementation IKEDWheelPic
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.frame = frame;
[self setSelfProperties];
}
return self;
}
-(void)drawRect:(CGRect)rect
{
[self setSelfProperties];
}
-(void)setSelfProperties
{
self.pagingEnabled = YES;
self.scrollEnabled = YES;
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
self.backgroundColor = [UIColor whiteColor];
//实现ScrollowView的delegate方法
self.delegate = self;
}
-(void)showPic
{
NSMutableArray *tempImageArray = [[NSMutableArray alloc]init];
//将传过来的数组最后的一张图片纺织再第一张的前面
[tempImageArray addObject:[self.pics lastObject]];
for (id obj in self.pics) {
[tempImageArray addObject:obj];
}
[tempImageArray addObject:[self.pics objectAtIndex:0]];
//清空原来的数组
self.pics = Nil;
//将可变数组的内容付给原来的数组
self.pics = tempImageArray;
//关键的地方
int i = 0;
for(id obj in self.pics)
{
//每次调用会清空原来的展现的Button
pic = nil;
//自定义Button
pic = [UIButton buttonWithType:UIButtonTypeCustom];
//UIViewContentModeTop:The option to center the content aligned at the top in the view’s bounds.
pic.imageView.contentMode = UIViewContentModeTop;
//根据图片的次序来设置Button的位置
[pic setFrame:CGRectMake(i*self.frame.size.width,0, self.frame.size.width, self.frame.size.height)];
UIImageView * tempImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, pic.frame.size.width, pic.frame.size.height)];
tempImage.contentMode = UIViewContentModeScaleAspectFill;
[tempImage setClipsToBounds:YES];
[tempImage setImage:[obj objectForKey:@"pic"]];
[pic addSubview:tempImage];
[pic setBackgroundColor:[UIColor grayColor]];
pic.tag = i;
[self addSubview:pic];
//用于展示标签栏
UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(i*self.frame.size.width, self.frame.size.height-30, self.frame.size.width,30)];
[title setBackgroundColor:[UIColor blackColor]];
[title setAlpha:.7f];
[title setText:[NSString stringWithFormat:@" %@",[obj objectForKey:@"title"]]];
[title setTextColor:[UIColor whiteColor]];
[title setFont:[UIFont fontWithName:@"Helvetica" size:12]];
[self addSubview:title];
i ++;
}
//scrollow view的大小
[self setContentSize:CGSizeMake(self.frame.size.width*[self.pics count], self.frame.size.height)];
[self setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat Width=self.frame.size.width;
if (scrollView.contentOffset.x == self.frame.size.width) {
flag = YES;
}
if (flag) {
if (scrollView.contentOffset.x <= 0) {
[self setContentOffset:CGPointMake(Width*([self.pics count]-2), 0) animated:NO];
}else if (scrollView.contentOffset.x >= Width*([self.pics count]-1)) {
[self setContentOffset:CGPointMake(Width*1, 0) animated:NO];
}
}
currentPage = scrollView.contentOffset.x/self.frame.size.width-1;
}
@end