Skip to content
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
108 changes: 62 additions & 46 deletions DKCircleButton/DKCircleButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,61 +20,74 @@ @interface DKCircleButton ()

@implementation DKCircleButton

@synthesize highLightView = _highLightView;
@synthesize displayShading = _displayShading;
@synthesize gradientLayerTop = _gradientLayerTop;
@synthesize gradientLayerBottom = _gradientLayerBottom;
@synthesize borderSize = _borderSize;
@synthesize borderColor = _borderColor;
@synthesize animateTap = _animateTap;
@synthesize highLightView;
@synthesize displayShading;
@synthesize gradientLayerTop;
@synthesize gradientLayerBottom;
@synthesize borderSize;
@synthesize borderColor;
@synthesize animateTap;

- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self commonInit];
}
return self;
}

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];

if (self) {
_highLightView = [[UIView alloc] initWithFrame:frame];

_highLightView.userInteractionEnabled = YES;
_highLightView.alpha = 0;
_highLightView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];

_borderColor = [UIColor whiteColor];
_animateTap = YES;
_borderSize = DKCircleButtonBorderWidth;

self.clipsToBounds = YES;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;

_gradientLayerTop = [CAGradientLayer layer];
_gradientLayerTop.frame = CGRectMake(0.0, 0.0, frame.size.width, frame.size.height / 4);
_gradientLayerTop.colors = @[(id)[UIColor blackColor].CGColor, (id)[[UIColor lightGrayColor] colorWithAlphaComponent:0.01].CGColor];

_gradientLayerBottom = [CAGradientLayer layer];
_gradientLayerBottom.frame = CGRectMake(0.0, frame.size.height * 3 / 4, frame.size.width, frame.size.height / 4);
_gradientLayerBottom.colors = @[(id)[[UIColor lightGrayColor] colorWithAlphaComponent:0.01].CGColor, (id)[UIColor blackColor].CGColor];

[self addSubview:_highLightView];
[self commonInit];
}

return self;
}

- (void)setDisplayShading:(BOOL)displayShading {
_displayShading = displayShading;
-(void)commonInit{
highLightView = [[UIView alloc] initWithFrame:self.bounds];

highLightView.userInteractionEnabled = YES;
highLightView.alpha = 0;
highLightView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];

borderColor = [UIColor whiteColor];
animateTap = YES;
borderSize = DKCircleButtonBorderWidth;

self.clipsToBounds = YES;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;

gradientLayerTop = [CAGradientLayer layer];
gradientLayerTop.frame = CGRectMake(0.0, 0.0, self.bounds.size.width, self.bounds.size.height / 4);
gradientLayerTop.colors = @[(id)[UIColor blackColor].CGColor, (id)[[UIColor lightGrayColor] colorWithAlphaComponent:0.01].CGColor];

gradientLayerBottom = [CAGradientLayer layer];
gradientLayerBottom.frame = CGRectMake(0.0, self.bounds.size.height * 3 / 4, self.bounds.size.width, self.bounds.size.height / 4);
gradientLayerBottom.colors = @[(id)[[UIColor lightGrayColor] colorWithAlphaComponent:0.01].CGColor, (id)[UIColor blackColor].CGColor];

[self addSubview:highLightView];
}

- (void)setDisplayShading:(BOOL)dS {
displayShading = dS;

if (displayShading) {
[self.layer addSublayer:self.gradientLayerTop];
[self.layer addSublayer:self.gradientLayerBottom];
[self.layer addSublayer:gradientLayerTop];
[self.layer addSublayer:gradientLayerBottom];
} else {
[self.gradientLayerTop removeFromSuperlayer];
[self.gradientLayerBottom removeFromSuperlayer];
[gradientLayerTop removeFromSuperlayer];
[gradientLayerBottom removeFromSuperlayer];
}
[self layoutSubviews];
}

- (void)setBorderColor:(UIColor *)borderColor {
_borderColor = borderColor;
- (void)setBorderColor:(UIColor *)bC {
borderColor = bC;

[self layoutSubviews];
}
Expand All @@ -87,12 +100,12 @@ - (void)layoutSubviews {
- (void)setHighlighted:(BOOL)highlighted {

if (highlighted) {
self.layer.borderColor = [self.borderColor colorWithAlphaComponent:1.0].CGColor;
self.layer.borderColor = [borderColor colorWithAlphaComponent:1.0].CGColor;

[self triggerAnimateTap];
}
else {
self.layer.borderColor = [self.borderColor colorWithAlphaComponent:0.7].CGColor;
self.layer.borderColor = [borderColor colorWithAlphaComponent:0.7].CGColor;
}
}

Expand All @@ -103,6 +116,9 @@ - (void)updateMaskToBounds:(CGRect)maskBounds {

maskLayer.bounds = maskBounds;
maskLayer.path = maskPath;

CGPathRelease(maskPath);

maskLayer.fillColor = [UIColor blackColor].CGColor;

CGPoint point = CGPointMake(maskBounds.size.width/2, maskBounds.size.height/2);
Expand All @@ -111,10 +127,10 @@ - (void)updateMaskToBounds:(CGRect)maskBounds {
[self.layer setMask:maskLayer];

self.layer.cornerRadius = CGRectGetHeight(maskBounds) / 2.0;
self.layer.borderColor = [self.borderColor colorWithAlphaComponent:0.7].CGColor;
self.layer.borderWidth = self.borderSize;
self.layer.borderColor = [borderColor colorWithAlphaComponent:0.7].CGColor;
self.layer.borderWidth = borderSize;

self.highLightView.frame = self.bounds;
highLightView.frame = self.bounds;
}

- (void)blink {
Expand Down Expand Up @@ -152,11 +168,11 @@ - (void)blink {

- (void)triggerAnimateTap {

if (self.animateTap == NO) {
if (animateTap == NO) {
return;
}

self.highLightView.alpha = 1;
highLightView.alpha = 1;

__weak typeof(self) this = self;

Expand All @@ -179,7 +195,7 @@ - (void)triggerAnimateTap {
circleShape.position = shapePosition;
circleShape.fillColor = [UIColor clearColor].CGColor;
circleShape.opacity = 0;
circleShape.strokeColor = self.borderColor.CGColor;
circleShape.strokeColor = borderColor.CGColor;
circleShape.lineWidth = 2.0;

[self.superview.layer addSublayer:circleShape];
Expand Down
22 changes: 16 additions & 6 deletions Example/Circle Button Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
02752159191530B6004C3D82 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 02752141191530B6004C3D82 /* UIKit.framework */; };
02752161191530B6004C3D82 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 0275215F191530B6004C3D82 /* InfoPlist.strings */; };
02752163191530B6004C3D82 /* Circle_Button_DemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 02752162191530B6004C3D82 /* Circle_Button_DemoTests.m */; };
0275216F191530F7004C3D82 /* DKCircleButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 0275216E191530F7004C3D82 /* DKCircleButton.m */; };
96DDD625198B983100F3D950 /* DKCircleButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 96DDD624198B983100F3D950 /* DKCircleButton.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -49,8 +49,8 @@
0275215E191530B6004C3D82 /* Circle Button DemoTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Circle Button DemoTests-Info.plist"; sourceTree = "<group>"; };
02752160191530B6004C3D82 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
02752162191530B6004C3D82 /* Circle_Button_DemoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Circle_Button_DemoTests.m; sourceTree = "<group>"; };
0275216D191530F7004C3D82 /* DKCircleButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DKCircleButton.h; sourceTree = "<group>"; };
0275216E191530F7004C3D82 /* DKCircleButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DKCircleButton.m; sourceTree = "<group>"; };
96DDD623198B983100F3D950 /* DKCircleButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DKCircleButton.h; sourceTree = "<group>"; };
96DDD624198B983100F3D950 /* DKCircleButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DKCircleButton.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -151,8 +151,16 @@
0275216C191530F7004C3D82 /* DKCircleButton */ = {
isa = PBXGroup;
children = (
0275216D191530F7004C3D82 /* DKCircleButton.h */,
0275216E191530F7004C3D82 /* DKCircleButton.m */,
96DDD622198B983100F3D950 /* DKCircleButton */,
);
path = DKCircleButton;
sourceTree = "<group>";
};
96DDD622198B983100F3D950 /* DKCircleButton */ = {
isa = PBXGroup;
children = (
96DDD623198B983100F3D950 /* DKCircleButton.h */,
96DDD624198B983100F3D950 /* DKCircleButton.m */,
);
path = DKCircleButton;
sourceTree = "<group>";
Expand Down Expand Up @@ -254,7 +262,7 @@
buildActionMask = 2147483647;
files = (
0275214A191530B6004C3D82 /* main.m in Sources */,
0275216F191530F7004C3D82 /* DKCircleButton.m in Sources */,
96DDD625198B983100F3D950 /* DKCircleButton.m in Sources */,
0275214E191530B6004C3D82 /* DKAppDelegate.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -452,6 +460,7 @@
02752168191530B6004C3D82 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
02752169191530B6004C3D82 /* Build configuration list for PBXNativeTarget "Circle Button DemoTests" */ = {
isa = XCConfigurationList;
Expand All @@ -460,6 +469,7 @@
0275216B191530B6004C3D82 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0600"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "02752139191530B6004C3D82"
BuildableName = "Circle Button Demo.app"
BlueprintName = "Circle Button Demo"
ReferencedContainer = "container:Circle Button Demo.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "02752154191530B6004C3D82"
BuildableName = "Circle Button DemoTests.xctest"
BlueprintName = "Circle Button DemoTests"
ReferencedContainer = "container:Circle Button Demo.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "02752139191530B6004C3D82"
BuildableName = "Circle Button Demo.app"
BlueprintName = "Circle Button Demo"
ReferencedContainer = "container:Circle Button Demo.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "02752139191530B6004C3D82"
BuildableName = "Circle Button Demo.app"
BlueprintName = "Circle Button Demo"
ReferencedContainer = "container:Circle Button Demo.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "02752139191530B6004C3D82"
BuildableName = "Circle Button Demo.app"
BlueprintName = "Circle Button Demo"
ReferencedContainer = "container:Circle Button Demo.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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>SchemeUserState</key>
<dict>
<key>Circle Button Demo.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>02752139191530B6004C3D82</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>02752154191530B6004C3D82</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
11 changes: 11 additions & 0 deletions Example/Circle Button Demo/DKCircleButton/DKCircleButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@

- (void)setImage:(UIImage *)image animated: (BOOL)animated;

/**
* fast creation
*/
+ (id)circleButtonWithSize:(CGSize)size borderSize:(CGFloat)borderSize normalTitle:(NSString *)title addTarget:(id)target action:(SEL)action animated:(BOOL)animated;

///

- (void)selectedWithAnimation:(BOOL)animation;

- (void)deselected;

@end
Loading