Skip to content

Commit

Permalink
Added a new "selectedPage" property. Allows for a custom defined page…
Browse files Browse the repository at this point in the history
… index (both initial and at run-time).
  • Loading branch information
manijak committed Oct 26, 2016
1 parent e64fdb9 commit 0fe37b0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions DKCarouselView/DKCarouselView.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ typedef void(^DKCarouselViewDidChangeBlock)(DKCarouselView *view, NSInteger inde
// set infinite slide or not, defaults to NO.
@property (nonatomic, assign, getter = isFinite) BOOL finite;

// set selected page index
@property (nonatomic, assign) NSUInteger selectedPage;

- (void)setItems:(NSArray *)items;

// auto paging.
Expand Down
13 changes: 12 additions & 1 deletion DKCarouselView/DKCarouselView.m
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ - (CGSize)indicatorSize {
return [self.pageControl sizeForNumberOfPages:self.pageControl.numberOfPages];
}

- (void)setSelectedPage: (NSUInteger)selectedPage {
self.pageControl.currentPage = self.currentPage = selectedPage;
if (self.carouselItemViews.count > selectedPage){
if (self.finite) {
[self.scrollView setContentOffset:CGPointMake(kScrollViewFrameWidth * selectedPage, 0) animated:YES];
}else{
[self setupViews];
}
}
}

-(void)setItems:(NSArray *)items {
if (items == nil) return;

Expand All @@ -219,7 +230,7 @@ -(void)setItems:(NSArray *)items {
_items = [items copy];

self.pageControl.numberOfPages = _items.count;
self.pageControl.currentPage = self.currentPage = 0;
self.pageControl.currentPage = self.currentPage;

_scrollView.scrollEnabled = _items.count > 1;

Expand Down

0 comments on commit 0fe37b0

Please sign in to comment.