iOS首页广告滚动栏循环

来源:互联网 发布:淘宝怎么选购液晶电视 编辑:程序博客网 时间:2024/06/07 03:46
////  MCScrollView.h//  Promotion////  Created by Hu on 15/6/24.//  Copyright (c) 2015年 hmc. All rights reserved.//#import <UIKit/UIKit.h>@protocol MCScrollViewDelegate <NSObject>//点击的是第几个(从0开始)- (void)MCScrollViewClickIndex:(NSInteger)index;@end@interface MCScrollView : UIView@property (nonatomic, strong) id<MCScrollViewDelegate>delegate;- (id)initWithFrame:(CGRect)frame ParameterArray:(NSArray*)array;//开启自动循环- (void)startTimer;//关闭自动循环- (void)endTimer;- (void)updateDisplay:(NSArray*)array;@end////  MCScrollView.m//  Promotion////  Created by Hu on 15/6/24.//  Copyright (c) 2015年 hmc. All rights reserved.//#import "MCScrollView.h"#import "UIButton+WebCache.h"#define ScrollSec 3@interface MCScrollView()<UIScrollViewDelegate>{    int _sum;}@property (nonatomic, strong) UIScrollView      *scrollView;@property (nonatomic, strong) UIPageControl     *pageControl;@property (nonatomic, strong) NSTimer           *timer;@property (nonatomic, strong) NSArray           *array;@end@implementation MCScrollView- (id)initWithFrame:(CGRect)frame ParameterArray:(NSArray*)array{    if(self = [super initWithFrame:frame]){        _array = [[NSArray alloc] initWithArray:array];        if(_array.count){            [self initControl:frame];        }else{            frame.origin.x = 0;            frame.origin.y = 0;            UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:frame];            tempImageView.image = kImageWithName(@"scrollView_default");  //默认的背景图片,            tempImageView.backgroundColor = WHITE_COLOR;            [self addSubview:tempImageView];        }    }    return self;}- (void)updateDisplay:(NSArray*)array{    _array = array;    if(_array.count){        [self initControl:self.frame];    }    [self endTimer];    [self startTimer];}- (void)initControl:(CGRect)frame {    frame.origin.y = 0;    _sum = (int)_array.count+2;    _scrollView = [[UIScrollView alloc] initWithFrame:frame];    [_scrollView setContentSize:CGSizeMake(WIDTH*_sum, frame.size.height)];    _scrollView.pagingEnabled = YES;    _scrollView.bounces = YES;    _scrollView.delegate = self;    _scrollView.showsHorizontalScrollIndicator = NO;    _scrollView.showsVerticalScrollIndicator = NO;    _scrollView.userInteractionEnabled = YES;    [_scrollView setContentOffset:CGPointMake(0, 0)];    [_scrollView scrollRectToVisible:CGRectMake(WIDTH,0,WIDTH,frame.size.height) animated:NO];    _scrollView.backgroundColor = [UIColor whiteColor];    [self addSubview:_scrollView];    //添加视图到scrollview,BannerModel广告Model    for(int i = 0; i<_sum; i++){        BannerModel *model;        if(i == 0){            model = _array[_sum-3];        }else if(i == _sum-1){            model = _array[0];        }else{            model = _array[i-1];        }        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];        [btn sd_setImageWithURL:[NSURL URLWithString:model.photo_addr] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"default"]];        btn.frame = CGRectMake(WIDTH*i, 0, WIDTH, frame.size.height);//WIDTH屏幕宽度        btn.tag = i;        [btn addTarget:self action:@selector(clickAdvertisment:) forControlEvents:UIControlEventTouchUpInside];        [_scrollView addSubview:btn];    }    [_scrollView setContentOffset:CGPointMake(WIDTH, 0) animated:NO];    //初始化UIPageControl    _pageControl = [[UIPageControl alloc] init];    CGRect frames = _scrollView.frame;    CGSize size = [_pageControl sizeForNumberOfPages:_sum-2];    frames.origin.y = frames.size.height-size.height+20;    frames.size = size;    _pageControl.frame = frames;    _pageControl.center = CGPointMake(WIDTH/2, frames.origin.y);    [_pageControl setCurrentPageIndicatorTintColor:[UIColor redColor]];    [_pageControl setPageIndicatorTintColor:[UIColor colorWithPatternImage:kImageWithName(@"scrollVIew_Unselected")]];    _pageControl.numberOfPages = _sum-2;    _pageControl.currentPage = 0;    [_pageControl addTarget:self action:@selector(turnPage) forControlEvents:UIControlEventValueChanged];    [self addSubview:_pageControl];}#pragma mark-Method- (void)turnPage{}- (void)clickAdvertisment:(UIButton*)sender{    [self.delegate MCScrollViewClickIndex:sender.tag-1];}- (void)startTimer{    if(_array.count > 1){        if(_timer){            [self endTimer];        }        _timer = [NSTimer scheduledTimerWithTimeInterval:ScrollSec target:self selector:@selector(runLoop) userInfo:nil repeats:YES];    }}- (void)endTimer{    if(_array.count > 1){        [_timer invalidate];        _timer = nil;    }}#pragma mark-初始化scrollview自动循环- (void)runLoop{    int currentPage = _scrollView.contentOffset.x/WIDTH;    if(currentPage == 0){        [_scrollView setContentOffset:CGPointMake(WIDTH*_array.count, 0) animated:NO];    }else if(currentPage == _array.count){        [_scrollView setContentOffset:CGPointMake(WIDTH*0, 0) animated:NO];        [_scrollView setContentOffset:CGPointMake(WIDTH*1, 0) animated:YES];    }else{        [_scrollView setContentOffset:CGPointMake(WIDTH+currentPage*WIDTH, 0) animated:YES];    }    _pageControl.currentPage = currentPage%_array.count;    [_pageControl updateCurrentPageDisplay];}#pragma mark-UIScrollViewDelegate- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    int currentPage = scrollView.contentOffset.x/WIDTH;    if(currentPage == 0){        [_scrollView setContentOffset:CGPointMake(WIDTH*_array.count, 0) animated:NO];    }else if(currentPage == _sum-1){        [_scrollView setContentOffset:CGPointMake(WIDTH*1, 0) animated:NO];    }    currentPage = scrollView.contentOffset.x/WIDTH;    _pageControl.currentPage = currentPage-1;    [_pageControl updateCurrentPageDisplay];    [self startTimer];}- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{    [self endTimer];}@end
0 0
原创粉丝点击