创建带有圆角的视图(四分之一圆)

问题描述 投票:0回答:3

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

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9KVkhjTi5wbmcifQ==” alt =“在此处输入图像说明”>

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

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
3个回答
2
投票

sudo代码:

    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];

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS83T2w2dC5wbmcifQ==” alt =“在此处输入图像描述”>

希望您的答案很清楚..😁


0
投票

它与我一起使用此代码:

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];

结果:

“在此处输入图像描述”


0
投票

iOS 13,Salah的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)

  }
}
© www.soinside.com 2019 - 2024. All rights reserved.