BBACyclePagerView.h 4.32 KB
//
//  BBACyclePagerView.h
//  CyclePagerView
//
//  Created by xuxiaonan on 2017/9/28.
//  Copyright © 2017年 xuxiaonan. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "BBACyclePagerTransformLayout.h"

NS_ASSUME_NONNULL_BEGIN

// pagerView scrolling direction
typedef NS_ENUM(NSUInteger, BBAPagerScrollDirection) {
    BBAPagerScrollDirectionLeft,
    BBAPagerScrollDirectionRight,
};

@class BBACyclePagerView;
@protocol BBACyclePagerViewDataSource <NSObject>

- (NSInteger)numberOfItemsInPagerView:(BBACyclePagerView *)pageView;
- (__kindof UICollectionViewCell *)pagerView:(BBACyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index;

/**
 return pagerView layout,and cache layout
 */
- (BBACyclePagerViewLayout *)layoutForPagerView:(BBACyclePagerView *)pageView;

@end

@protocol BBACyclePagerViewDelegate <NSObject>

@optional

/**
 pagerView did scroll to new index page
 */
- (void)pagerView:(BBACyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex;

/**
 pagerView did selected item cell
 */
- (void)pagerView:(BBACyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index;

// custom layout
- (void)pagerView:(BBACyclePagerView *)pageView initializeTransformAttributes:(UICollectionViewLayoutAttributes *)attributes;
- (void)pagerView:(BBACyclePagerView *)pageView applyTransformToAttributes:(UICollectionViewLayoutAttributes *)attributes;


// scrollViewDelegate

- (void)pagerViewDidScroll:(BBACyclePagerView *)pageView;
- (void)pagerViewWillBeginDragging:(BBACyclePagerView *)pageView;
- (void)pagerViewDidEndDragging:(BBACyclePagerView *)pageView willDecelerate:(BOOL)decelerate;
- (void)pagerViewWillBeginDecelerating:(BBACyclePagerView *)pageView;
- (void)pagerViewDidEndDecelerating:(BBACyclePagerView *)pageView;
- (void)pagerViewWillBeginScrollingAnimation:(BBACyclePagerView *)pageView;
- (void)pagerViewDidEndScrollingAnimation:(BBACyclePagerView *)pageView;

@end

@interface BBACyclePagerView : UIView

// will be automatically resized to track the size of the pagerView
@property (nonatomic, strong, nullable) UIView *backgroundView;

@property (nonatomic, weak, nullable) id<BBACyclePagerViewDataSource> dataSource;
@property (nonatomic, weak, nullable) id<BBACyclePagerViewDelegate> delegate;

// pager view layout
@property (nonatomic, strong, readonly) BBACyclePagerViewLayout *layout;

/**
 is infinite cycle pageview
 */
@property (nonatomic, assign) BOOL isInfiniteLoop;

/**
 pagerView automatic scroll time interval, default 0,disable automatic
 */
@property (nonatomic, assign) CGFloat autoScrollInterval;

/**
 current page index
 */
@property (nonatomic, assign, readonly) NSInteger curIndex;

// scrollView property
@property (nonatomic, assign, readonly) CGPoint contentOffset;
@property (nonatomic, assign, readonly) BOOL tracking;
@property (nonatomic, assign, readonly) BOOL dragging;
@property (nonatomic, assign, readonly) BOOL decelerating;

/**
 set scroll not like page
 */
@property (nonatomic, assign) BOOL scrollNormal;

/**
 reload data, !!important!!: will clear layout and call delegate layoutForPagerView
 */
- (void)reloadData;

/**
 update data is reload data, but not clear layuot
 */
- (void)updateData;

/**
 if you only want update layout
 */
- (void)setNeedUpdateLayout;

/**
 will set layout nil and call delegate->layoutForPagerView
 */
- (void)setNeedClearLayout;

/**
 current index cell in pagerView
 */
- (__kindof UICollectionViewCell * _Nullable)curIndexCell;

/**
 visible cells in pageView
 */
- (NSArray<__kindof UICollectionViewCell *> *_Nullable)visibleCells;


/**
 visible pageView indexs, maybe repeat index
 */
- (NSArray *)visibleIndexs;

/**
 scroll to item at index
 */
- (void)scrollToItemAtIndex:(NSInteger)index animate:(BOOL)animate;

/**
 scroll to next or pre item
 */
- (void)scrollToNearlyIndexAtDirection:(BBAPagerScrollDirection)direction animate:(BOOL)animate;

/**
 register pager view cell with class
 */
- (void)registerClass:(Class)Class forCellWithReuseIdentifier:(NSString *)identifier;

/**
 register pager view cell with nib
 */
- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;

/**
 dequeue reusable cell for pagerView
 */
- (__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndex:(NSInteger)index;

@end

NS_ASSUME_NONNULL_END