I have to create a custom shaped (inverted T) bordered Uiview on iOS. I am attaching the screenshot below. I have researched a lot and I found a way using UIBezierPath from here.
我必须在iOS上创建一个自定义的(反转T)边界Uiview。我附上下面的截图。我研究了很多,找到了使用UIBezierPath的方法。
But I didn't get any idea to shape my view as inverted T shaped.
但是我没有任何想法来改变我的观点。
3 个解决方案
#1
6
UIView
s are always rectangular. From the documentation:
ui视图总是矩形。从文档:
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. If the activity is outside, the event should be ignored.
当矩形视图接收触摸和其他事件时,事件处理程序应该首先检查活动是否发生在反转t区域。如果活动在外部,则应该忽略该事件。
#2
4
Phew.. Finally I have done it. 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. Lets call topView and bottomView.
创建了两个UiView子类。让我们调用topView和底部视图。
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方法为bottom view绘制了两个部分边框(顶部、左、右)。
See my TopView class here.
请参见我的TopView类。
See my BottomView class here.
看看我的底部视图类。
Thanks to all.
感谢所有。
Output:
输出:
#3
3
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];
}
#1
6
UIView
s are always rectangular. From the documentation:
ui视图总是矩形。从文档:
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. If the activity is outside, the event should be ignored.
当矩形视图接收触摸和其他事件时,事件处理程序应该首先检查活动是否发生在反转t区域。如果活动在外部,则应该忽略该事件。
#2
4
Phew.. Finally I have done it. 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. Lets call topView and bottomView.
创建了两个UiView子类。让我们调用topView和底部视图。
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方法为bottom view绘制了两个部分边框(顶部、左、右)。
See my TopView class here.
请参见我的TopView类。
See my BottomView class here.
看看我的底部视图类。
Thanks to all.
感谢所有。
Output:
输出:
#3
3
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];
}