ATPagingView.h 3.08 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
//
//  Copyright 2011 Andrey Tarantsov. Distributed under the MIT license.
//

#import <Foundation/Foundation.h>

//this enum added by Lee, for UIScrollView Direction
typedef enum {
    ATPagingViewVertical,
    ATPagingViewHorizontal
} ATPagingViewDirection;

@protocol ATPagingViewDelegate;

// a wrapper around UIScrollView in (horizontal) paging mode, with an API similar to UITableView
@interface ATPagingView : UIView {
    // subviews
    UIScrollView *_scrollView;

    // properties
    id<ATPagingViewDelegate> __weak _delegate;
    CGFloat _gapBetweenPages;
    NSInteger _pagesToPreload;

    // state
    NSInteger _pageCount;
    NSInteger _currentPageIndex;
    NSInteger _firstLoadedPageIndex;
    NSInteger _lastLoadedPageIndex;
    NSMutableSet *_recycledPages;
    NSMutableSet *_visiblePages;

    NSInteger _previousPageIndex;

    BOOL _rotationInProgress;
    BOOL _scrollViewIsMoving;
    BOOL _recyclingEnabled;
}

@property(nonatomic, assign) ATPagingViewDirection direction; //default is ATPagingViewHorizontal;

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

@property(nonatomic, assign) CGFloat gapBetweenPages;  // default is 20

@property(nonatomic, assign) NSInteger pagesToPreload;  // number of invisible pages to keep loaded to each side of the visible pages, default is 1

@property(nonatomic, readonly) NSInteger pageCount;

@property(nonatomic, assign) NSInteger currentPageIndex;
@property(nonatomic, assign, readonly) NSInteger previousPageIndex; // only for reading inside currentPageDidChangeInPagingView

@property(nonatomic, assign, readonly) NSInteger firstVisiblePageIndex;
@property(nonatomic, assign, readonly) NSInteger lastVisiblePageIndex;

@property(nonatomic, assign, readonly) NSInteger firstLoadedPageIndex;
@property(nonatomic, assign, readonly) NSInteger lastLoadedPageIndex;
@property(nonatomic)  UIScrollView *scrollView;

@property(nonatomic, assign) BOOL moving;
@property(nonatomic, assign) BOOL recyclingEnabled;

- (void)reloadData;  // must be called at least once to display something

- (UIView *)viewForPageAtIndex:(NSUInteger)index;  // nil if not loaded

- (UIView *)dequeueReusablePage;  // nil if none

- (void)willAnimateRotation;  // call this from willAnimateRotationToInterfaceOrientation:duration:

- (void)didRotate;  // call this from didRotateFromInterfaceOrientation:

- (id)initVerticalWithFrame:(CGRect)frame;
@end


@protocol ATPagingViewDelegate <NSObject>

@required

- (NSInteger)numberOfPagesInPagingView:(ATPagingView *)pagingView;

- (UIView *)viewForPageInPagingView:(ATPagingView *)pagingView atIndex:(NSInteger)index;

@optional

- (void)currentPageDidChangeInPagingView:(ATPagingView *)pagingView;

- (void)pagesDidChangeInPagingView:(ATPagingView *)pagingView;

// a good place to start and stop background processing
- (void)pagingViewWillBeginMoving:(ATPagingView *)pagingView;
- (void)pagingViewDidEndMoving:(ATPagingView *)pagingView;

@end


@interface ATPagingViewController : UIViewController <ATPagingViewDelegate> {
    ATPagingView *_pagingView;
}

@property(nonatomic, strong) IBOutlet ATPagingView *pagingView;

@end