Latest 1.0.0
Homepage https://github.com/gaoleegin/GLJInfiniteScrollView
License MIT
Platforms ios 6.0, requires ARC
Authors

(logo)

GLJInfiniteScrollView

  • 一个很好用的图片无限轮播控件

如何使用 GLJInfiniteScrollView

  • cocoaPods导入 : pod 'GLJInfiniteScrollView'
  • 手动导入:
    • 将 `GLJInfiniteScrollView`文件夹里面的两个文件 GLJInfiniteScrollView.h 和 GLJInfiniteScrollView.m 拖入工程中
    • 导入头文件 GLJInfiniteScrollView.h

GLJInfiniteScrollView.h 文件 接口调用


#import <UIKit/UIKit.h>

@interface GLJInfiniteScrollView : UIView

/** 图片数组 */
@property (strong, nonatomic) NSArray *images;

/** 分页符 */
@property (weak, nonatomic, readonly) UIPageControl *pageControl;

/** 控制横向滚动还是竖向滚动 */
@property (assign, nonatomic, getter=isScrollDirectionPortrait) BOOL scrollDirectionPortrait;

@end

GLJInfiniteScrollView.m 文件 接口调用


#import "GLJInfiniteScrollView.h"

static int const ImageViewCount = 3;

@interface GLJInfiniteScrollView() <UIScrollViewDelegate>
@property (weak, nonatomic) UIScrollView *scrollView;
@property (weak, nonatomic) NSTimer *timer;
@end

@implementation GLJInfiniteScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        // 滚动视图
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.pagingEnabled = YES;
        scrollView.bounces = NO;
        scrollView.delegate = self;
        [self addSubview:scrollView];
        self.scrollView = scrollView;

        // 图片控件
        for (int i = 0; i<ImageViewCount; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            [scrollView addSubview:imageView];
        }

        // 页码视图
        UIPageControl *pageControl = [[UIPageControl alloc] init];
        [self addSubview:pageControl];
        _pageControl = pageControl;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.scrollView.frame = self.bounds;
    if (self.isScrollDirectionPortrait) {
        self.scrollView.contentSize = CGSizeMake(0, ImageViewCount * self.bounds.size.height);
    } else {
        self.scrollView.contentSize = CGSizeMake(ImageViewCount * self.bounds.size.width, 0);
    }

    for (int i = 0; i<ImageViewCount; i++) {
        UIImageView *imageView = self.scrollView.subviews[i];

        if (self.isScrollDirectionPortrait) {
            imageView.frame = CGRectMake(0, i * self.scrollView.frame.size.height, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        } else {
            imageView.frame = CGRectMake(i * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        }
    }

    CGFloat pageW = 80;
    CGFloat pageH = 20;
    CGFloat pageY = self.scrollView.frame.size.height - pageH;
    self.pageControl.frame = CGRectMake((self.frame.size.width - pageW) * 0.5, pageY, pageW, pageH);

}

- (void)setImages:(NSArray *)images
{
    _images = images;

    // 设置页码
    self.pageControl.numberOfPages = images.count;
    self.pageControl.currentPage = 0;

    // 设置内容
    [self updateContent];

    // 开始定时器
    [self startTimer];
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 找出最中间的那个图片控件
    NSInteger page = 0;
    CGFloat minDistance = MAXFLOAT;
    for (int i = 0; i<self.scrollView.subviews.count; i++) {
        UIImageView *imageView = self.scrollView.subviews[i];
        CGFloat distance = 0;
        if (self.isScrollDirectionPortrait) {
            distance = ABS(imageView.frame.origin.y - scrollView.contentOffset.y);
        } else {
            distance = ABS(imageView.frame.origin.x - scrollView.contentOffset.x);
        }
        if (distance < minDistance) {
            minDistance = distance;
            page = imageView.tag;
        }
    }
    self.pageControl.currentPage = page;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self startTimer];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self updateContent];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self updateContent];
}

#pragma mark - 内容更新
- (void)updateContent
{
    // 设置图片
    for (int i = 0; i<self.scrollView.subviews.count; i++) {
        UIImageView *imageView = self.scrollView.subviews[i];
        NSInteger index = self.pageControl.currentPage;
        if (i == 0) {
            index--;
        } else if (i == 2) {
            index++;
        }
        if (index < 0) {
            index = self.pageControl.numberOfPages - 1;
        } else if (index >= self.pageControl.numberOfPages) {
            index = 0;
        }
        imageView.tag = index;
        imageView.image = self.images[index];
    }

    // 设置偏移量在中间
    if (self.isScrollDirectionPortrait) {
        self.scrollView.contentOffset = CGPointMake(0, self.scrollView.frame.size.height);
    } else {
        self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
    }
}

#pragma mark - 定时器处理
- (void)startTimer
{
    NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(next) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
}

- (void)stopTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

- (void)next
{
    if (self.isScrollDirectionPortrait) {
        [self.scrollView setContentOffset:CGPointMake(0, 2 * self.scrollView.frame.size.height) animated:YES];
    } else {
        [self.scrollView setContentOffset:CGPointMake(2 * self.scrollView.frame.size.width, 0) animated:YES];
    }
}
@end

调用代码


GLJInfiniteScrollView *scrollView = [[GLJInfiniteScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.images = @[
                          [UIImage imageNamed:@"img_00.jpg"],
                          [UIImage imageNamed:@"img_01.jpg"],
                          [UIImage imageNamed:@"img_02.jpg"],
                          [UIImage imageNamed:@"img_03.jpg"]
                          ];
    scrollView.pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    scrollView.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    scrollView.scrollDirectionPortrait = NO;
    [self.view addSubview:scrollView];

备注

  • 一个简单的图片轮滑的控件,可以在这个基础上进行扩展
  • 希望大家在用的过程中有什么建议或好的想法,可以告诉我,共同进步,共同交流

许可证

  • GLJInfiniteScrollView 使用 MIT 许可证,详情见 LICENSE 文件。

Latest podspec

{
    "name": "GLJInfiniteScrollView",
    "version": "1.0.0",
    "license": "MIT",
    "summary": "Harness the power of Auto Layout NSLayoutConstraints with a simplified, chainable and expressive syntax.",
    "homepage": "https://github.com/gaoleegin/GLJInfiniteScrollView",
    "authors": {
        "Jonas Budelmann": "[email protected]"
    },
    "source": {
        "git": "https://github.com/gaoleegin/GLJInfiniteScrollView.git",
        "tag": "1.0.0"
    },
    "description": "GLJInfiniteScrollView is a light-weight layout framework which wraps AutoLayout with a nicer syntax.nMasonry has its own layout DSL which provides a chainable way of describing yournNSLayoutConstraints which results in layout code which is more concise and readable.nMasonry supports iOS and Mac OSX.",
    "source_files": "GLJInfiniteScrollView/*.{h,m}",
    "ios": {
        "frameworks": [
            "Foundation",
            "UIKit"
        ]
    },
    "platforms": {
        "ios": "6.0"
    },
    "requires_arc": true
}

Pin It on Pinterest

Share This