diff --git a/DKCircleButton/DKCircleButton.m b/DKCircleButton/DKCircleButton.m index 99f0a82..fa9e36a 100644 --- a/DKCircleButton/DKCircleButton.m +++ b/DKCircleButton/DKCircleButton.m @@ -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]; } @@ -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; } } @@ -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); @@ -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 { @@ -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; @@ -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]; diff --git a/Example/Circle Button Demo.xcodeproj/project.pbxproj b/Example/Circle Button Demo.xcodeproj/project.pbxproj index 488aa43..8141241 100644 --- a/Example/Circle Button Demo.xcodeproj/project.pbxproj +++ b/Example/Circle Button Demo.xcodeproj/project.pbxproj @@ -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 */ @@ -49,8 +49,8 @@ 0275215E191530B6004C3D82 /* Circle Button DemoTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Circle Button DemoTests-Info.plist"; sourceTree = ""; }; 02752160191530B6004C3D82 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 02752162191530B6004C3D82 /* Circle_Button_DemoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Circle_Button_DemoTests.m; sourceTree = ""; }; - 0275216D191530F7004C3D82 /* DKCircleButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DKCircleButton.h; sourceTree = ""; }; - 0275216E191530F7004C3D82 /* DKCircleButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DKCircleButton.m; sourceTree = ""; }; + 96DDD623198B983100F3D950 /* DKCircleButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DKCircleButton.h; sourceTree = ""; }; + 96DDD624198B983100F3D950 /* DKCircleButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DKCircleButton.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -151,8 +151,16 @@ 0275216C191530F7004C3D82 /* DKCircleButton */ = { isa = PBXGroup; children = ( - 0275216D191530F7004C3D82 /* DKCircleButton.h */, - 0275216E191530F7004C3D82 /* DKCircleButton.m */, + 96DDD622198B983100F3D950 /* DKCircleButton */, + ); + path = DKCircleButton; + sourceTree = ""; + }; + 96DDD622198B983100F3D950 /* DKCircleButton */ = { + isa = PBXGroup; + children = ( + 96DDD623198B983100F3D950 /* DKCircleButton.h */, + 96DDD624198B983100F3D950 /* DKCircleButton.m */, ); path = DKCircleButton; sourceTree = ""; @@ -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; @@ -452,6 +460,7 @@ 02752168191530B6004C3D82 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 02752169191530B6004C3D82 /* Build configuration list for PBXNativeTarget "Circle Button DemoTests" */ = { isa = XCConfigurationList; @@ -460,6 +469,7 @@ 0275216B191530B6004C3D82 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/Example/Circle Button Demo.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Example/Circle Button Demo.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..0f109d7 --- /dev/null +++ b/Example/Circle Button Demo.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Example/Circle Button Demo.xcodeproj/project.xcworkspace/xcuserdata/Jason.xcuserdatad/UserInterfaceState.xcuserstate b/Example/Circle Button Demo.xcodeproj/project.xcworkspace/xcuserdata/Jason.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..0c7fe01 Binary files /dev/null and b/Example/Circle Button Demo.xcodeproj/project.xcworkspace/xcuserdata/Jason.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Example/Circle Button Demo.xcodeproj/xcuserdata/Jason.xcuserdatad/xcschemes/Circle Button Demo.xcscheme b/Example/Circle Button Demo.xcodeproj/xcuserdata/Jason.xcuserdatad/xcschemes/Circle Button Demo.xcscheme new file mode 100644 index 0000000..58411db --- /dev/null +++ b/Example/Circle Button Demo.xcodeproj/xcuserdata/Jason.xcuserdatad/xcschemes/Circle Button Demo.xcscheme @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Example/Circle Button Demo.xcodeproj/xcuserdata/Jason.xcuserdatad/xcschemes/xcschememanagement.plist b/Example/Circle Button Demo.xcodeproj/xcuserdata/Jason.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..3783188 --- /dev/null +++ b/Example/Circle Button Demo.xcodeproj/xcuserdata/Jason.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,27 @@ + + + + + SchemeUserState + + Circle Button Demo.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 02752139191530B6004C3D82 + + primary + + + 02752154191530B6004C3D82 + + primary + + + + + diff --git a/Example/Circle Button Demo/DKCircleButton/DKCircleButton.h b/Example/Circle Button Demo/DKCircleButton/DKCircleButton.h index 4484e78..49c622d 100644 --- a/Example/Circle Button Demo/DKCircleButton/DKCircleButton.h +++ b/Example/Circle Button Demo/DKCircleButton/DKCircleButton.h @@ -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 diff --git a/Example/Circle Button Demo/DKCircleButton/DKCircleButton.m b/Example/Circle Button Demo/DKCircleButton/DKCircleButton.m index 99f0a82..23cd140 100644 --- a/Example/Circle Button Demo/DKCircleButton/DKCircleButton.m +++ b/Example/Circle Button Demo/DKCircleButton/DKCircleButton.m @@ -103,6 +103,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); @@ -115,6 +118,7 @@ - (void)updateMaskToBounds:(CGRect)maskBounds { self.layer.borderWidth = self.borderSize; self.highLightView.frame = self.bounds; + } - (void)blink { @@ -226,4 +230,34 @@ - (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 +{ + DKCircleButton *readModeBtn = [[DKCircleButton alloc] init]; + readModeBtn.frame = CGRectMake(0, 0, size.width, size.height); + + animated?[readModeBtn blink]:nil; + + readModeBtn.borderSize = borderSize; + [readModeBtn addTarget:target action:action forControlEvents:UIControlEventTouchDown]; + + [readModeBtn setTitle:title forState:UIControlStateNormal]; + readModeBtn.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:17]; + + return readModeBtn; +} + +- (void)selectedWithAnimation:(BOOL)animation +{ + animation?[self blink]: nil; + [self setSelected:YES]; + self.backgroundColor = [UIColor colorWithWhite:1 alpha:0.25]; + +} +- (void)deselected +{ + [self setSelected:NO]; + self.backgroundColor = nil; +} + @end