【问题标题】:Create a view with rounded corners (quarter circle)创建带圆角的视图(四分之一圆)
【发布时间】:2014-10-21 09:51:39
【问题描述】:

我想创建一个如下图所示的视图

为了实现这一点,我尝试了以下方法:

UIView *myView = [[UIView alloc] init];
imgView.frame = CGRectMake(50, 150, 200, 200);
[self.view addSubview:imgView];

UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:myView.bounds
                                 byRoundingCorners:UIRectCornerTopRight
                                       cornerRadii:CGSizeMake(50.0, 50.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = myView.bounds;
maskLayer.path = maskPath.CGPath;
myView.layer.mask = maskLayer;

但它对我不起作用。有人可以帮帮我吗?

【问题讨论】:

    标签: ios calayer uibezierpath cgpath cashapelayer


    【解决方案1】:

    伪代码:

        UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(50, 150, 200, 200)];
    myView.backgroundColor = [UIColor blueColor];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:myView.bounds
                                     byRoundingCorners:UIRectCornerAllCorners
                                           cornerRadii:CGSizeMake(CGRectGetWidth(myView.frame), CGRectGetHeight(myView.frame))];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = myView.bounds;
    maskLayer.path = maskPath.CGPath;
    myView.layer.mask = maskLayer;
    
    
    CGRect frame = myView.frame;
    CGRect remainder, slice;
    
    CGRectDivide(frame, &slice, &remainder, CGRectGetHeight(frame)/2, CGRectMaxYEdge);
    
    NSLog(@"%@",NSStringFromCGRect(frame));
    NSLog(@"%@",NSStringFromCGRect(remainder));
    NSLog(@"%@",NSStringFromCGRect(slice));
    
    CGRect remainder2, slice2;
    CGRectDivide(remainder, &slice2, &remainder2, CGRectGetWidth(remainder)/2, CGRectMaxXEdge);
    
    NSLog(@"%@",NSStringFromCGRect(remainder));
    NSLog(@"%@",NSStringFromCGRect(remainder2));
    NSLog(@"%@",NSStringFromCGRect(slice2));
    
    myView.frame = remainder2;
    [self.view addSubview:myView];
    

    希望你的答案很清楚..?

    【讨论】:

    • 谢谢!,它对我有用。我发现了一些可以实现相同的东西:float width = 100; UIBezierPath *path = [UIBezierPath new]; [路径 moveToPoint:CGPointMake(0, width)]; [path addArcWithCenter:CGPointMake(width, width) radius:width startAngle:M_PI endAngle:(M_PI * 1.5f) 顺时针:YES]; [路径 addLineToPoint:CGPointMake(宽度, 宽度)]; [路径关闭路径];返回路径;
    • 似乎不错.. 我只是按照您的代码解决问题而不是引入其他解决方案.. ? 谢谢..
    【解决方案2】:

    这对我有用:

    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(50, 150, 200, 200)];
    myView.backgroundColor = [UIColor blueColor];
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
    [self.view addSubview:imgView];
    
    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:myView.bounds
                                     byRoundingCorners:UIRectCornerTopRight
                                           cornerRadii:CGSizeMake(50.0, 50.0)];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = myView.bounds;
    maskLayer.path = maskPath.CGPath;
    myView.layer.mask = maskLayer;
    
    [self.view addSubview:myView];
    

    结果:

    【讨论】:

    • 谢谢,但我的要求与答案不同。使用问题中附加的代码,我可以绘制它,但在这里我想绘制四分之一圆。
    • 对不起.. 但是使用 CGRectDivide 怎么样?检查链接:soulwithmobiletechnology.blogspot.com/2011/08/…
    【解决方案3】:

    SalahSalah优秀答案的iOS 13、Swift 5翻译

    import UIKit
    
    class ViewController: UIViewController {
    
    override func viewDidLoad() {
      super.viewDidLoad()
      halfCircle(.red)
    
    // Do any additional setup after loading the view.
    }
    
    func halfCircle(_ color: UIColor) {
      let myView = UIView(frame: CGRect(x: 200, y: 200, width: 200, height: 200))
      myView.backgroundColor = color
    
      let maskPath = UIBezierPath(roundedRect: myView.bounds, byRoundingCorners: UIRectCorner.allCorners, cornerRadii: CGSize(width: myView.bounds.width, height: myView.bounds.height))
      let maskLayer = CAShapeLayer()
      maskLayer.frame = myView.bounds
      maskLayer.path = maskPath.cgPath
      myView.layer.mask = maskLayer
    
      let frame = myView.bounds.divided(atDistance: 100.0, from: CGRectEdge.maxYEdge)
    
      debugPrint(frame)
      debugPrint(frame.remainder)
      debugPrint(frame.slice)
    
      let frame2 = frame.remainder.divided(atDistance: 100.0, from: CGRectEdge.maxXEdge)
    
      debugPrint(frame2)
      debugPrint(frame2.remainder)
      debugPrint(frame2.slice)
    
      myView.frame = frame2.remainder
      self.view.addSubview(myView)
    
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-04
      • 2016-04-27
      • 2019-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      相关资源
      最近更新 更多