iOS 无限滚动图片控件。

说明!!!

此控件只是能展示本地图片,如果想要展示网络图片、或者其他视图,请在代码里自行修改。我只是个页面仔!!!还有很多不足的地方请见谅!!

//

//  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];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值