Skip to content

add peekView #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions BDKCollectionIndexView.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ typedef NS_ENUM(NSInteger, BDKCollectionIndexViewDirection) {
*/
@property (strong, nonatomic) NSArray *indexTitles;

@property (strong, nonatomic) UIColor *colorPeekView;

/**
Indicates the position of the last-selected index title. Should map directly to a table view / collection section.
*/
Expand Down Expand Up @@ -88,4 +90,6 @@ typedef NS_ENUM(NSInteger, BDKCollectionIndexViewDirection) {
- (void)collectionIndexView:(BDKCollectionIndexView *)collectionIndexView isPressedOnIndex:(NSUInteger)pressedIndex indexTitle:(NSString *)indexTitle;
- (void)collectionIndexView:(BDKCollectionIndexView *)collectionIndexView liftedFingerFromIndex:(NSUInteger)pressedIndex;

- (NSAttributedString *)collectionIndexView:(BDKCollectionIndexView *)collectionIndexView index:(NSUInteger )index;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name for this delegate function doesn't tell me anything about what it does. The other two indicate actions a user takes against the index view. What does this one communicate? Can you rename it to something more descriptive, please?


@end
47 changes: 47 additions & 0 deletions BDKCollectionIndexView.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ - (instancetype)initWithFrame:(CGRect)frame indexTitles:(NSArray *)indexTitles {

-(void)setupWithIndexTitles:(NSArray *)indexTitles {

_colorPeekView = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];
_currentIndex = 0;
_touchStatusViewAlpha = 0.25;
_touchStatusBackgroundColor = [UIColor blackColor];
Expand Down Expand Up @@ -314,6 +315,44 @@ - (void)setNewIndexForPoint:(CGPoint)point {
break;
}

if ([self.delegate respondsToSelector:@selector(collectionIndexView:index:)]) {
NSAttributedString * attr = [self.delegate collectionIndexView:self index:self.currentIndex];
if (attr != NULL) {
UIView * peekView = [self viewWithTag:9999];
UILabel * titleLabel = [peekView viewWithTag:9998];
Comment on lines +321 to +322
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apple has advocated against using -viewWithTag: due to its inability to handle tag index collisions, especially when using third-party code, and this could potentially introduce collisions with other third-party code when added to somebody else's project.

Can you update this to use something other than viewWithTag? A retained reference perhaps?

if(peekView == NULL) {
peekView = [UIView new];
peekView.layer.cornerRadius = 25;
peekView.layer.backgroundColor = self.colorPeekView.CGColor;
peekView.tag = 9999;
peekView.alpha = 0;
[self addSubview:peekView];

titleLabel = [UILabel new];
titleLabel.tag = 9998;
[peekView addSubview:titleLabel];
[UIView animateWithDuration:0.3 animations:^{
peekView.alpha = 1;
}];
}

CGFloat maxOffset = self.frame.size.height - 50;
CGFloat minOffset = 0;
CGFloat peekOffset = point.y;
if (peekOffset > maxOffset){
peekOffset = maxOffset;
}
else if (peekOffset < minOffset) {
peekOffset = minOffset;
}

peekView.frame = CGRectMake( (self.frame.size.width + 25) * -1, peekOffset, 50, 50);
Comment on lines +339 to +349
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change needed here, but I just want to call this out: I would prefer the offset behavior and size of this peek view be a bit more customizable, but I think this is good for now. I may update this code to add customization options in the future.

titleLabel.frame = peekView.bounds;
titleLabel.attributedText = attr;
titleLabel.textAlignment = NSTextAlignmentCenter;
}
}

if (newIndex == -1) {
UILabel *topLabel = self.indexLabels[0];
UILabel *bottomLabel = self.indexLabels[self.indexLabels.count - 1];
Expand Down Expand Up @@ -344,6 +383,14 @@ - (void)handleGesture:(UIGestureRecognizer *)recognizer {

if (recognizer != _longPresser) { return; }
if (recognizer.state == UIGestureRecognizerStateEnded) {

UIView *peekView = [self viewWithTag:9999];
[UIView animateWithDuration:0.3 animations:^{
peekView.alpha = 0;
} completion:^(BOOL finished) {
[peekView removeFromSuperview];
}];

if ([self.delegate respondsToSelector:@selector(collectionIndexView:liftedFingerFromIndex:)]) {
[self.delegate collectionIndexView:self liftedFingerFromIndex:self.currentIndex];
}
Expand Down
4 changes: 2 additions & 2 deletions Project/BDKCollectionIndexView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = Prefix.pch;
INFOPLIST_FILE = "$(SRCROOT)/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
PRODUCT_NAME = "$(TARGET_NAME)";
TARGETED_DEVICE_FAMILY = "1,2";
WRAPPER_EXTENSION = app;
Expand All @@ -311,7 +311,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = Prefix.pch;
INFOPLIST_FILE = "$(SRCROOT)/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
PRODUCT_NAME = "$(TARGET_NAME)";
TARGETED_DEVICE_FAMILY = "1,2";
WRAPPER_EXTENSION = app;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Comment on lines +1 to +8
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this file from your changes?

13 changes: 12 additions & 1 deletion Project/BDKViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#import "BDKCell.h"

@interface BDKViewController () <UICollectionViewDataSource>
@interface BDKViewController () <UICollectionViewDataSource,BDKCollectionIndexViewDelegate>

@property (strong, nonatomic) UICollectionView *collectionView;

Expand Down Expand Up @@ -49,6 +49,7 @@ - (void)viewDidLoad {

self.sections = sections.copy;
self.indexView.indexTitles = self.sections;
self.indexView.delegate = self;
}

- (void)viewWillLayoutSubviews {
Expand Down Expand Up @@ -129,4 +130,14 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
return cell;
}


-(NSAttributedString*)collectionIndexView:(BDKCollectionIndexView *)collectionIndexView index:(NSUInteger)index {

NSAttributedString * attr = [[NSAttributedString alloc]initWithString: self.sections[index]];

return attr;
}

@end