说明!!!
此控件只是能展示本地图片,如果想要展示网络图片、或者其他视图,请在代码里自行修改。我只是个页面仔!!!还有很多不足的地方请见谅!!
//
// YTRotationChartView.h
// CaterCompanySupervision
//
// Created by 孙东日 on 16/4/2020.
// Copyright © 2020 孙东日. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface YTRotationChartView : UIView
-(instancetype)initWithFrame:(CGRect)frame itemArray:(NSMutableArray <NSString *>*)itemArray;
@property (nonatomic) BOOL showPageControl;
@property (nonatomic,strong) UIColor *pageIndicatorTintColor;
@property (nonatomic,strong) UIColor *currentPageIndicatorTintColor;
@end
NS_ASSUME_NONNULL_END
----------------------------------------------------------------------------
//
// YTRotationChartView.m
// CaterCompanySupervision
//
// Created by 孙东日 on 16/4/2020.
// Copyright © 2020 孙东日. All rights reserved.
//
#import "YTRotationChartView.h"
@interface YTRotationChartView()<UIScrollViewDelegate>
@property (strong,nonatomic) UIScrollView *mainScrollView;
@property (strong,nonatomic) UIPageControl *pageControl;
@property (strong,nonatomic) NSMutableArray *itemsArray;
@property (strong,nonatomic) NSMutableArray *originalItemsArray;
@end
@implementation YTRotationChartView
-(instancetype)initWithFrame:(CGRect)frame itemArray:(NSMutableArray*)itemArray{
self = [super initWithFrame:frame];
if (self) {
self.itemsArray = itemArray;
self.originalItemsArray = [NSMutableArray arrayWithArray:itemArray];
id obj = [itemArray lastObject];
[self.itemsArray insertObject:obj atIndex:0];
[self configMainView];
}
return self;
}
-(void)configMainView{
//装配视图
[self addSubview:self.mainScrollView];
[self insertSubview:self.pageControl aboveSubview:self.mainScrollView];
//加载数据
for (int i = 0; i < self.itemsArray.count; i++) {
UITapGestureRecognizer *imgTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTheImg:)];
UIImageView *img = [[UIImageView alloc] init];
img.frame = CGRectMake(i*self.mainScrollView.width, 0, self.mainScrollView.width, self.mainScrollView.height);
img.image = [UIImage imageNamed:self.itemsArray[i]];
img.userInteractionEnabled = YES;
img.contentMode = UIViewContentModeScaleAspectFit;
[img addGestureRecognizer:imgTap];
[self.mainScrollView addSubview:img];
}
}
#pragma mark - Actions
-(void)tapTheImg:(UIGestureRecognizer*)recognizer{
NSLog(@"点击了,%ld",(long)self.pageControl.currentPage);
}
#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
double pageNum = scrollView.contentOffset.x/scrollView.width;
if (pageNum < 0) {
self.mainScrollView.contentOffset = CGPointMake(self.originalItemsArray.count*self.width, 0);
}
if (pageNum > self.originalItemsArray.count) {
self.mainScrollView.contentOffset = CGPointMake(0, 0);
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
double pageNum = scrollView.contentOffset.x/scrollView.width;
if (pageNum < 0) {
pageNum = self.originalItemsArray.count;
}
if (pageNum > self.originalItemsArray.count || pageNum == self.originalItemsArray.count) {
pageNum = 0;
}
self.pageControl.currentPage = (int)(pageNum);
}
#pragma mark- Getter and Setter
-(UIScrollView *)mainScrollView{
if (!_mainScrollView) {
_mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
_mainScrollView.contentSize = CGSizeMake(self.itemsArray.count*self.width,self.height);
_mainScrollView.delegate = self;
_mainScrollView.backgroundColor = [UIColor blackColor];
_mainScrollView.pagingEnabled = YES;
_mainScrollView.showsVerticalScrollIndicator = NO;
_mainScrollView.showsHorizontalScrollIndicator = NO;
}
return _mainScrollView;
}
-(UIPageControl *)pageControl{
if (!_pageControl) {
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.height - 30, self.width, 30)];
_pageControl.numberOfPages = self.originalItemsArray.count;
_pageControl.currentPage = 0;
_pageControl.pageIndicatorTintColor = [UIColor blueColor];
_pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
}
return _pageControl;
}
-(void)setShowPageControl:(BOOL)showPageControl{
_showPageControl = showPageControl;
self.pageControl.hidden = _showPageControl;
}
-(void)setPageIndicatorTintColor:(UIColor *)pageIndicatorTintColor{
_pageIndicatorTintColor = pageIndicatorTintColor;
_pageControl.pageIndicatorTintColor = _pageIndicatorTintColor;
}
-(void)setCurrentPageIndicatorTintColor:(UIColor *)currentPageIndicatorTintColor{
_currentPageIndicatorTintColor = currentPageIndicatorTintColor;
_pageControl.currentPageIndicatorTintColor = _currentPageIndicatorTintColor;
}
@end
-----------------------------------------------------------------------------------------------
具体用法!!!
-(YTRotationChartView*)rotationChartView{
if (!_rotationChartView) {
_rotationChartView = [[YTRotationChartView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, HeaderViewHeight) itemArray:[NSMutableArray arrayWithArray:@[@"2.png",@"3.png",@"1.png"]]];
_rotationChartView.currentPageIndicatorTintColor = [UIColor systemRedColor];
}
return _rotationChartView;
}
[self.view addSubview:self.rotationChartView];