Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

Add A Vertical Orientation Option #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions AKPickerViewSample/AKPickerView/AKPickerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ typedef NS_ENUM(NSInteger, AKPickerViewStyle) {
AKPickerViewStyleFlat
};

typedef NS_ENUM(NSUInteger, AKPickerViewOrientation) {
AKPickerViewOrientationHorizontal = 1,
AKPickerViewOrientationVertical,
};

@class AKPickerView;

@protocol AKPickerViewDataSource <NSObject>
Expand Down Expand Up @@ -42,6 +47,7 @@ typedef NS_ENUM(NSInteger, AKPickerViewStyle) {
@property (nonatomic, assign) CGFloat fisheyeFactor; // 0...1; slight value recommended such as 0.0001
@property (nonatomic, assign, getter=isMaskDisabled) BOOL maskDisabled;
@property (nonatomic, assign) AKPickerViewStyle pickerViewStyle;
@property (nonatomic, assign) AKPickerViewOrientation pickerViewOrientation;
@property (nonatomic, assign, readonly) NSUInteger selectedItem;
@property (nonatomic, assign, readonly) CGPoint contentOffset;

Expand Down
131 changes: 105 additions & 26 deletions AKPickerViewSample/AKPickerView/AKPickerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ - (void)initialize
self.textColor = self.textColor ?: [UIColor darkGrayColor];
self.highlightedTextColor = self.highlightedTextColor ?: [UIColor blackColor];
self.pickerViewStyle = self.pickerViewStyle ?: AKPickerViewStyle3D;
self.pickerViewOrientation = self.pickerViewOrientation ?: AKPickerViewOrientationHorizontal;
self.maskDisabled = self.maskDisabled;

[self.collectionView removeFromSuperview];
self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds
collectionViewLayout:[self collectionViewLayout]];
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.showsVerticalScrollIndicator = NO;
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.decelerationRate = UIScrollViewDecelerationRateFast;
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Expand Down Expand Up @@ -147,17 +149,37 @@ - (void)setMaskDisabled:(BOOL)maskDisabled
(id)[[UIColor clearColor] CGColor],];
maskLayer.locations = @[@0.0, @0.33, @0.66, @1.0];
maskLayer.startPoint = CGPointMake(0.0, 0.0);
maskLayer.endPoint = CGPointMake(1.0, 0.0);
maskLayer.endPoint = (self.pickerViewOrientation == AKPickerViewOrientationVertical) ? CGPointMake(0.0, 1.0) : CGPointMake(1.0, 0.0);
maskLayer;
});
}

- (void)setPickerViewOrientation:(AKPickerViewOrientation)pickerViewOrientation
{
_pickerViewOrientation = pickerViewOrientation;

self.collectionView.collectionViewLayout = [self collectionViewLayout];
if (!self.maskDisabled && [self.collectionView.layer.mask isKindOfClass:[CAGradientLayer class]]) {
CAGradientLayer *maskLayer = (CAGradientLayer *)self.collectionView.layer.mask;
maskLayer.endPoint = (self.pickerViewOrientation == AKPickerViewOrientationVertical) ? CGPointMake(0.0, 1.0) : CGPointMake(1.0, 0.0);
}
}

#pragma mark -

- (AKCollectionViewLayout *)collectionViewLayout
{
AKCollectionViewLayout *layout = [AKCollectionViewLayout new];
layout.delegate = self;
switch (self.pickerViewOrientation) {
case AKPickerViewOrientationVertical:
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
break;
case AKPickerViewOrientationHorizontal:
default:
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
break;
}
return layout;
}

Expand Down Expand Up @@ -197,14 +219,22 @@ - (CGFloat)offsetForItem:(NSUInteger)item
for (NSInteger i = 0; i < item; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
CGSize cellSize = [self collectionView:self.collectionView layout:self.collectionView.collectionViewLayout sizeForItemAtIndexPath:indexPath];
offset += cellSize.width;
if (self.pickerViewOrientation == AKPickerViewOrientationVertical) {
offset += cellSize.height;
} else {
offset += cellSize.width;
}
}

NSIndexPath *firstIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
CGSize firstSize = [self collectionView:self.collectionView layout:self.collectionView.collectionViewLayout sizeForItemAtIndexPath:firstIndexPath];
NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForItem:item inSection:0];
CGSize selectedSize = [self collectionView:self.collectionView layout:self.collectionView.collectionViewLayout sizeForItemAtIndexPath:selectedIndexPath];
offset -= (firstSize.width - selectedSize.width) / 2;
if (self.pickerViewOrientation == AKPickerViewOrientationVertical) {
offset -= (firstSize.height - selectedSize.height) / 2;
} else {
offset -= (firstSize.width - selectedSize.width) / 2;
}

return offset;
}
Expand All @@ -213,14 +243,25 @@ - (void)scrollToItem:(NSUInteger)item animated:(BOOL)animated
{
switch (self.pickerViewStyle) {
case AKPickerViewStyleFlat: {
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:item inSection:0]
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:animated];
if (self.pickerViewOrientation == AKPickerViewOrientationVertical) {
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:item inSection:0]
atScrollPosition:UICollectionViewScrollPositionCenteredVertically
animated:animated];
} else {
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:item inSection:0]
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:animated];
}
break;
}
case AKPickerViewStyle3D: {
[self.collectionView setContentOffset:CGPointMake([self offsetForItem:item], self.collectionView.contentOffset.y)
animated:animated];
if (self.pickerViewOrientation == AKPickerViewOrientationVertical) {
[self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x, [self offsetForItem:item])
animated:animated];
} else {
[self.collectionView setContentOffset:CGPointMake([self offsetForItem:item], self.collectionView.contentOffset.y)
animated:animated];
}
break;
}
default: break;
Expand Down Expand Up @@ -260,9 +301,16 @@ - (void)didEndScrolling
for (NSUInteger i = 0; i < [self collectionView:self.collectionView numberOfItemsInSection:0]; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
AKCollectionViewCell *cell = (AKCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
if ([self offsetForItem:i] + cell.bounds.size.width / 2 > self.collectionView.contentOffset.x) {
[self selectItem:i animated:YES];
break;
if (self.pickerViewOrientation == AKPickerViewOrientationVertical) {
if ([self offsetForItem:i] + cell.bounds.size.height / 2 > self.collectionView.contentOffset.y) {
[self selectItem:i animated:YES];
break;
}
} else {
if ([self offsetForItem:i] + cell.bounds.size.width / 2 > self.collectionView.contentOffset.x) {
[self selectItem:i animated:YES];
break;
}
}
}
}
Expand Down Expand Up @@ -315,17 +363,29 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize size = CGSizeMake(self.interitemSpacing, collectionView.bounds.size.height);
CGSize size = (self.pickerViewOrientation == AKPickerViewOrientationVertical) ? CGSizeMake(collectionView.bounds.size.width, self.interitemSpacing) : CGSizeMake(self.interitemSpacing, collectionView.bounds.size.height);
if ([self.dataSource respondsToSelector:@selector(pickerView:titleForItem:)]) {
NSString *title = [self.dataSource pickerView:self titleForItem:indexPath.item];
size.width += [self sizeForString:title].width;
if (self.pickerViewOrientation == AKPickerViewOrientationVertical) {
size.height += [self sizeForString:title].height;
} else {
size.width += [self sizeForString:title].width;
}
if ([self.delegate respondsToSelector:@selector(pickerView:marginForItem:)]) {
CGSize margin = [self.delegate pickerView:self marginForItem:indexPath.item];
size.width += margin.width * 2;
if (self.pickerViewOrientation == AKPickerViewOrientationVertical) {
size.height += margin.height * 2;
} else {
size.width += margin.width * 2;
}
}
} else if ([self.dataSource respondsToSelector:@selector(pickerView:imageForItem:)]) {
UIImage *image = [self.dataSource pickerView:self imageForItem:indexPath.item];
size.width += image.size.width;
if (self.pickerViewOrientation == AKPickerViewOrientationVertical) {
size.height += image.size.height;
} else {
size.width += image.size.width;
}
}
return size;
}
Expand All @@ -347,8 +407,13 @@ - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UIColl
CGSize firstSize = [self collectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:firstIndexPath];
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:number - 1 inSection:section];
CGSize lastSize = [self collectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:lastIndexPath];
return UIEdgeInsetsMake(0, (collectionView.bounds.size.width - firstSize.width) / 2,
0, (collectionView.bounds.size.width - lastSize.width) / 2);
if (self.pickerViewOrientation == AKPickerViewOrientationVertical) {
return UIEdgeInsetsMake((collectionView.bounds.size.height - firstSize.height) / 2, 0,
(collectionView.bounds.size.height - lastSize.height) / 2, 0);
} else {
return UIEdgeInsetsMake(0, (collectionView.bounds.size.width - firstSize.width) / 2,
0, (collectionView.bounds.size.width - lastSize.width) / 2);
}
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Expand Down Expand Up @@ -455,7 +520,9 @@ - (void)setSelected:(BOOL)selected

@interface AKCollectionViewLayout ()
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGFloat midX;
@property (nonatomic, assign) CGFloat midY;
@property (nonatomic, assign) CGFloat maxAngle;
@end

Expand All @@ -466,7 +533,6 @@ - (id)init
self = [super init];
if (self) {
self.sectionInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.minimumLineSpacing = 0.0;
}
return self;
Expand All @@ -476,7 +542,9 @@ - (void)prepareLayout
{
CGRect visibleRect = (CGRect){self.collectionView.contentOffset, self.collectionView.bounds.size};
self.midX = CGRectGetMidX(visibleRect);
self.midY = CGRectGetMidY(visibleRect);
self.width = CGRectGetWidth(visibleRect) / 2;
self.height = CGRectGetHeight(visibleRect) / 2;
self.maxAngle = M_PI_2;
}

Expand All @@ -493,14 +561,25 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSInde
return attributes; break;
}
case AKPickerViewStyle3D: {
CGFloat distance = CGRectGetMidX(attributes.frame) - self.midX;
CGFloat currentAngle = self.maxAngle * distance / self.width / M_PI_2;
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, -distance, 0, -self.width);
transform = CATransform3DRotate(transform, currentAngle, 0, 1, 0);
transform = CATransform3DTranslate(transform, 0, 0, self.width);
attributes.transform3D = transform;
attributes.alpha = (ABS(currentAngle) < self.maxAngle);
if (self.scrollDirection == UICollectionViewScrollDirectionVertical) {
CGFloat distance = CGRectGetMidY(attributes.frame) - self.midY;
CGFloat currentAngle = self.maxAngle * distance / self.height / M_PI_2;
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, 0, distance, -self.height);
transform = CATransform3DRotate(transform, currentAngle, 1, 0, 0);
transform = CATransform3DTranslate(transform, 0, 0, self.height);
attributes.transform3D = transform;
attributes.alpha = (ABS(currentAngle) < self.maxAngle);
} else {
CGFloat distance = CGRectGetMidX(attributes.frame) - self.midX;
CGFloat currentAngle = self.maxAngle * distance / self.width / M_PI_2;
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, -distance, 0, -self.width);
transform = CATransform3DRotate(transform, currentAngle, 0, 1, 0);
transform = CATransform3DTranslate(transform, 0, 0, self.width);
attributes.transform3D = transform;
attributes.alpha = (ABS(currentAngle) < self.maxAngle);
}
return attributes; break;
}
default: return nil; break;
Expand Down