第四篇、图片轮播查看器

简介:

  这是一个图片轮播查看器,代码还需要进一步的封装。

使用:

PictureCircle *APictureCircle = [[PictureCircle alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 120) array:Array];
 APictureCircle.delegate =self;
 [self.FPictureCircleView addSubview:APictureCircle];

实现:

#import <UIKit/UIKit.h>



@protocol PictureCircleDelegate <NSObject>

- (void)TouchIndex:(NSInteger)AInteger;

@end

@interface PictureCircle : UIView<DownloadDelete>

@property (nonatomic, strong) UIScrollView * picScrollView; // 轮播图

@property (nonatomic, strong) UIPageControl * pageControl; // 显示条

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

@property (nonatomic, assign) NSInteger totalPicCount; // 总共多少个图片

- (id)initWithFrame:(CGRect)frame array:(NSMutableArray *)picArray; // 根据传入数组来初始化此view

@property (weak,nonatomic) id<PictureCircleDelegate>delegate;

@end
#import "PictureCircle.h"

@interface PictureCircle ()<UIScrollViewDelegate> @property (nonatomic, strong) NSMutableArray * dataArray; @property (nonatomic, assign) NSInteger currentPageIndex; @property (strong,nonatomic) NSMutableDictionary *FImageViewDictionary; - (void)addTimer; // 添加定时器 - (void)removeTimer; // 移除定时器 @end @implementation PictureCircle - (id)initWithFrame:(CGRect)frame array:(NSMutableArray *)picArray { self = [super initWithFrame:frame]; if (self) { [self setupScrollViewWithArray:picArray]; } return self; } - (void)setupScrollViewWithArray:(NSMutableArray *)picArray { // 图片的宽高和起始坐标 CGFloat imageW = KScreenWidth; CGFloat imageH = self.frame.size.height; CGFloat imageY = 0; // 图片数 if (![picArray count]) { return; } self.dataArray = [NSMutableArray arrayWithArray:picArray]; [self.dataArray insertObject:[picArray objectAtIndex:(picArray.count - 1)] atIndex:0]; [self.dataArray addObject:[picArray objectAtIndex:0]]; NSInteger totalCount = self.dataArray.count; self.totalPicCount = totalCount;
// 轮播图 UIScrollView * tempScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, imageW, imageH)]; tempScrollView.showsHorizontalScrollIndicator = NO; tempScrollView.showsVerticalScrollIndicator = NO; self.picScrollView = tempScrollView; // 添加图片 for (int i = 0; i < totalCount; i ++) { UIImageView * tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageW * i, imageY, imageW, imageH)]; if (!self.FImageViewDictionary) { self.FImageViewDictionary = [[NSMutableDictionary alloc]init]; } [self.FImageViewDictionary setObject:tempImageView forKey:[NSString stringWithFormat:@"%d",i]]; tempImageView.tag = i; [tempImageView setUserInteractionEnabled:YES]; // 设置图片 NSString * tempString1 = [[self.dataArray objectAtIndex:i] objectForKey:@"feature_img"]; // 添加点击手势 UITapGestureRecognizer *ASingTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(SingTap:)]; [tempImageView addGestureRecognizer:ASingTap]; [self.picScrollView addSubview:tempImageView]; } // 设置scrollView的滚动范围 self.picScrollView.contentSize = CGSizeMake(totalCount * imageW, 0); self.picScrollView.contentOffset = CGPointMake(KScreenWidth, 0); // 设置分页 self.picScrollView.pagingEnabled = YES; // 监听scrollView的滚动 self.picScrollView.delegate = self; [self.picScrollView setScrollEnabled:YES]; self.picScrollView.bounces = NO; // 设置小圆点的起始位置 CGFloat APageControlX = KScreenWidth -30 - totalCount * 6; self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(APageControlX, imageH -20, 25, 20)]; self.pageControl.numberOfPages = totalCount - 2; self.pageControl.currentPage = 0; [self addSubview:self.picScrollView]; [self addSubview:self.pageControl]; if(1 == [picArray count]) { [self.pageControl setHidden:YES]; [self.picScrollView setScrollEnabled:NO]; } else { [self addTimer]; } } // 单击手势监听 - (void)SingTap:(UITapGestureRecognizer *)ATapGestureRecognizer { [self.delegate TouchIndex:ATapGestureRecognizer.view.tag-1]; } - (void)addTimer { self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(nextImage) userInfo:nil repeats:YES]; } - (void)removeTimer { [self.timer invalidate]; } // 播放下一张图片 - (void)nextImage { // 设置滚动 CGFloat scollViewW = self.picScrollView.frame.size.width; CGFloat offsetX = self.picScrollView.contentOffset.x; int page = floor((offsetX - scollViewW / 2) / scollViewW) + 1; self.currentPageIndex = page + 1; if (self.currentPageIndex > 0 && self.currentPageIndex < self.dataArray.count) { // 滚动scrollView CGFloat x = self.currentPageIndex * self.picScrollView.frame.size.width; [UIView animateWithDuration:0.2 animations:^{ self.picScrollView.contentOffset = CGPointMake(x, 0); }]; if (self.currentPageIndex == self.dataArray.count - 1) { self.currentPageIndex ++; } } if (self.currentPageIndex == self.dataArray.count) { [_picScrollView setContentOffset:CGPointMake(KScreenWidth, 0)]; } if (self.currentPageIndex == 0) { [_picScrollView setContentOffset:CGPointMake((self.dataArray.count - 2) * KScreenWidth, 0)]; } } #pragma mark - scrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat scollViewW = self.picScrollView.frame.size.width; CGFloat offsetX = self.picScrollView.contentOffset.x; int page = floor((offsetX - scollViewW / 2) / scollViewW) + 1; self.currentPageIndex = page; self.pageControl.currentPage = page - 1; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [self removeTimer]; self.timer = nil; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { [self addTimer]; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (self.currentPageIndex == 0) { [_picScrollView setContentOffset:CGPointMake((self.dataArray.count - 2) * KScreenWidth, 0)]; } if (self.currentPageIndex == (self.dataArray.count - 1)) { [_picScrollView setContentOffset:CGPointMake(KScreenWidth, 0)]; } } - (void)dealloc { [self removeTimer]; self.timer = nil; } @end
原文地址:https://www.cnblogs.com/HJQ2016/p/5791119.html