1. 程式人生 > >工具類(為控制元件設定圓角)

工具類(為控制元件設定圓角)

為了便於日常開發效率,因此建立了一些小的工具類便於使用. 具體 code 如下: 宣告:

/*
 為控制元件新增邊框樣式_工具類
 */
#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger,LQQSideType) {
    kLQQSideTypeTop    = 0,
    kLQQSideTypeLeft   = 1,
    kLQQSideTypeBottom = 2,
    kLQQSideTypeRight  = 3,
    kLQQSideTypeAll    = 4,
};

typedef NS_ENUM(NSInteger,LQQSideAngleType) {
    kLQQSideAngleTypeTopLeft         = 0,
    kLQQSideAngleTypeTopRight        = 1,
    kLQQSideAngleTypeBottomLeft      = 2,
    kLQQSideAngleTypeBottomRight     = 3,
    kLQQSideAngleTypeAll             = 4,
};



@interface UIView (FYH)

/**
 設定不同邊的圓角

 @param sideType        圓角型別
 @param cornerRadius    圓角半徑
 */
- (void)cornerSideType:(LQQSideType)sideType withCornerRadius:(CGFloat)cornerRadius;


/**
 設定不同角的圓角

 @param sideType        圓角型別
 @param cornerRadius    圓角半徑
 */
- (void)cornerSideAngleType:(LQQSideAngleType)sideType withCornerRadius:(CGFloat)cornerRadius;


/**
 設定view某一邊框

 @param sideType    哪個邊
 @param color       邊框顏色
 @param width       邊框寬度
 */
- (void)cornerSideType:(LQQSideType)sideType lineColor:(UIColor *)color lineWidth:(CGFloat)width;

@end

實現:

#import "UIView+FYH.h"

@implementation UIView (FYH)

- (void)cornerSideType:(LQQSideType)sideType withCornerRadius:(CGFloat)cornerRadius
{
    CGSize cornerSize = CGSizeMake(cornerRadius, cornerRadius);
    UIBezierPath *maskPath;
    
    switch (sideType) {
        case kLQQSideTypeTop:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
                                                   cornerRadii:cornerSize];
        }
            break;
        case kLQQSideTypeLeft:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerBottomLeft)
                                                   cornerRadii:cornerSize];
        }
            break;
        case kLQQSideTypeBottom:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight)
                                                   cornerRadii:cornerSize];
        }
            break;
        case kLQQSideTypeRight:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:(UIRectCornerTopRight|UIRectCornerBottomRight)
                                                   cornerRadii:cornerSize];
        }
            break;
        default:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:UIRectCornerAllCorners
                                                   cornerRadii:cornerSize];
        }
            break;
    }
    
    // Create the shape layer and set its path
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    
    // Set the newly created shape layer as the mask for the image view's layer
    self.layer.mask = maskLayer;
    
    [self.layer setMasksToBounds:YES];
}


- (void)cornerSideAngleType:(LQQSideAngleType)sideType withCornerRadius:(CGFloat)cornerRadius
{
    CGSize cornerSize = CGSizeMake(cornerRadius, cornerRadius);
    UIBezierPath *maskPath;
    
    switch (sideType) {
        case kLQQSideAngleTypeTopLeft:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:(UIRectCornerTopLeft)
                                                   cornerRadii:cornerSize];
        }
            break;
        case kLQQSideAngleTypeTopRight:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:(UIRectCornerTopRight)
                                                   cornerRadii:cornerSize];
        }
            break;
        case kLQQSideAngleTypeBottomLeft:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:(UIRectCornerBottomLeft)
                                                   cornerRadii:cornerSize];
        }
            break;
        case kLQQSideAngleTypeBottomRight:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:(UIRectCornerBottomRight)
                                                   cornerRadii:cornerSize];
        }
            break;
        default:
        {
            maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                             byRoundingCorners:UIRectCornerAllCorners
                                                   cornerRadii:cornerSize];
        }
            break;
    }
    
    // Create the shape layer and set its path
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    
    // Set the newly created shape layer as the mask for the image view's layer
    self.layer.mask = maskLayer;
    
    [self.layer setMasksToBounds:YES];
}

- (void)cornerSideType:(LQQSideType)sideType lineColor:(UIColor *)color lineWidth:(CGFloat)width
{
    CAShapeLayer *layer = [CAShapeLayer layer];
    UIBezierPath *aPath = [UIBezierPath bezierPath];
    
    switch (sideType) {
        case kLQQSideTypeTop:
        {
            [aPath moveToPoint:CGPointMake(0.0, 0.0)];
            [aPath addLineToPoint:CGPointMake(self.frame.size.width, 0.0)];
        }
            break;
        case kLQQSideTypeLeft:
        {
            [aPath moveToPoint:CGPointMake(0.0, 0.0)];
            [aPath addLineToPoint:CGPointMake(0.0, self.frame.size.height)];
        }
            break;
        case kLQQSideTypeBottom:
        {
            [aPath moveToPoint:CGPointMake(0.0, self.frame.size.height)];
            [aPath addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height)];
        }
            break;
        case kLQQSideTypeRight:
        {
            [aPath moveToPoint:CGPointMake(self.frame.size.width,0.0)];
            [aPath addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height)];
            
        }
            break;
        default:
        {
            
        }
            break;
    }
    
    layer.path = aPath.CGPath;
    layer.strokeColor = color.CGColor;
    layer.lineWidth = width;
    [self.layer addSublayer:layer];
}

@end

以上便是此次分享的內容,期待大神多多指點補充,使其更加強壯!