有的人想到 可以在方法里面直接調用哪個控件用就在哪里調用 ?但是不推薦 因為用到的控件 可能是UILabel蒂萎、UIBUtton、UIView等 而且為了 貼近系統(tǒng)屬性設置的特點 ? 最好是給UIView加分類
使用 (注意震嫉,由于基于Layer?控件的背景色用我提供的dj_backgroundColor)
? ? UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 160, 100)];
? ? view.dj_backgroundColor = [UIColor redColor];
? ? view.dj_shadowColor? ? = [UIColorblackColor];
? ? view.dj_shadowOffset? ? =CGSizeMake(0,6);
? ? view.dj_shadowRadius? ? =6;
? ? view.dj_shadowOpacity? =0.6;
? ? view.dj_corners? ? ? ? =UIRectCornerTopLeft | UIRectCornerBottomLeft;
? ? view.dj_radius? ? ? ? ? =25;
? ? [self.viewaddSubview:view];
分類的.h
/**
? 圓角
?*/
@property (nonatomic, assign) CGFloat? ? ? dj_radius;
/**
? 圓角位置
?*/
@property (nonatomic, assign) UIRectCorner dj_corners;
/**
? 陰影偏移量
?*/
@property(nonatomic,assign)CGSize? ? ? dj_shadowOffset;
/**
? 陰影透明度
?*/
@property(nonatomic,assign)CGFloat? ? ? dj_shadowOpacity;
/**
? 陰影圓角
?*/
@property(nonatomic,assign)CGFloat? ? ? dj_shadowRadius;
/**
? 陰影顏色
?*/
@property(nonatomic,strong)UIColor? ? *dj_shadowColor;
/**
? 背景顏色(必須使用該屬性設定背景顏色)
?*/
@property(nonatomic,strong)UIColor? ? *dj_backgroundColor;
分類的.m
#import "UIView+DJRandomRadius.h"
#import
staticvoid*bezierPathKey? ? ? = &bezierPathKey;
staticvoid*shapeLayerKey? ? ? = &shapeLayerKey;
staticvoid*radiusKey? ? ? ? ? = &radiusKey;
staticvoid*cornersKey? ? ? ? = &cornersKey;
staticvoid*shadowColorKey? ? = &shadowColorKey;
staticvoid*shadowOffsetKey? ? = &shadowOffsetKey;
staticvoid*shadowRadiusKey? ? = &shadowRadiusKey;
staticvoid*shadowOpacityKey? = &shadowOpacityKey;
staticvoid*backgroundColorKey = &backgroundColorKey;
@interface UIView ()
@property (nonatomic, strong) UIBezierPath *bezierPath;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@end
staticCGFloat? ? ? _radius;
static UIRectCorner _corners;
@implementationUIView (DJRandomRadius)
- (void)shouldStructElement {
? ? [self.bezierPath removeAllPoints];
? ? [self.shapeLayer removeFromSuperlayer];
? ? self.bezierPath = nil;
? ? self.shapeLayer = nil;
? ? self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.0];
? ? self.bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:_corners cornerRadii:CGSizeMake(_radius, _radius)];
? ? self.shapeLayer= [[CAShapeLayeralloc]init];
? ? self.shapeLayer.path = self.bezierPath.CGPath;
? ? self.shapeLayer.frame = self.bounds;
? ? [self.layer addSublayer:self.shapeLayer];
? ? self.shapeLayer.fillColor = !self.dj_backgroundColor ? [UIColor whiteColor].CGColor : self.dj_backgroundColor.CGColor;
? ? self.shapeLayer.shadowColor = self.dj_shadowColor.CGColor;
? ? self.shapeLayer.shadowOffset = self.dj_shadowOffset;
? ? self.shapeLayer.shadowOpacity = self.dj_shadowOpacity;
? ? self.shapeLayer.shadowRadius = self.dj_shadowRadius;
}
- (void)setDj_corners:(UIRectCorner)dj_corners {
? ? _corners= dj_corners;
? ? [self shouldStructElement];
? ? objc_setAssociatedObject(self, &cornersKey, @(dj_corners), OBJC_ASSOCIATION_RETAIN);
}
- (UIRectCorner)dj_corners {
? ? return [objc_getAssociatedObject(self, &cornersKey) integerValue];
}
- (void)setDj_radius:(CGFloat)dj_radius {
? ? _radius= dj_radius;
? ? [self shouldStructElement];
? ? objc_setAssociatedObject(self, &radiusKey, @(dj_radius), OBJC_ASSOCIATION_RETAIN);
}
- (CGFloat)dj_radius {
? ? return [objc_getAssociatedObject(self, &radiusKey) floatValue];
}
- (void)setDj_shadowOffset:(CGSize)dj_shadowOffset {
? ? self.shapeLayer.shadowOffset= dj_shadowOffset;
? ? objc_setAssociatedObject(self, &shadowOffsetKey, @(dj_shadowOffset), OBJC_ASSOCIATION_RETAIN);
}
- (CGSize)dj_shadowOffset {
? ? return [objc_getAssociatedObject(self, &shadowOffsetKey) CGSizeValue];
}
- (void)setDj_shadowOpacity:(CGFloat)dj_shadowOpacity {
? ? self.shapeLayer.shadowOpacity= dj_shadowOpacity;
? ? objc_setAssociatedObject(self, &shadowOpacityKey, @(dj_shadowOpacity), OBJC_ASSOCIATION_RETAIN);
}
- (CGFloat)dj_shadowOpacity {
? ? return [objc_getAssociatedObject(self, &shadowOpacityKey) floatValue];
}
- (void)setDj_shadowRadius:(CGFloat)dj_shadowRadius {
? ? self.shapeLayer.shadowRadius= dj_shadowRadius;
? ? objc_setAssociatedObject(self, &shadowRadiusKey, @(dj_shadowRadius), OBJC_ASSOCIATION_RETAIN);
}
- (CGFloat)dj_shadowRadius {
? ? return [objc_getAssociatedObject(self, &shadowRadiusKey) floatValue];
}
- (void)setDj_shadowColor:(UIColor*)dj_shadowColor {
? ? self.shapeLayer.shadowColor= dj_shadowColor.CGColor;
? ? objc_setAssociatedObject(self, &shadowColorKey, dj_shadowColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIColor*)dj_shadowColor {
? ? return objc_getAssociatedObject(self, &shadowColorKey);
}
- (void)setDj_backgroundColor:(UIColor*)dj_backgroundColor {
? ? self.shapeLayer.fillColor= dj_backgroundColor.CGColor;
? ? objc_setAssociatedObject(self, &backgroundColorKey, dj_backgroundColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIColor*)dj_backgroundColor {
? ? return objc_getAssociatedObject(self, &backgroundColorKey);
}
- (void)setBezierPath:(UIBezierPath*)bezierPath {
? ? objc_setAssociatedObject(self, &bezierPathKey, bezierPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIBezierPath*)bezierPath {
? ? return objc_getAssociatedObject(self, &bezierPathKey);
}
- (void)setShapeLayer:(CAShapeLayer*)shapeLayer {
? ? objc_setAssociatedObject(self, &shapeLayerKey, shapeLayer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CAShapeLayer*)shapeLayer {
? ? return objc_getAssociatedObject(self, &shapeLayerKey);
}