Skip to content
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

Make it more like pinterest #20

Open
wants to merge 5 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
71 changes: 53 additions & 18 deletions GHContextMenu/GHCollectionViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ - (void)viewDidLoad
[super viewDidLoad];

GHContextMenuView* overlay = [[GHContextMenuView alloc] init];
overlay.menuViewBackgroundColor = [UIColor whiteColor];
overlay.dataSource = self;
overlay.delegate = self;
overlay.menuViewBackgroundColor = [UIColor colorWithWhite:1.0f alpha:0.85f];

// Do any additional setup after loading the view.
// [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
Expand Down Expand Up @@ -89,21 +91,32 @@ -(UIImage*) imageForItemAtIndex:(NSInteger)index
NSString* imageName = nil;
switch (index) {
case 0:
imageName = @"facebook-white";
imageName = @"gp";
break;
case 1:
imageName = @"twitter-white";
imageName = @"p";
break;
case 2:
imageName = @"google-plus-white";
imageName = @"t";
break;
case 3:
imageName = @"linkedin-white";
default:
break;
}
return [UIImage imageNamed:imageName];
}

- (UIImage *)highlightImageForItemAtIndex:(NSInteger)index {
NSString* imageName = nil;
switch (index) {
case 0:
imageName = @"gps";
break;
case 1:
imageName = @"ps";
break;
case 4:
imageName = @"pinterest-white";
case 2:
imageName = @"ts";
break;

default:
break;
}
Expand All @@ -117,21 +130,14 @@ - (void) didSelectItemAtIndex:(NSInteger)selectedIndex forMenuAtPoint:(CGPoint)p
NSString* msg = nil;
switch (selectedIndex) {
case 0:
msg = @"Facebook Selected";
msg = @"Google+ Selected";
break;
case 1:
msg = @"Twitter Selected";
msg = @"Pinterest Selected";
break;
case 2:
msg = @"Google Plus Selected";
break;
case 3:
msg = @"Linkedin Selected";
break;
case 4:
msg = @"Pinterest Selected";
msg = @"Twitter Selected";
break;

default:
break;
}
Expand All @@ -143,5 +149,34 @@ - (void) didSelectItemAtIndex:(NSInteger)selectedIndex forMenuAtPoint:(CGPoint)p

}

- (NSString *)tipForItemAtIndex:(NSInteger)index {
NSString* tip = @"";
switch (index) {
case 0:
tip = @"Google+";
break;
case 1:
tip = @"Pinterest";
break;
case 2:
tip = @"Twitter";
break;
default:
break;
}
return tip;
}

- (UIView *)overlayViewAtPoint:(CGPoint)point {
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
CGPoint origin = cell.frame.origin;
origin = [self.view convertPoint:origin fromView:self.collectionView];
UIView *snap = [cell snapshotViewAfterScreenUpdates:NO];
CGRect snapFrame = cell.bounds;
snapFrame.origin = origin;
snap.frame = snapFrame;
return snap;
}

@end
7 changes: 7 additions & 0 deletions GHContextMenu/GHContextMenuView.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ typedef NS_ENUM(NSInteger, GHContextMenuActionType){

- (void) longPressDetected:(UIGestureRecognizer*) gestureRecognizer;

@property (nonatomic, strong) UIFont *tipFont;
@property (nonatomic, strong) UIColor *tipColor;
@property (nonatomic, strong) UIColor *menuViewBackgroundColor;

@end

@protocol GHContextOverlayViewDataSource <NSObject>
Expand All @@ -37,6 +41,9 @@ typedef NS_ENUM(NSInteger, GHContextMenuActionType){

@optional
-(BOOL) shouldShowMenuAtPoint:(CGPoint) point;
- (UIImage*) highlightImageForItemAtIndex:(NSInteger) index;
- (NSString*) tipForItemAtIndex:(NSInteger) index;
- (UIView *) overlayViewAtPoint:(CGPoint) point;

@end

Expand Down
144 changes: 121 additions & 23 deletions GHContextMenu/GHContextMenuView.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ @interface GHContextMenuView ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong) NSMutableArray* itemLocations;
@property (nonatomic) NSInteger prevIndex;

@property (nonatomic) CGColorRef itemBGHighlightedColor;
@property (nonatomic) CGColorRef itemBGColor;
@property (nonatomic, copy) NSArray *normalImages;
@property (nonatomic, copy) NSArray *highlightImages;
@property (nonatomic, copy) NSArray *tips;

@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic) CGRect tipLabelFrame;
@property (nonatomic, strong) UIView *overlayView;

@end

Expand Down Expand Up @@ -82,9 +87,18 @@ - (id)init
_arcAngle = M_PI_2;
_radius = 90;

self.itemBGColor = [UIColor grayColor].CGColor;
self.itemBGHighlightedColor = [UIColor redColor].CGColor;

self.tipLabel = [[UILabel alloc] init];
if (self.tipFont) {
self.tipLabel.font = self.tipFont;
} else {
self.tipLabel.font = [UIFont boldSystemFontOfSize:36.0f];
}
if (self.tipColor) {
self.tipLabel.textColor = self.tipColor;
} else {
self.tipLabel.textColor = [UIColor blackColor];
}
[self addSubview:self.tipLabel];
}
return self;
}
Expand Down Expand Up @@ -157,8 +171,16 @@ - (void) longPressDetected:(UIGestureRecognizer*) gestureRecognizer

[[UIApplication sharedApplication].keyWindow addSubview:self];
self.longPressLocation = [gestureRecognizer locationInView:self];
if ([self.dataSource respondsToSelector:@selector(overlayViewAtPoint:)]) {
self.overlayView = [self.dataSource overlayViewAtPoint:pointInView];
[self insertSubview:self.overlayView atIndex:0];
}

self.layer.backgroundColor = [UIColor colorWithWhite:0.1f alpha:.8f].CGColor;
UIColor *backgroundColor = [UIColor colorWithWhite:0.1f alpha:.8f];
if (self.menuViewBackgroundColor) {
backgroundColor = self.menuViewBackgroundColor;
}
self.layer.backgroundColor = self.menuViewBackgroundColor.CGColor;
self.isShowing = YES;
[self animateMenu:YES];
[self setNeedsDisplay];
Expand Down Expand Up @@ -192,27 +214,18 @@ - (void) hideMenu
[self animateMenu:NO];
[self setNeedsDisplay];
[self removeFromSuperview];
[self.overlayView removeFromSuperview];
}
}

- (CALayer*) layerWithImage:(UIImage*) image
{
CALayer *layer = [CALayer layer];
layer.bounds = CGRectMake(0, 0, GHMenuItemSize, GHMenuItemSize);
layer.cornerRadius = GHMenuItemSize/2;
layer.borderColor = [UIColor whiteColor].CGColor;
layer.borderWidth = GHBorderWidth;
layer.shadowColor = [UIColor blackColor].CGColor;
layer.shadowOffset = CGSizeMake(0, -1);
layer.backgroundColor = self.itemBGColor;

CALayer* imageLayer = [CALayer layer];
imageLayer.contents = (id) image.CGImage;
imageLayer.bounds = CGRectMake(0, 0, GHMenuItemSize*2/3, GHMenuItemSize*2/3);
imageLayer.position = CGPointMake(GHMenuItemSize/2, GHMenuItemSize/2);
[layer addSublayer:imageLayer];

return layer;
return imageLayer;
}

- (void) setDataSource:(id<GHContextOverlayViewDataSource>)dataSource
Expand All @@ -228,15 +241,36 @@ - (void) reloadData
{
[self.menuItems removeAllObjects];
[self.itemLocations removeAllObjects];
NSMutableArray *normalImages = @[].mutableCopy;
NSMutableArray *highlightImages = @[].mutableCopy;
NSMutableArray *tips = @[].mutableCopy;

if (self.dataSource != nil) {
NSInteger count = [self.dataSource numberOfMenuItems];
for (int i = 0; i < count; i++) {
UIImage* image = [self.dataSource imageForItemAtIndex:i];
CALayer *layer = [self layerWithImage:image];
[normalImages addObject:image];
if ([self.dataSource respondsToSelector:@selector(highlightImageForItemAtIndex:)]) {
UIImage *highlightImage = [self.dataSource highlightImageForItemAtIndex:i];
[highlightImages addObject:highlightImage];
} else {
[highlightImages addObject:image];
}
if ([self.dataSource respondsToSelector:@selector(tipForItemAtIndex:)]) {
NSString *tip = [self.dataSource tipForItemAtIndex:i];
[tips addObject:tip];
} else {
[tips addObject:@""];
}
CALayer *imageLayer = [self layerWithImage:image];
CALayer *layer = [CALayer layer];
[layer addSublayer:imageLayer];
[self.layer addSublayer:layer];
[self.menuItems addObject:layer];
}
self.normalImages = normalImages.copy;
self.highlightImages = highlightImages.copy;
self.tips = tips.copy;
}
}

Expand Down Expand Up @@ -267,6 +301,29 @@ - (void) layoutMenuItems
layer.transform = CATransform3DRotate(CATransform3DIdentity, angle, 0, 0, 1);
}
}

[self layoutTipLabel];
}

- (void)layoutTipLabel {
CGFloat dx = self.center.x - self.longPressLocation.x;
CGFloat dy = self.center.y - self.longPressLocation.y;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGPoint lableOrigin = CGPointZero;
if (dx >= 0) {
lableOrigin.x = screenWidth / 2.0f + 25.0f;
} else {
lableOrigin.x = 25.0f;
}
if (dy >= 0) {
lableOrigin.y = self.longPressLocation.y + 150.0f - 44.0f;
} else {
lableOrigin.y = self.longPressLocation.y - 150.0f;
}

CGRect labelFrame = CGRectMake(lableOrigin.x, lableOrigin.y, 300.0f, 44.0f);
self.tipLabelFrame = labelFrame;
self.tipLabel.frame = labelFrame;
}

- (GHMenuItemLocation*) locationForItemAtIndex:(NSUInteger) index
Expand Down Expand Up @@ -322,7 +379,7 @@ - (CGFloat) angleBeweenStartinPoint:(CGPoint) startingPoint endingPoint:(CGPoint
float bearingRadians = atan2f(originPoint.y, originPoint.x);

bearingRadians = (bearingRadians > 0.0 ? bearingRadians : (M_PI*2 + bearingRadians));

return bearingRadians;
}

Expand Down Expand Up @@ -351,19 +408,37 @@ - (void) highlightMenuItemForPoint
}
}

CGFloat tipMove = self.curretnLocation.y - self.longPressLocation.y;
if (tipMove >= self.radius) {
tipMove = self.radius;
}
if (tipMove <= -self.radius) {
tipMove = -self.radius;
}
CGRect tipLabelFrame = self.tipLabelFrame;
tipLabelFrame.origin.y += tipMove / 7.0f;
self.tipLabel.frame = tipLabelFrame;

if (closeToIndex >= 0 && closeToIndex < self.menuItems.count) {

GHMenuItemLocation* itemLocation = [self.itemLocations objectAtIndex:closeToIndex];

CGFloat distanceFromCenter = sqrt(pow(self.curretnLocation.x - self.longPressLocation.x, 2)+ pow(self.curretnLocation.y-self.longPressLocation.y, 2));

CGFloat toleranceDistance = (self.radius - GHMainItemSize/(2*sqrt(2)) - GHMenuItemSize/(2*sqrt(2)) )/2;

CGFloat distanceFromItem = fabsf(distanceFromCenter - self.radius) - GHMenuItemSize/(2*sqrt(2)) ;

if (fabs(distanceFromItem) < toleranceDistance ) {
[self showTips];
CALayer *layer = [self.menuItems objectAtIndex:closeToIndex];
layer.backgroundColor = self.itemBGHighlightedColor;
for (CALayer *sublayer in layer.sublayers) {
[sublayer removeFromSuperlayer];
}
CALayer *imageLayer = [self layerWithImage:self.highlightImages[closeToIndex]];
[layer addSublayer:imageLayer];

self.tipLabel.text = self.tips[closeToIndex];

CGFloat distanceFromItemBorder = fabs(distanceFromItem);

Expand All @@ -389,21 +464,45 @@ - (void) highlightMenuItemForPoint
self.prevIndex = closeToIndex;

} else if(self.prevIndex >= 0) {
[self hideTips];
[self resetPreviousSelection];
}
}else {
[self hideTips];
[self resetPreviousSelection];
}
}
}

- (void)showTips {
[self.tipLabel.layer removeAllAnimations];
[UIView animateWithDuration:0.1f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.tipLabel.alpha = 1.0f;
} completion:^(BOOL finished) {

}];
}

- (void)hideTips {
[self.tipLabel.layer removeAllAnimations];
[UIView animateWithDuration:0.1f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.tipLabel.alpha = 0.0f;
} completion:^(BOOL finished) {

}];
}

- (void) resetPreviousSelection
{
if (self.prevIndex >= 0) {
CALayer *layer = self.menuItems[self.prevIndex];
for (CALayer *sublayer in layer.sublayers) {
[sublayer removeFromSuperlayer];
}
CALayer *imageLayer = [self layerWithImage:self.normalImages[self.prevIndex]];
[layer addSublayer:imageLayer];
GHMenuItemLocation* itemLocation = [self.itemLocations objectAtIndex:self.prevIndex];
layer.position = itemLocation.position;
layer.backgroundColor = self.itemBGColor;
layer.transform = CATransform3DIdentity;
self.prevIndex = -1;
}
Expand Down Expand Up @@ -460,7 +559,6 @@ - (void)animationDidStart:(CAAnimation *)anim
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
layer.position = toPosition;
layer.backgroundColor = self.itemBGColor;
layer.opacity = 0.0f;
layer.transform = CATransform3DIdentity;
[CATransaction commit];
Expand Down
Loading