iOS首页广告滚动栏循环

//
//  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值