Skip to content

Commit

Permalink
ability to set and retrieve the currentIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhenry committed Aug 6, 2014
1 parent fa71c7f commit 6de1a5c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Classes/MHYahooParallaxView.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ typedef enum {
@interface MHYahooParallaxView : UIView<UICollectionViewDelegate,UICollectionViewDataSource> {
CGFloat _width;
CGFloat _height;
NSInteger _currentPageIndex;
CGFloat _pageDivisor;
NSInteger _dataCount;
}
Expand All @@ -66,11 +65,13 @@ typedef enum {
@property (nonatomic) MHYahooParallaxViewType parallaxViewType;
@property (nonatomic) id<MHYahooParallaxViewDatasource> datasource;
@property (nonatomic) id<MHYahooParallaxViewDelegate> delegate;
@property (nonatomic) NSInteger currentIndex;

- (id)initWithFrame:(CGRect)frame withViewType:(MHYahooParallaxViewType) viewType;

- (void) registerClass:(__unsafe_unretained Class) cellClass forCellWithReuseIdentifier:(NSString *) reuseIdentifier;
- (UICollectionViewCell*) cellForItemAtIndexPath:(NSIndexPath*)indexPath;
- (UICollectionViewCell*) dequeueReusableCellWithReuseIdentifier:(NSString *) reuseIdentifier forIndexPath:(NSIndexPath*)indexPath ;

- (void) setCurrentIndex:(NSInteger)index animated:(BOOL) animated;
@end
22 changes: 17 additions & 5 deletions Classes/MHYahooParallaxView.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ @implementation MHYahooParallaxView
@synthesize delegate = _delegate;
@synthesize datasource = _datasource;
@synthesize parallaxViewType = _parallaxViewType;
@synthesize currentIndex = _currentIndex;

- (id)initWithFrame:(CGRect)frame {
return [self initWithFrame:frame withViewType:MHYahooParallaxViewTypeHorizontal];
Expand Down Expand Up @@ -95,17 +96,17 @@ - (UICollectionViewCell*) dequeueReusableCellWithReuseIdentifier:(NSString *) re


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
_currentPageIndex = scrollView.contentOffset.x/_width;
_currentIndex = scrollView.contentOffset.x/_width;


if(_parallaxViewType == MHYahooParallaxViewTypeHorizontal){

NSInteger leftIndex = -1;
NSInteger rightIndex = -1;

leftIndex = _currentPageIndex;
leftIndex = _currentIndex;

if(_currentPageIndex < (_dataCount - 1)) {
if(_currentIndex < (_dataCount - 1)) {
rightIndex = leftIndex + 1;
}

Expand All @@ -124,9 +125,9 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSInteger topIndex = -1;
NSInteger bottomIndex = -1;

topIndex = _currentPageIndex;
topIndex = _currentIndex;

if(_currentPageIndex < (_dataCount - 1)) {
if(_currentIndex < (_dataCount - 1)) {
bottomIndex = topIndex + 1;
}

Expand All @@ -142,7 +143,18 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
}
}

- (void) setCurrentIndex:(NSInteger)index {
[self setCurrentIndex:index animated:NO];
}

- (void) setCurrentIndex:(NSInteger)index animated:(BOOL) animated {
_currentIndex = index;
[_parallaxCollectionView setContentOffset:CGPointMake(index * _width, 0.0f) animated:animated];
}

- (void) dealloc {
_parallaxCollectionView = nil;
}


@end

0 comments on commit 6de1a5c

Please sign in to comment.