具有自定义形状的UIView [英] UIView with custom shape
本文介绍了具有自定义形状的UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想拥有一个具有自定义形状的UIElement(UIView,UIButton,UILabel等中的任何一个),可以说一个三角形。
您如何建议实现它。
预先感谢。
I want to have a UIElement (any of UIView, UIButton, UILabel etc.) with custom shape lets say a triangle.
How do you suggest to achieve it.
Thanks in advance.
推荐答案
UIView子类(三角形)的示例:
Here is a sample of a UIView subclass (triangle):
@implementation TriangleView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextBeginPath(ctx);
CGContextMoveToPoint (ctx, CGRectGetMinX(rect), CGRectGetMaxY(rect)); // top left
CGContextAddLineToPoint(ctx, CGRectGetMidX(rect), CGRectGetMinY(rect)); // mid right
CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), CGRectGetMaxY(rect)); // bottom left
CGContextClosePath(ctx);
CGContextSetRGBFillColor(ctx, 241/255.0, 241/255.0, 241/255.0, 1);
CGContextFillPath(ctx);
}
@end
这篇关于具有自定义形状的UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文