zoukankan      html  css  js  c++  java
  • UIView改变边框宽度中间内容大小不变

    最近有个需求是,保证中间圆大小不变,动态改变边线宽度。这里比较简单的方法有两种:view和UIBezierPath

    //view

        

        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 100, 100)];

        view1.backgroundColor = [UIColor redColor];

        view1.layer.cornerRadius = 50;

        view1.clipsToBounds = YES;

        view1.layer.borderColor = [UIColor greenColor].CGColor;

        view1.layer.borderWidth = 10;

        [self.view addSubview:view1];

        

        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(200, 100, 120, 120)];

        view2.backgroundColor = [UIColor redColor];

        view2.layer.cornerRadius = 60;

        view2.clipsToBounds = YES;

        view2.layer.borderColor = [UIColor greenColor].CGColor;

        view2.layer.borderWidth = 20;

        view2.center = CGPointMake(255, 150);

        [self.view addSubview:view2];

        

        //这里保证填充大小不变  view的宽、高的增量和cornerRadius的增量、borderWidth的增量比为:211

        

        UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(10, 250, 100, 100)];

        view3.backgroundColor = [UIColor redColor];

        view3.layer.cornerRadius = 50;

        view3.clipsToBounds = YES;

        [self.view addSubview:view3]; 

     //贝塞尔曲线

        

        UIBezierPath *path=[UIBezierPath bezierPath];

        [path addArcWithCenter:CGPointMake(60, 150) radius:50 startAngle:2 endAngle:M_PI*2+2

                     clockwise:YES];

        

        CAShapeLayer *layer1=[CAShapeLayer layer];

        layer1.path=path.CGPath;

        layer1.fillColor = [UIColor redColor].CGColor;

        layer1.strokeColor = [UIColor greenColor].CGColor;

        layer1.lineWidth=10;

        [self.view.layer addSublayer:layer1];

        

        UIBezierPath *path1=[UIBezierPath bezierPath];

        [path1 addArcWithCenter:CGPointMake(250, 150) radius:55 startAngle:2 endAngle:M_PI*2+2

                     clockwise:YES];

        

        CAShapeLayer *layer2=[CAShapeLayer layer];

        layer2.path=path1.CGPath;

        layer2.fillColor = [UIColor redColor].CGColor;

        layer2.strokeColor = [UIColor greenColor].CGColor;

        layer2.lineWidth=20;

        [self.view.layer addSublayer:layer2];

        

        //这里要保证中间填充部分大小不变的情况下,改变lineWidthlineWidth的增量与radius增量的比例为21

        

        UIBezierPath *path2=[UIBezierPath bezierPath];

        [path2 addArcWithCenter:CGPointMake(250, 350) radius:50 startAngle:2 endAngle:M_PI*2+2

                      clockwise:YES];

        

        CAShapeLayer *layer3=[CAShapeLayer layer];

        layer3.path=path2.CGPath;

        layer3.fillColor = [UIColor redColor].CGColor;

        layer3.strokeColor = [UIColor greenColor].CGColor;

        layer3.lineWidth=10;

        [self.view.layer addSublayer:layer3];

    这里需要注意的就是:

    view的操作 borderWidth的大小不影响viewframe。当borderWidth增大时,viewframe不变,那么中间部分的直径就会减小;

    path的操作 lineWidth的大小会影响视图的frame。当lineWidth增大时,viewframe会增大lineWidth,这样只增加lineWidth的大小,中间部分直径不变

  • 相关阅读:
    Hadoopif.for.while 语句
    完全分布模式的四大模块设置
    语法糖定义
    Karaf 依赖equinox and felix,karaf 本Apache的很多项目作为基础框架
    Karaf 基于 osgi
    MEF(Managed Extensibility Framework) 微软平台插件化开发
    析构函数,构造函数
    C#和ASP.Net面试题目集锦
    论C# java的基本类型
    Boolean.parseBoolean("true") 和 Boolean.getBoolean("true");的区别及用法
  • 原文地址:https://www.cnblogs.com/NINIiOS/p/4425212.html
Copyright © 2011-2022 走看看