大部分APP中都会有循环自动图片播放一些广告,一个前辈做好的JCTop挺好用的,记录下来。
在我的app中是这样用的,
-(void)setUpImageScrollView{
//创建数据
NSMutableArray * tempArray = [[NSMutableArray alloc]init];
for (int i = 1; i< self.getAppContext.yksyImgs.count+1; i++) {
AppConfigInfoBean *images = self.getAppContext.yksyImgs[i-1];
if ([images.sourceParam isEqualToString:@""]) {
//若网络获取的数据images的地址为空的则不记录,不加入数组
}else{
NSString *url = [APINAME stringByAppendingString:images.sourceParam];
//网络图片加载失败
UIImage * PlaceholderImage = [UIImage imageNamed:@"default.jpg"];
NSString *titleString = images.name;
[tempArray addObject:[NSDictionary dictionaryWithObjects:@[url,titleString,@NO,PlaceholderImage,images.actionname] forKeys:@[@"pic",@"title",@"isLoc",@"placeholderImage",@"url"]]];
}
//加入数据
}
if (tempArray.count == 0) {
//若无数据则加载默认图片
[self setDefaultImageView];
}else{
//实例化 ,轮播播放器实例化
_Topic = [[JCTopic alloc]initWithFrame:CGRectMake(0, 0, self.view2.frame.size.width, self.view2.frame.size.height)];
_Topic.JCdelegate = self;
_Topic.pics = tempArray;
//更新
[_Topic upDate];
//代理
UIPageControl * pageControl = [[UIPageControl alloc] init];
pageControl.frame = CGRectMakeAuto(0, 0, 328, 10);
pageControl.numberOfPages = tempArray.count;
pageControl.currentPage = 0;
pageControl.userInteractionEnabled = NO;
self.page = pageControl;
self.page.backgroundColor=[UIColor clearColor];
[self.view1 addSubview:pageControl];
[self.view2 addSubview:_Topic];
}
}
-(void)didClick:(id)data{
// NSLog(@"!!%@",(NSDictionary*)data);
//跳转在本地打开网页
// NewWebViewController *newVC = [self.storyboard instantiateViewControllerWithIdentifier:@"WebVC"];
// [self.navigationController pushViewController:newVC animated:YES];
// NSString *title = [(NSDictionary*)data objectForKey:@"title"];
NSString *url = [(NSDictionary*)data objectForKey:@"url"];
// newVC.url = url;
// newVC.invent = title;
//在safari里打开网页
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
下面小点的代理:-(void)currentPage:(int)page total:(NSUInteger)total{
_page.currentPage = page;
}
实现如:#import <UIKit/UIKit.h>
@protocol JCTopicDelegate<NSObject>
-(void)didClick:(id)data;
-(void)currentPage:(int)page total:(NSUInteger)total;
@end
@interface JCTopic : UIScrollView<UIScrollViewDelegate>{
UIButton * pic;
bool flag;
int scrollTopicFlag;
NSTimer * scrollTimer;
int currentPage;
CGSize imageSize;
UIImage *image;
}
@property (weak, nonatomic) UIPageControl * page;
@property(nonatomic,strong)NSArray * pics;
@property(nonatomic,retain)id<JCTopicDelegate> JCdelegate;
-(void)releaseTimer;
-(void)upDate;
@end
#import "JCTopic.h"
@implementation JCTopic
@synthesize JCdelegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.frame = frame;
[self setSelf];
}
return self;
}
-(void)setSelf{
self.pagingEnabled = YES;
self.scrollEnabled = YES;
self.delegate = self;
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
self.backgroundColor = [UIColor whiteColor];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
[self setSelf];
// Drawing code
}
-(void)upDate{
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) {
pic= Nil;
pic = [UIButton buttonWithType:UIButtonTypeCustom];
pic.imageView.contentMode = UIViewContentModeTop;
[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];
if ([[obj objectForKey:@"isLoc"]boolValue]) {
[tempImage setImage:[obj objectForKey:@"pic"]];
}else{
if ([obj objectForKey:@"placeholderImage"]) {
[tempImage setImage:[obj objectForKey:@"placeholderImage"]];
}
[NSURLConnection sendAsynchronousRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[obj objectForKey:@"pic"]]]
queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
if (!error && responseCode == 200) {
tempImage.image = Nil;
UIImage *_img = [[UIImage alloc] initWithData:data];
[tempImage setImage:_img];
}else{
if ([obj objectForKey:@"placeholderImage"]) {
[tempImage setImage:[obj objectForKey:@"placeholderImage"]];
}
}
}];
}
[pic addSubview:tempImage];
[pic setBackgroundColor:[UIColor grayColor]];
pic.tag = i;
[pic addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:pic];
//
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(i*self.frame.size.width, self.frame.size.height-80, self.frame.size.width,80)];
UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(i*self.frame.size.width, self.frame.size.height-80, self.frame.size.width,80)];
// UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(0,0,0,0)];
title.textAlignment = NSTextAlignmentCenter;
title.numberOfLines = 3;
[title setBackgroundColor:[UIColor clearColor]];
// [title sette
// [title setAlpha:.7f];
// [title setText:[NSString stringWithFormat:@" %@",[obj objectForKey:@"title"]]];
[title setTextColor:[UIColor whiteColor]];
UIFont * tfont = [UIFont fontWithName:@"Helvetica" size:21];
title.font = tfont;
title.lineBreakMode = NSLineBreakByTruncatingTail ;
NSString *tstring = [obj objectForKey:@"title"];
title.text = tstring ;
//高度估计文本大概要显示几行,宽度根据需求自己定义。 MAXFLOAT 可以算出具体要多高
CGSize size =CGSizeMake(280,80);
NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
//ios7方法,获取文本需要的size,限制宽度
CGSize actualsize =[tstring boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:tdic context:nil].size;
// 更新UILabel的frame
title.frame =CGRectMake((view1.frame.size.width-actualsize.width)/2,0, actualsize.width, actualsize.height);
UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, view1.frame.size.height-actualsize.height, self.frame.size.width,actualsize.height)];
view2.backgroundColor = [UIColor blackColor];
view2.alpha = 0.7;
[view2 addSubview:title];
[view1 addSubview:view2];
[self addSubview:view1];
i ++;
}
[self setContentSize:CGSizeMake(self.frame.size.width*[self.pics count], self.frame.size.height)];
[self setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
if (scrollTimer) {
[scrollTimer invalidate];
scrollTimer = nil;
}
if ([self.pics count]>3) {
scrollTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(scrollTopic) userInfo:nil repeats:YES];
}
}
-(void)click:(id)sender{
[JCdelegate didClick:[self.pics objectAtIndex:[sender tag]]];
}
- (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(self.frame.size.width, 0) animated:NO];
}
}
currentPage = scrollView.contentOffset.x/self.frame.size.width-1;
[JCdelegate currentPage:currentPage total:[self.pics count]-2];
scrollTopicFlag = currentPage+2==2?2:currentPage+2;
}
-(void)scrollTopic{
[self setContentOffset:CGPointMake(self.frame.size.width*scrollTopicFlag, 0) animated:YES];
if (scrollTopicFlag > [self.pics count]) {
scrollTopicFlag = 1;
}else {
scrollTopicFlag++;
}
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
scrollTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(scrollTopic) userInfo:nil repeats:YES];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
if (scrollTimer) {
[scrollTimer invalidate];
scrollTimer = nil;
}
}
-(void)releaseTimer{
if (scrollTimer) {
[scrollTimer invalidate];
scrollTimer = nil;
}
}
@end