zoukankan      html  css  js  c++  java
  • 工具类(为控件设置圆角)

    为了便于日常开发效率,因此创建了一些小的工具类便于使用.
    具体 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
    

     

    以上便是此次分享的内容,期待大神多多指点补充,使其更加强壮!

  • 相关阅读:
    vue 中 过滤filter 和 foreach的使用
    vuex 获取数据
    动态设置样式 calc计算
    vue-echarts-v3 使用 dataZoom属性 相关笔记
    访存加速-Speed-up of Memory Access Intensive Program
    台式机新添内存条无法开机?
    ADB的版本问题
    1184. Distance Between Bus Stops
    485. Max Consecutive Ones
    448. Find All Numbers Disappeared in an Array
  • 原文地址:https://www.cnblogs.com/survivorsfyh/p/9635124.html
Copyright © 2011-2022 走看看