From 7b02207537d35bfa1714bf8beafc921f717d100a Mon Sep 17 00:00:00 2001 From: 单军华 Date: Wed, 11 Jul 2018 10:47:42 +0800 Subject: [PATCH] 首次上传 --- screendisplay/screendisplay/Classes/Category/UIButton+LMJ.m | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 176 insertions(+), 0 deletions(-) diff --git a/screendisplay/screendisplay/Classes/Category/UIButton+LMJ.m b/screendisplay/screendisplay/Classes/Category/UIButton+LMJ.m new file mode 100755 index 0000000..d13e70c --- /dev/null +++ b/screendisplay/screendisplay/Classes/Category/UIButton+LMJ.m @@ -0,0 +1,176 @@ +// +// UIButton+LMJ.m +// iOSProject +// +// Created by windshan on 2017/12/28. +// Copyright �� 2017��� windshan. All rights reserved. +// + +#import "UIButton+LMJ.h" + +static const void *UIButtonBlockKey = &UIButtonBlockKey; + +@implementation UIButton (LMJ) + +-(void)addActionHandler:(TouchedBlock)touchHandler{ + objc_setAssociatedObject(self, UIButtonBlockKey, touchHandler, OBJC_ASSOCIATION_COPY_NONATOMIC); + [self addTarget:self action:@selector(actionTouched:) forControlEvents:UIControlEventTouchUpInside]; +} +-(void)actionTouched:(UIButton *)btn{ + TouchedBlock block = objc_getAssociatedObject(self, UIButtonBlockKey); + if (block) { + block(btn.tag); + } +} + + +/** + * @brief ������������������������������ + * + * @param backgroundColor ������������ + * @param state ������������ + */ +- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state +{ + [self setBackgroundImage:[UIButton imageWithColor:backgroundColor] forState:state]; +} + ++ (UIImage *)imageWithColor:(UIColor *)color +{ + CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); + UIGraphicsBeginImageContext(rect.size); + CGContextRef context = UIGraphicsGetCurrentContext(); + + CGContextSetFillColorWithColor(context, [color CGColor]); + CGContextFillRect(context, rect); + + UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + + return image; +} + +- (instancetype)initWithFrame:(CGRect)frame buttonTitle:(NSString *)buttonTitle normalBGColor:(UIColor *)normalBGColor selectBGColor:(UIColor *)selectBGColor + normalColor:(UIColor *)normalColor selectColor:(UIColor *)selectColor buttonFont:(UIFont *)buttonFont cornerRadius:(CGFloat )cornerRadius + doneBlock:(void(^)(UIButton *))doneBlock +{ + self = [self initWithFrame:frame]; + + self.layer.masksToBounds=YES; + self.layer.cornerRadius=cornerRadius; + + + self.titleLabel.font=buttonFont; + [self setTitle:buttonTitle forState:UIControlStateNormal]; + [self setTitleColor:normalColor forState:UIControlStateNormal]; + [self setTitleColor:selectColor forState:UIControlStateHighlighted]; + [self setBackgroundImage:[UIImage imageWithColor:normalBGColor] forState:UIControlStateNormal]; + [self setBackgroundImage:[UIImage imageWithColor:selectBGColor] forState:UIControlStateHighlighted]; + + TWWeak(self); + [self addTapGestureRecognizer:^(UITapGestureRecognizer *recognizer, NSString *gestureId) { + + !doneBlock ?: doneBlock(weakself); + }]; + + return self; +} + + + + ++(UIButton *)initWithFrame:(CGRect)frame buttonTitle:(NSString *)buttonTitle normalBGColor:(UIColor *)normalBGColor selectBGColor:(UIColor *)selectBGColor + normalColor:(UIColor *)normalColor selectColor:(UIColor *)selectColor buttonFont:(UIFont *)buttonFont cornerRadius:(CGFloat )cornerRadius + doneBlock:(void(^)(UIButton *))doneBlock +{ + UIButton *solidColorButton=[[UIButton alloc]initWithFrame:frame buttonTitle:buttonTitle normalBGColor:normalBGColor selectBGColor:selectBGColor normalColor:normalColor selectColor:selectColor buttonFont:buttonFont cornerRadius:cornerRadius doneBlock:doneBlock]; + + return solidColorButton; +} + +@end + + +@implementation APRoundedButton + + +- (void)makeCorner { + UIRectCorner corners; + + switch ( self.style ) + { + case 0: + corners = UIRectCornerBottomLeft; + break; + case 1: + corners = UIRectCornerBottomRight; + break; + case 2: + corners = UIRectCornerTopLeft; + break; + case 3: + corners = UIRectCornerTopRight; + break; + case 4: + corners = UIRectCornerBottomLeft | UIRectCornerBottomRight; + break; + case 5: + corners = UIRectCornerTopLeft | UIRectCornerTopRight; + break; + case 6: + corners = UIRectCornerBottomLeft | UIRectCornerTopLeft; + break; + case 7: + corners = UIRectCornerBottomRight | UIRectCornerTopRight; + break; + case 8: + corners = UIRectCornerBottomRight | UIRectCornerTopRight | UIRectCornerTopLeft; + break; + case 9: + corners = UIRectCornerBottomRight | UIRectCornerTopRight | UIRectCornerBottomLeft; + break; + default: + corners = UIRectCornerAllCorners; + break; + } + + _nj_cornerRaduous = _nj_cornerRaduous ?: 10.0; + + UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds + byRoundingCorners:corners + cornerRadii:CGSizeMake(_nj_cornerRaduous, _nj_cornerRaduous)]; + CAShapeLayer *maskLayer = [CAShapeLayer layer]; + maskLayer.frame = self.bounds; + maskLayer.path = maskPath.CGPath; + self.layer.mask = maskLayer; +} + +- (instancetype)initWithFrame:(CGRect)frame +{ + if (self = [super initWithFrame:frame]) { + [self setupUIOnce]; + } + return self; +} + +- (instancetype)initWithCoder:(NSCoder *)coder +{ + self = [super initWithCoder:coder]; + if (self) { + [self setupUIOnce]; + } + return self; +} + + +- (void)setupUIOnce +{ + [self makeCorner]; +} + +- (void)layoutSubviews { + [super layoutSubviews]; + [self makeCorner]; +} + +@end -- Gitblit v1.8.0