首页 > 解决方案 > 如何用另一个 UIView 的矩形切割 UIView 的顶角

问题描述

如何在特定点用另一个视图的矩形切割视图?

要求的结果

这是两种观点

  1. UIImageView 显示图像。
  2. UIView 具有显示用户状态的背景颜色。

注意:两个视图的高度和宽度都是统一的。

一个工作示例将非常有帮助。

谢谢

标签: iosobjective-ciphone

解决方案


例子:

 -(void) viewDidAppear:(BOOL)animated {
    self.view.backgroundColor = [UIColor blueColor];
    UIImageView* yourView = [[UIImageView alloc] initWithFrame:CGRectMake(200, 200, 200, 200)];
    yourView.center = self.view.center;
    yourView.image = [UIImage imageNamed:@"testImg"];

    yourView.layer.cornerRadius = 100;
    yourView.clipsToBounds = YES;
    [self.view addSubview: yourView];

    NSUInteger radius = 50;
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) cornerRadius:radius];

    UIBezierPath *path =  [UIBezierPath bezierPathWithRect:yourView];
    [path appendPath:circlePath];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = path.CGPath;
    maskLayer.fillRule = kCAFillRuleEvenOdd;

    yourView.layer.mask  = maskLayer;

    UIImageView* smallView = [[UIImageView alloc] initWithFrame:CGRectMake(yourView.frame.origin.x + 8 ,
                                                                           yourView.frame.origin.y + 8 , 80, 80)];
    smallView.image = [UIImage imageNamed:@"testImg"];
    smallView.layer.cornerRadius = 40;
    smallView.clipsToBounds = YES;
    [self.view addSubview:smallView];

}

在此处输入图像描述将蒙版添加到图层。


推荐阅读