iOS 广告轮播图

来源:互联网 发布:域名到期时间影响seo 编辑:程序博客网 时间:2024/05/19 12:12

//封装一个轮播图

//**************************************.h文件

#import <UIKit/UIKit.h>

typedef void (^scrollerviewclick)(NSInteger index);
@interface SliderView : UIView

@property(copy,nonatomic)scrollerviewclick  scrollerviewclickBlock;
/**
 *  图片个数
 */
@property (nonatomic, strong) NSArray *imageArray;

/**
 *  时间间隔
 */
@property (nonatomic, assign) NSTimeInterval timeInterval;

/**
 *  动画执行时间
 */
@property (nonatomic, assign) NSTimeInterval duration;

/**
 *  计时器
 */
@property (nonatomic, strong) NSTimer *timer;

/**
 *  保底一张默认图片
 */
@property (nonatomic, copy) NSString *defaultImg;



- (void)updateSlideShow;


@end


/***************************************/.m文件


#import "SliderView.h"
#import "UIImageView+WebCache.h"

@interface SliderView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIPageControl *pageControl;

@property (nonatomic, strong) NSMutableArray *imageViewArray;



@end

@implementation SliderView

- (NSTimer *)timer
{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:self.timeInterval target:self selector:@selector(automaticScorll:) userInfo:nil repeats:YES];
    }
    return _timer;
}

- (UIPageControl *)pageControl
{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height-20, self.frame.size.width, 20)];
        _pageControl.backgroundColor = [UIColor clearColor];
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.currentPage = 0;
    }
    return _pageControl;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.bounces = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
    }
    return _scrollView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _imageViewArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)updateSlideShow
{
    [self handleContentImageArray];
    [self addSubview:self.scrollView];
    for (int i = 0; i < self.imageViewArray.count; i ++)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
        imageView.userInteractionEnabled = YES;
        
        
     NSURL *url = [NSURL URLWithString:[self.imageViewArray[i] stringByReplacingOccurrencesOfString:@"https" withString:@"http"]];
        [imageView sd_setImageWithURL:url  placeholderImage:[UIImage imageNamed:self.defaultImg]];
        
        UITapGestureRecognizer *sdTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sdImageTap:)];
        [imageView addGestureRecognizer:sdTap];
        
        [self.scrollView addSubview:imageView];
    }
    [self addSubview:self.pageControl];
    [self.timer setFireDate:[NSDate dateWithTimeInterval:self.timeInterval sinceDate:[NSDate date]]];
}

- (void)handleContentImageArray
{
    [self.imageViewArray removeAllObjects];
    if (self.imageArray.count == 0 || self.imageViewArray == nil) {
        [self.imageViewArray addObject:self.defaultImg];
        self.pageControl.numberOfPages = 1;
        self.scrollView.contentOffset = CGPointMake(0, 0);
        self.scrollView.scrollEnabled = NO;
        [self.timer invalidate];
        self.scrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
    }
    else if (self.imageArray.count == 1)
    {
        [self.imageViewArray addObjectsFromArray:self.imageArray];
        self.scrollView.contentOffset = CGPointMake(0, 0);
        self.scrollView.scrollEnabled = NO;
        [self.timer invalidate];
        self.pageControl.numberOfPages = 1;
        self.scrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
    }
    else
    {
        [self.imageViewArray addObject:[self.imageArray lastObject]];
        [self.imageViewArray addObjectsFromArray:self.imageArray];
        [self.imageViewArray addObject:[self.imageArray firstObject]];
        self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
        self.pageControl.numberOfPages = self.imageArray.count;
        self.scrollView.contentSize = CGSizeMake(self.frame.size.width * self.imageViewArray.count, self.frame.size.height);
    }
}

- (void)automaticScorll:(NSTimer *)sender
{
    [UIView animateWithDuration:self.duration animations:^{
        
        self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + self.frame.size.width, 0);
    }];
    if (self.scrollView.contentOffset.x == (self.imageViewArray.count - 2) * self.frame.size.width) {
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer setFireDate:[NSDate distantFuture]];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.timer setFireDate:[NSDate dateWithTimeInterval:self.timeInterval sinceDate:[NSDate date]]];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat X = scrollView.contentOffset.x;
    if (X == 0)
    {
        self.pageControl.currentPage = self.imageArray.count;
        scrollView.contentOffset = CGPointMake(self.frame.size.width * (self.imageViewArray.count - 2), 0);
    }
    else if (X == (self.imageViewArray.count - 1) * self.frame.size.width)
    {
        self.pageControl.currentPage = 0;
        scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    else if (X >= self.frame.size.width && X <= (self.imageViewArray.count - 2) * self.frame.size.width)
    {
        self.pageControl.currentPage = scrollView.contentOffset.x/self.frame.size.width - 1;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat X = scrollView.contentOffset.x;
    if (X/self.frame.size.width < 1)
    {
        self.pageControl.currentPage = self.imageArray.count;
    }
    else if (X/self.frame.size.width >= (self.imageViewArray.count - 1))
    {
        self.pageControl.currentPage = 0;
        scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    else
    {
        self.pageControl.currentPage = scrollView.contentOffset.x/self.frame.size.width-1;
    }
}


#pragma mark 点击事件
- (void)sdImageTap:(UITapGestureRecognizer *)tap{
    
    if (self.scrollerviewclickBlock) {
        self.scrollerviewclickBlock(self.pageControl.currentPage);
    }


}

@end

//-----------------------

@property (nonatomic, strong) SliderView *slide;

- (void)viewDidLoad {

    self.slide = [[SliderView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth,kDeviceWidth *0.4)];
    self.slide.timeInterval = 6;
    self.slide.duration = 0.25;
    self.slide.defaultImg = @"mybackground";
    [self.view  addSubview:self.slide];

//self.advertisingPositionArray 数组存从网络请求下来的广告图

      self.slide.imageArray = self.advertisingPositionArray;
                [self.slide updateSlideShow];
    
    /* 轮播图的 点击事件  */
    __weak typeof(self) weakSelf = self;
    self.slide.scrollerviewclickBlock = ^(NSInteger index){

        SdDetailViewController *sdController = [[SdDetailViewController alloc] init];
        sdController.sdNumber = index;
        [weakSelf.navigationController pushViewController:sdController animated:NO];

    };


0 0
原创粉丝点击