123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- //
- // UIView+MAC.m
- // WeiSchoolTeacher
- //
- // Created by MacKun on 15/12/11.
- // Copyright © 2015年 MacKun. All rights reserved.
- //
- #import "UIView+MAC.h"
- #import <QuartzCore/QuartzCore.h>
- #import <objc/runtime.h>
- static char BAViewCookieKey;
- @implementation UIView(MAC)
- /**
- * @brief 找到当前view所在的viewcontroler
- */
- - (UIViewController *)viewController
- {
- UIResponder *responder = self.nextResponder;
- do {
- if ([responder isKindOfClass:[UIViewController class]]) {
- return (UIViewController *)responder;
- }
- responder = responder.nextResponder;
- } while (responder);
- return nil;
- }
- - cookie
- {
- return objc_getAssociatedObject(self, &BAViewCookieKey);
- }
- - (void)setCookie:cookie
- {
- objc_setAssociatedObject(self, &BAViewCookieKey, cookie, OBJC_ASSOCIATION_RETAIN);
- }
- - (void)setSize:(CGSize)size;
- {
- CGPoint origin = [self frame].origin;
-
- [self setFrame:CGRectMake(origin.x, origin.y, size.width, size.height)];
- }
- - (CGSize)size;
- {
- return [self frame].size;
- }
- - (CGFloat)left;
- {
- return CGRectGetMinX([self frame]);
- }
- - (void)setLeft:(CGFloat)x;
- {
- CGRect frame = [self frame];
- frame.origin.x = x;
- [self setFrame:frame];
- }
- - (CGFloat)top;
- {
- return CGRectGetMinY([self frame]);
- }
- - (void)setTop:(CGFloat)y;
- {
- CGRect frame = [self frame];
- frame.origin.y = y;
- [self setFrame:frame];
- }
- - (CGFloat)right;
- {
- return CGRectGetMaxX([self frame]);
- }
- - (void)setRight:(CGFloat)right;
- {
- CGRect frame = [self frame];
- frame.origin.x = right - frame.size.width;
-
- [self setFrame:frame];
- }
- - (CGFloat)bottom;
- {
- return CGRectGetMaxY([self frame]);
- }
- - (void)setBottom:(CGFloat)bottom;
- {
- CGRect frame = [self frame];
- frame.origin.y = bottom - frame.size.height;
-
- [self setFrame:frame];
- }
- - (CGFloat)centerX;
- {
- return [self center].x;
- }
- - (void)setCenterX:(CGFloat)centerX;
- {
- [self setCenter:CGPointMake(centerX, self.center.y)];
- }
- - (CGFloat)centerY;
- {
- return [self center].y;
- }
- - (void)setCenterY:(CGFloat)centerY;
- {
- [self setCenter:CGPointMake(self.center.x, centerY)];
- }
- - (CGFloat)width;
- {
- return CGRectGetWidth([self frame]);
- }
- - (void)setWidth:(CGFloat)width;
- {
- CGRect frame = [self frame];
- frame.size.width = width;
-
- [self setFrame:CGRectStandardize(frame)];
- }
- - (CGFloat)height;
- {
- return CGRectGetHeight([self frame]);
- }
- - (void)setHeight:(CGFloat)height;
- {
- CGRect frame = [self frame];
- frame.size.height = height;
- [self setFrame:CGRectStandardize(frame)];
- }
- -(void)addShadowonBottom
- {
- self.layer.shadowOpacity = 0.4;
- self.layer.shadowRadius = 0.7;
- self.layer.shadowOffset = CGSizeMake(0, 0);
-
- UIBezierPath *path = [UIBezierPath bezierPath];
-
- CGPoint p1 = CGPointMake(0.0, 0.0+self.frame.size.height);
- CGPoint p2 = CGPointMake(0.0+self.frame.size.width, p1.y);
- CGPoint c1 = CGPointMake((p1.x+p2.x)/256 , p1.y+1.50);
- CGPoint c2 = CGPointMake(c1.x*255, c1.y);
-
- [path moveToPoint:p1];
- [path addCurveToPoint:p2 controlPoint1:c1 controlPoint2:c2];
-
- self.layer.shadowPath = path.CGPath;
- }
- -(void)addShadowonTop
- {
- self.layer.shadowOpacity = 0.4;
- self.layer.shadowRadius = 0.7;
- self.layer.shadowOffset = CGSizeMake(0, 0);
-
- UIBezierPath *path = [UIBezierPath bezierPath];
-
- CGPoint p1 = CGPointMake(0.0, 0.0);
- CGPoint p2 = CGPointMake(0.0+self.frame.size.width, p1.y);
- CGPoint c1 = CGPointMake((p1.x+p2.x)/4 , p1.y-2.5);
- CGPoint c2 = CGPointMake(c1.x*3, c1.y);
-
- [path moveToPoint:p1];
- [path addCurveToPoint:p2 controlPoint1:c1 controlPoint2:c2];
-
- self.layer.shadowPath = path.CGPath;
- }
- -(void)addGrayGradientShadow
- {
- self.layer.shadowOpacity = 0.4;
-
- CGFloat rectWidth = 10.0;
- CGFloat rectHeight = self.frame.size.height;
-
- CGMutablePathRef shadowPath = CGPathCreateMutable();
- CGPathMoveToPoint(shadowPath, NULL, 0.0, 0.0);
- CGPathAddRect(shadowPath, NULL, CGRectMake(0.0-rectWidth, 0.0, rectWidth, rectHeight));
- CGPathAddRect(shadowPath, NULL, CGRectMake(self.frame.size.width, 0.0, rectWidth, rectHeight));
-
- self.layer.shadowPath = shadowPath;
- CGPathRelease(shadowPath);
-
- self.layer.shadowOffset = CGSizeMake(0, 0);
-
- self.layer.shadowRadius = 10.0;
- }
- -(void)addShadowWithColor:(UIColor *)color width:(CGFloat)width opacity:(CGFloat)opacity{
- // 阴影颜色
- self.layer.shadowColor = color.CGColor;
- // 阴影偏移,默认(0, -3)
- self.layer.shadowOffset = CGSizeMake(0,0);
- // 阴影透明度,默认0
- self.layer.shadowOpacity = opacity;
- // 阴影半径,默认3
- self.layer.shadowRadius = width;
- }
- -(void)addMovingShadow
- {
- static float step = 0.0;
- if (step>20.0) {
- step = 0.0;
- }
-
- self.layer.shadowOpacity = 0.4;
- self.layer.shadowRadius = 1.5;
- self.layer.shadowOffset = CGSizeMake(0, 0);
-
- UIBezierPath *path = [UIBezierPath bezierPath];
-
- CGPoint p1 = CGPointMake(0.0, 0.0+self.frame.size.height);
- CGPoint p2 = CGPointMake(0.0+self.frame.size.width, p1.y);
- CGPoint c1 = CGPointMake((p1.x+p2.x)/4 , p1.y+step);
- CGPoint c2 = CGPointMake(c1.x*3, c1.y);
-
- [path moveToPoint:p1];
- [path addCurveToPoint:p2 controlPoint1:c1 controlPoint2:c2];
-
- self.layer.shadowPath = path.CGPath;
- step += 0.1;
- [self performSelector:@selector(addMovingShadow) withObject:nil afterDelay:1.0/30.0];
- }
- -(void)removeShadow
- {
- self.layer.shadowOpacity =0;
- self.layer.shadowRadius = 0;
- self.layer.shadowOffset = CGSizeMake(0, 0);
-
- UIBezierPath *path = [UIBezierPath bezierPath];
-
- CGPoint p1 = CGPointMake(0.0, 0.0);
- CGPoint p2 = CGPointMake(0.0+self.frame.size.width, p1.y);
- CGPoint c1 = CGPointMake(0 , 0);
- CGPoint c2 = CGPointMake(0, 0);
-
- [path moveToPoint:p1];
- [path addCurveToPoint:p2 controlPoint1:c1 controlPoint2:c2];
-
- self.layer.shadowPath = path.CGPath;
- }
- //针对给定的坐标系居中
- - (void)centerInRect:(CGRect)rect;
- {
- //如果参数是小数,则求最大的整数但不大于本身.
- //CGRectGetMidX获取中心点的X轴坐标
- [self setCenter:CGPointMake(floorf(CGRectGetMidX(rect)) + ((int)floorf([self width]) % 2 ? .5 : 0) , floorf(CGRectGetMidY(rect)) + ((int)floorf([self height]) % 2 ? .5 : 0))];
- }
- //针对给定的坐标系纵向居中
- - (void)centerVerticallyInRect:(CGRect)rect;
- {
- [self setCenter:CGPointMake([self center].x, floorf(CGRectGetMidY(rect)) + ((int)floorf([self height]) % 2 ? .5 : 0))];
- }
- //针对给定的坐标系横向居中
- - (void)centerHorizontallyInRect:(CGRect)rect;
- {
- [self setCenter:CGPointMake(floorf(CGRectGetMidX(rect)) + ((int)floorf([self width]) % 2 ? .5 : 0), [self center].y)];
- }
- //相对父视图居中
- - (void)centerInSuperView;
- {
- [self centerInRect:[[self superview] bounds]];
- }
- - (void)centerVerticallyInSuperView;
- {
- [self centerVerticallyInRect:[[self superview] bounds]];
- }
- - (void)centerHorizontallyInSuperView;
- {
- [self centerHorizontallyInRect:[[self superview] bounds]];
- }
- //同一父视图的兄弟视图水平居中
- - (void)centerHorizontallyBelow:(UIView *)view padding:(CGFloat)padding;
- {
- // for now, could use screen relative positions.
- NSAssert([self superview] == [view superview], @"views must have the same parent");
-
- [self setCenter:CGPointMake([view center].x,
- floorf(padding + CGRectGetMaxY([view frame]) + ([self height] / 2)))];
- }
- - (void)centerHorizontallyBelow:(UIView *)view;
- {
- [self centerHorizontallyBelow:view padding:0];
- }
- - (void)setFrameSize:(CGSize)newSize
- {
- CGRect f = self.frame;
- f.size = newSize;
- self.frame = f;
- }
- - (void)setFrameWidth:(CGFloat)newWidth {
- CGRect f = self.frame;
- f.size.width = newWidth;
- self.frame = f;
- }
- - (void)setFrameHeight:(CGFloat)newHeight {
- CGRect f = self.frame;
- f.size.height = newHeight;
- self.frame = f;
- }
- - (void)setFrameOrigin:(CGPoint)newOrigin
- {
- CGRect f = self.frame;
- f.origin = newOrigin;
- self.frame = f;
- }
- - (void)setFrameOriginX:(CGFloat)newX {
- CGRect f = self.frame;
- f.origin.x = newX;
- self.frame = f;
- }
- - (void)setFrameOriginY:(CGFloat)newY {
- CGRect f = self.frame;
- f.origin.y = newY;
- self.frame = f;
- }
- - (void)addSizeWidth:(CGFloat)newWidth {
- CGRect f = self.frame;
- f.size.width += newWidth;
- self.frame = f;
- }
- - (void)addSizeHeight:(CGFloat)newHeight {
- CGRect f = self.frame;
- f.size.height += newHeight;
- self.frame = f;
- }
- - (void)addOriginX:(CGFloat)newX {
- CGRect f = self.frame;
- f.origin.x += newX;
- self.frame = f;
- }
- - (void)addOriginY:(CGFloat)newY {
- CGRect f = self.frame;
- f.origin.y += newY;
- self.frame = f;
- }
- - (void)cornerRadius:(CGFloat)cornerRadius withUIViewRadiusType:(UIViewRadiusType)type
- {
- self.clipsToBounds = YES;
- UIBezierPath *cornerRadiusPath;
- switch (type) {
- case UIViewRadiusTypeNone:
- {
- cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
- }
- break;
- case UIViewRadiusTypeTopLeft:
- {
- cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
-
- }
- break;
- case UIViewRadiusTypeTopRight:
- {
- cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
- }
- break;
- case UIViewRadiusTypeBottomLeft:
- {
- cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
- }
- break;
- case UIViewRadiusTypeBottomRight:
- {
- cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
- }
- break;
-
- default:
- break;
- }
-
-
- CAShapeLayer *cornerRadiusLayer = [ [CAShapeLayer alloc ] init];
- cornerRadiusLayer.frame = self.bounds;
- cornerRadiusLayer.path = cornerRadiusPath.CGPath; self.layer.mask = cornerRadiusLayer;
-
- UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(15, 15)];
-
- }
- @end
|