Skip to content

Commit

Permalink
Added configurable button size and border width, as well as custom si…
Browse files Browse the repository at this point in the history
…ze images.
  • Loading branch information
JaNd3r committed Sep 9, 2014
1 parent c97565d commit 3144639
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
4 changes: 3 additions & 1 deletion CKCircleMenuView/CKCircleMenuView.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ extern NSString* const CIRCLE_MENU_RADIUS;
extern NSString* const CIRCLE_MENU_MAX_ANGLE;
extern NSString* const CIRCLE_MENU_DIRECTION;
extern NSString* const CIRCLE_MENU_DEPTH;
extern NSString* const CIRCLE_MENU_BUTTON_RADIUS;
extern NSString* const CIRCLE_MENU_BUTTON_BORDER_WIDTH;

typedef enum {
CircleMenuDirectionUp = 1,
Expand Down Expand Up @@ -78,7 +80,7 @@ typedef enum {
* select a button and to close the menu, once the
* gesture ends.
*/
- (void)openMenuWithRecognizer:(UILongPressGestureRecognizer*)aRecognizer;
- (void)openMenuWithRecognizer:(UIGestureRecognizer*)aRecognizer;

/**
* Offers the possibility to close the menu externally.
Expand Down
53 changes: 31 additions & 22 deletions CKCircleMenuView/CKCircleMenuView.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@interface CKCircleMenuView()

@property (nonatomic) NSMutableArray* buttons;
@property (weak, nonatomic) UILongPressGestureRecognizer* recognizer;
@property (weak, nonatomic) UIGestureRecognizer* recognizer;
@property (nonatomic) int hoverTag;

@property (nonatomic) UIColor* innerViewColor;
Expand All @@ -21,6 +21,8 @@ @interface CKCircleMenuView()
@property (nonatomic) CGFloat animationDelay;
@property (nonatomic) CGFloat startingAngle;
@property (nonatomic) BOOL depth;
@property (nonatomic) CGFloat buttonRadius;
@property (nonatomic) CGFloat buttonBorderWidth;

@property (nonatomic, weak) UIView* clippingView;

Expand All @@ -41,6 +43,8 @@ @interface CKCircleMenuView()
NSString* const CIRCLE_MENU_MAX_ANGLE = @"kCircleMenuMaxAngle";
NSString* const CIRCLE_MENU_DIRECTION = @"kCircleMenuDirection";
NSString* const CIRCLE_MENU_DEPTH = @"kCircleMenuDepth";
NSString* const CIRCLE_MENU_BUTTON_RADIUS = @"kCircleMenuButtonRadius";
NSString* const CIRCLE_MENU_BUTTON_BORDER_WIDTH = @"kCircleMenuButtonBorderWidth";

@implementation CKCircleMenuView

Expand Down Expand Up @@ -71,6 +75,8 @@ - (id)initWithOptions:(NSDictionary*)anOptionsDictionary
break;
}
self.depth = [[anOptionsDictionary valueForKey:CIRCLE_MENU_DEPTH] boolValue];
self.buttonRadius = [[anOptionsDictionary valueForKey:CIRCLE_MENU_BUTTON_RADIUS] doubleValue];
self.buttonBorderWidth = [[anOptionsDictionary valueForKey:CIRCLE_MENU_BUTTON_BORDER_WIDTH] doubleValue];
} else {
// using some default settings
self.innerViewColor = [UIColor colorWithRed:0.0 green:0.25 blue:0.5 alpha:1.0];
Expand All @@ -81,6 +87,8 @@ - (id)initWithOptions:(NSDictionary*)anOptionsDictionary
self.maxAngle = 180.0;
self.startingAngle = 0.0;
self.depth = NO;
self.buttonRadius = 39.0;
self.buttonBorderWidth = 2.0;
}
}
return self;
Expand All @@ -90,7 +98,7 @@ - (id)initAtOrigin:(CGPoint)aPoint usingOptions:(NSDictionary *)anOptionsDiction
{
self = [self initWithOptions:anOptionsDictionary];
if (self) {
self.frame = CGRectMake(aPoint.x - self.radius - 39.0, aPoint.y - self.radius - 39.0, self.radius * 2 + 78.0, self.radius * 2 + 78.0);
self.frame = CGRectMake(aPoint.x - self.radius - self.buttonRadius, aPoint.y - self.radius - self.buttonRadius, self.radius * 2 + self.buttonRadius * 2, self.radius * 2 + self.buttonRadius * 2);
int tTag = 1;
for (UIImage* img in anImageArray) {
UIView* tView = [self createButtonViewWithImage:img andTag:tTag];
Expand All @@ -105,7 +113,7 @@ - (id)initAtOrigin:(CGPoint)aPoint usingOptions:(NSDictionary *)anOptionsDiction
{
self = [self initWithOptions:anOptionsDictionary];
if (self) {
self.frame = CGRectMake(aPoint.x - self.radius - 39.0, aPoint.y - self.radius - 39.0, self.radius * 2 + 78.0, self.radius * 2 + 78.0);
self.frame = CGRectMake(aPoint.x - self.radius - self.buttonRadius, aPoint.y - self.radius - self.buttonRadius, self.radius * 2 + self.buttonRadius * 2, self.radius * 2 + self.buttonRadius * 2);
int tTag = 1;
va_list args;
va_start(args, anImage);
Expand All @@ -129,17 +137,19 @@ - (id)initAtOrigin:(CGPoint)aPoint usingOptions:(NSDictionary *)anOptionsDiction
- (UIView*)createButtonViewWithImage:(UIImage*)anImage andTag:(int)aTag
{
UIButton* tButton = [UIButton buttonWithType:UIButtonTypeCustom];
tButton.frame = CGRectMake(23.0, 23.0, 32.0, 32.0);
CGFloat tButtonViewX = self.buttonRadius - anImage.size.width / 2;
CGFloat tButtonViewY = self.buttonRadius - anImage.size.height / 2;
tButton.frame = CGRectMake(tButtonViewX, tButtonViewY, anImage.size.width, anImage.size.height);
[tButton setImage:anImage forState:UIControlStateNormal];
tButton.tag = aTag + TAG_BUTTON_OFFSET;

UIView* tInnerView = [[CKRoundView alloc] initWithFrame:CGRectMake(0.0, 0.0, 78.0, 78.0)];
UIView* tInnerView = [[CKRoundView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.buttonRadius * 2, self.buttonRadius * 2)];
tInnerView.backgroundColor = self.innerViewColor;
tInnerView.opaque = YES;
tInnerView.clipsToBounds = NO;
tInnerView.layer.cornerRadius = 39.0;
tInnerView.layer.cornerRadius = self.buttonRadius;
tInnerView.layer.borderColor = [self.borderViewColor CGColor];
tInnerView.layer.borderWidth = 2.0;
tInnerView.layer.borderWidth = self.buttonBorderWidth;

if (self.depth) {
[self applyInactiveDepthToButtonView:tInnerView];
Expand All @@ -160,15 +170,15 @@ - (void)calculateButtonPositions
// climb view hierarchy up, until first view with clipToBounds = YES
self.clippingView = [self clippingViewOfChild:self];
}
CGFloat tMaxX = self.frame.size.width - 39.0;
CGFloat tMinX = 39.0;
CGFloat tMaxY = self.frame.size.height - 39.0;
CGFloat tMinY = 39.0;
CGFloat tMaxX = self.frame.size.width - self.buttonRadius;
CGFloat tMinX = self.buttonRadius;
CGFloat tMaxY = self.frame.size.height - self.buttonRadius;
CGFloat tMinY = self.buttonRadius;
if (self.clippingView) {
CGRect tClippingFrame = [self.clippingView convertRect:self.clippingView.bounds toView:self];
tMaxX = tClippingFrame.size.width + tClippingFrame.origin.x - 78.0;
tMaxX = tClippingFrame.size.width + tClippingFrame.origin.x - self.buttonRadius * 2;
tMinX = tClippingFrame.origin.x;
tMaxY = tClippingFrame.size.height + tClippingFrame.origin.y - 78.0;
tMaxY = tClippingFrame.size.height + tClippingFrame.origin.y - self.buttonRadius * 2;
tMinY = tClippingFrame.origin.y;
}

Expand Down Expand Up @@ -219,7 +229,7 @@ - (UIView*)clippingViewOfChild:(UIView*)aView
}
}

- (void)openMenuWithRecognizer:(UILongPressGestureRecognizer*)aRecognizer
- (void)openMenuWithRecognizer:(UIGestureRecognizer*)aRecognizer
{
self.recognizer = aRecognizer;
// use target action to get notified upon gesture changes
Expand All @@ -230,8 +240,8 @@ - (void)openMenuWithRecognizer:(UILongPressGestureRecognizer*)aRecognizer
for (UIView* tButtonView in self.buttons) {
[self addSubview:tButtonView];
tButtonView.alpha = 0.0;
CGFloat tDiffX = tOrigin.x - tButtonView.frame.origin.x - 39.0;
CGFloat tDiffY = tOrigin.y - tButtonView.frame.origin.y - 39.0;
CGFloat tDiffX = tOrigin.x - tButtonView.frame.origin.x - self.buttonRadius;
CGFloat tDiffY = tOrigin.y - tButtonView.frame.origin.y - self.buttonRadius;
tButtonView.transform = CGAffineTransformMakeTranslation(tDiffX, tDiffY);
}

Expand Down Expand Up @@ -376,12 +386,11 @@ @implementation CKRoundView
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
// Pythagoras a^2 + b^2 = c^2
CGFloat tDiffX = 39.0 - point.x;
CGFloat tDiffY = 39.0 - point.y;
CGFloat tDeltaX = tDiffX * tDiffX;
CGFloat tDeltaY = tDiffY * tDiffY;
CGFloat tDistanceSquared = tDeltaX + tDeltaY;
CGFloat tRadiusSquared = 39.0 * 39.0;
CGFloat tRadius = self.bounds.size.width / 2;
CGFloat tDiffX = tRadius - point.x;
CGFloat tDiffY = tRadius - point.y;
CGFloat tDistanceSquared = tDiffX * tDiffX + tDiffY * tDiffY;
CGFloat tRadiusSquared = tRadius * tRadius;
return tDistanceSquared < tRadiusSquared;
}

Expand Down

0 comments on commit 3144639

Please sign in to comment.