用四个角点创建 UIView? [英] Create UIView with four corner points?

查看:30
本文介绍了用四个角点创建 UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建带有四个角的 UIView.

How can i create UIView with four corner.

0 = "NSPoint: {79.583483072916664, 54.148963562011716}";  // top left
1 = "NSPoint: {274.96375, 30.877176879882814}"; // top right
2 = "NSPoint: {306.15565104166666, 357.91370518493653}"; // bottom right
3 = "NSPoint: {77.101464843749994, 363.47374218750002}"; // bottom left

我不知道从哪里开始.

推荐答案

由于您的视图不是常规矩形,您需要先绘制一条路径,即 BezierPath 并获得一个形状并将该形状强加于到视图.此视图 myView 的行为与任何其他普通视图

As your view is not a regular rectangle you need to first draw a path i.e. BezierPath and get a shape and imposing the shape on to the View. This view myView would behave like any other normal view

    UIView *myView = [[UIView alloc]initWithFrame:self.view.frame];    
    UIBezierPath *linePath = [UIBezierPath bezierPath];

    [linePath moveToPoint:CGPointMake(points[0].x, points[0].y)];
    [linePath addLineToPoint:CGPointMake(points[1].x, points[1].y)];
    [linePath addLineToPoint:CGPointMake(points[2].x, points[2].y)];
    [linePath addLineToPoint:CGPointMake(points[3].x, points[3].y)];
    [linePath closePath];
    CAShapeLayer *shape = [[CAShapeLayer alloc]init];
    [shape setPath:linePath.CGPath];
    myView.layer.mask=shape;
    myView.backgroundColor = [UIColor redColor];
   [self.view addSubview:myView];

输出截图如下:

这篇关于用四个角点创建 UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆