Skip to content
This repository was archived by the owner on Oct 17, 2024. It is now read-only.
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
5 changes: 5 additions & 0 deletions DDExpandableButton/DDExpandableButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
CGFloat maxWidth;
NSArray *labels;
DDView *leftTitleView;
UIView *backgroundView;
}

// Current button status (if expanded or shrunk).
Expand Down Expand Up @@ -80,6 +81,9 @@
// Access UIView used to draw labels.
@property (nonatomic,readonly) NSArray *labels;

// Access the UIView that is the background which is transparent/white by default. Customize this view to customize the appearance.
@property (nonatomic,retain) UIView *backgroundView;

- (id)initWithPoint:(CGPoint)point leftTitle:(id)leftTitle buttons:(NSArray *)buttons;

- (void)setSelectedItem:(NSUInteger)selected animated:(BOOL)animated;
Expand All @@ -89,6 +93,7 @@

- (void)disableTimeout;
- (void)updateDisplay;
- (void)setExpandedFrame;

@end

Expand Down
126 changes: 89 additions & 37 deletions DDExpandableButton/DDExpandableButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ @implementation DDExpandableButton
@synthesize borderWidth;
@synthesize innerBorderWidth;
@synthesize labels;
@synthesize backgroundView;


#pragma mark Default Values
Expand Down Expand Up @@ -140,6 +141,10 @@ - (id)initWithFrame:(CGRect)frame
verticalPadding = DEFAULT_VERT_PADDING;
timeout = DEFAULT_TIMEOUT;

backgroundView = [[UIView alloc] init];
[backgroundView setUserInteractionEnabled:NO];
[self addSubview:backgroundView];

[self addTarget:self action:@selector(chooseLabel:forEvent:) forControlEvents:UIControlEventTouchUpInside];

self.borderColor = [UIColor colorWithWhite:DEFAULT_BORDER_WHITE alpha:DEFAULT_BORDER_ALPHA];
Expand Down Expand Up @@ -222,6 +227,15 @@ - (void)setButtons:(NSArray *)buttons
[_labels addObject:v];
}
labels = [_labels ah_retain];

[self setExpandedFrame];
}

- (void) setToggleMode:(BOOL)_toggleMode
{
toggleMode = _toggleMode;

[self setExpandedFrame];
}

- (void)updateDisplay
Expand Down Expand Up @@ -251,50 +265,38 @@ - (void)updateDisplay
self.layer.cornerRadius = roundf(maxHeight / 2.0f);

[self setSelectedItem:0 animated:NO];

[self setExpandedFrame];
}


#pragma mark Frame Rect Methods

- (CGRect)shrunkFrameRect
- (void)setExpandedFrame
{
// Save and reset the transform to perform frame calculations correctly
CGAffineTransform transform = [self transform];
[self setTransform:CGAffineTransformIdentity];

// Set the frame of the control to the maximum expanded size
if (toggleMode)
{
return CGRectMake(self.frame.origin.x, self.frame.origin.y, (cornerAdditionalPadding + horizontalPadding) * 2 + maxWidth, maxHeight);
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, (cornerAdditionalPadding + horizontalPadding) * 2 + maxWidth, maxHeight)];
}
else
{
DDView *currentLabel = [labels objectAtIndex:selectedItem];
return CGRectMake(self.frame.origin.x, self.frame.origin.y, currentLabel.frame.origin.x + currentLabel.frame.size.width + cornerAdditionalPadding, maxHeight);
}
}

- (CGRect)expandedFrameRect
{
if (toggleMode)
{
return [self shrunkFrameRect];
}
else
{
DDView *lastLabel = [labels lastObject];
return CGRectMake(self.frame.origin.x, self.frame.origin.y, lastLabel.frame.origin.x + lastLabel.frame.size.width + cornerAdditionalPadding, maxHeight);
}
}

- (CGRect)currentFrameRect
{
if (expanded)
{
return [self expandedFrameRect];
}
else
{
return [self shrunkFrameRect];
CGFloat x = leftWidth;
CGFloat width = 0.0f;
for (DDView *v in labels)
{
CGRect labelRect = CGRectMake(x, 0, [v defaultFrameSize].width + horizontalPadding * 2, maxHeight);
width = x + labelRect.size.width;
x += labelRect.size.width - v.layer.borderWidth;
}
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, width, maxHeight)];
}

// Restore the transform
[self setTransform:transform];
}


#pragma mark Animation Methods

- (void)setEnabled:(BOOL)enabled
Expand Down Expand Up @@ -408,13 +410,24 @@ - (void)setExpanded:(BOOL)_expanded animated:(BOOL)animated
i++;
}
}


// Set the frame of the background view
CGRect bounds = [self bounds];
CGRect backgroundRect;
if (expanded || toggleMode)
{
backgroundRect = bounds;
}
else
{
DDView *currentLabel = [labels objectAtIndex:selectedItem];
backgroundRect = CGRectMake(0.0f, 0.0f, currentLabel.frame.origin.x + currentLabel.frame.size.width + cornerAdditionalPadding, maxHeight);
}
[backgroundView setFrame:backgroundRect];

// set title frames
leftTitleView.frame = CGRectMake(cornerAdditionalPadding + horizontalPadding, 0, [leftTitleView defaultFrameSize].width, maxHeight);

// set whole frame
[self setFrame:[self currentFrameRect]];

if (animated)
{
[UIView commitAnimations];
Expand Down Expand Up @@ -445,6 +458,12 @@ - (void)setSelectedItem:(NSUInteger)selected animated:(BOOL)animated

- (void)chooseLabel:(id)sender forEvent:(UIEvent *)event
{
// Ignore touches that are outside of the visible background view
if (![backgroundView pointInside:[[[event allTouches] anyObject] locationInView:backgroundView] withEvent:event])
{
return;
}

if (toggleMode)
{
[self setSelectedItem:((selectedItem + 1) % [labels count])];
Expand Down Expand Up @@ -479,6 +498,39 @@ - (void)chooseLabel:(id)sender forEvent:(UIEvent *)event
}
}

#pragma mark Accessors Proxy Methods

- (void)setBackgroundColor:(UIColor *)backgroundColor
{
[backgroundView setBackgroundColor:backgroundColor];
}
- (UIColor *)backgroundColor
{
return [backgroundView backgroundColor];
}

- (void)setAlpha:(CGFloat)alpha
{
[backgroundView setAlpha:alpha];
}
- (CGFloat)alpha
{
return [backgroundView alpha];
}

- (void)setOpaque:(BOOL)opaque
{
[backgroundView setOpaque:opaque];
}
- (BOOL)isOpaque
{
return [backgroundView isOpaque];
}

- (CALayer *)layer
{
return [backgroundView layer];
}

#pragma mark Utilities

Expand Down