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
    

     

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

  • 相关阅读:
    如何禁止用户直接对TextBox进行数据粘贴?(ASP.NET WEB开发)
    jquery过滤选择器前加空格与不加空格的区别(转)
    linux设置ip.dns.gateway
    Adobe Fireworks CS4 序列号(注册码)
    AS3清空数组的四种方法
    Flash中用AS3做的游戏,导出apk安装到手机上滤镜效果出不来为什么?
    用AS3清空容器下所有子显示对象
    对Linux进程的理解
    C++基础
    虚拟机三种网络模式(hostonly、Bridged、NAT)
  • 原文地址:https://www.cnblogs.com/survivorsfyh/p/9635124.html
Copyright © 2011-2022 走看看