简体   繁体   English

在iOS中定制形状(倒T)边界Uiview

[英]Custom shaped (inverted T) bordered Uiview in iOS

I have to create a custom shaped (inverted T) bordered Uiview on iOS. 我必须在iOS上创建一个自定义形状(倒T)边界Uiview。 I am attaching the screenshot below. 我附上下面的截图。 I have researched a lot and I found a way using UIBezierPath from here . 我已经研究了很多,我从这里找到了一种使用UIBezierPath的方法。

But I didn't get any idea to shape my view as inverted T shaped. 但是我没有想到把我的视角塑造成倒T形。

倒带边T

UIView s are always rectangular. UIView总是长方形的。 From the documentation: 从文档:

The UIView class defines a rectangular area on the screen and the interfaces for managing the content in that area. UIView类在屏幕上定义了一个矩形区域,以及用于管理该区域内容的接口。

Even though the view is limited to being rectangular, you can shape your active area in any way that you like. 即使视图仅限于矩形,您也可以以任何您喜欢的方式塑造活动区域。 By combining that with a transparent background, you can imitate a view of any shape that you can draw. 通过将其与透明背景相结合,您可以模拟可以绘制的任何形状的视图。

When your rectangular view receives touches and other events, your event handlers should first check if the activity has happened in the inverted-T area. 当您的矩形视图接收到触摸和其他事件时,您的事件处理程序应首先检查活动是否发生在倒T区域。 If the activity is outside, the event should be ignored. 如果活动在室外,则应忽略该事件。

Phew.. Finally I have done it. Phew ..最后我做到了。 I have used two UiViews subclasses (top & bottom) . 我使用了两个UiViews子类(顶部和底部)

The main challenge I faced was about the border, because if I set the border to my two views (top & bottom), it will not show as a single container item. 我面临的主要挑战是关于边框,因为如果我将边框设置为我的两个视图(顶部和底部),它将不会显示为单个容器项。 :) :)

Steps that I done: 我完成的步骤:

Created two UiView subclasses . 创建了两个UiView子类 Lets call topView and bottomView . 让我们致电冠捷bottomView。

    TopView *topView = [[TopView alloc] initWithFrame:CGRectMake(220, 60, 200, 200)];
    [topView setBackgroundColor:[UIColor yellowColor]]; 
    [self.view addSubview:topView]; 

    BottomView *bottomView = [[BottomView alloc] initWithFrame:CGRectMake(130, 260, 380, 200)];
    [bottomView setBackgroundColor:[UIColor yellowColor]];
    bottomView.customShape = topView; //Set the custom shape as TopView to frame to draw the border.    
    [self.view addSubview:topView];

I have drawn three borders (top,right,left) for TopView and two full borders (bottom, right), two partial borders (top left, top right) for BottomView through overriding the drawRect method. 我已经为TopView绘制了三个边框 (顶部,右边,左边)和两个完整边框 (底部,右边),通过覆盖drawRect方法为BottomView绘制了 两个部分边框 (左上角,右上角)。

See my TopView class here . 在这里查看我的TopView课程。

See my BottomView class here . 在这里查看我的BottomView类。

Thanks to all. 谢谢大家。

Output : 输出

自定义UiView

It should be possible to create a view with a CAShapeLayer as layer. 应该可以使用CAShapeLayer作为图层创建视图。

Make a subclass of UIView and override the layerClass method: 创建UIView的子类并覆盖layerClass方法:

+ (Class)layerClass {
    return [CAShapeLayer class];
}

Then in the viewDidLoad you can specify the bezierPath to the shapeLayer: 然后在viewDidLoad中,您可以为shapeLayer指定bezierPath:

- (void)viewDidLoad {
     [(CAShapeLayer *)self.layer setPath:someBezierPath.CGPath];
     [self.layer setBackgroundColor:[UIColor redColor].CGColor];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM