2014-03-13 53 views
1

我想有一个UIElement(UIView,UIButton,UILabel等)与自定义形状可以说一个三角形。 你如何建议实现它。
在此先感谢。UIView与自定义形状

回答

2

这里是一个UIView子类(三角形)的样品:

@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 
+0

由于好友。这实际上是否将我们的观点转换为三角形?如果我将一个手势识别器添加到视图中,如果我在没有绘制三角形的矩形的空白位置上胶带,它会触发吗? –

+0

我不知道,因为我没有尝试,但你可以测试它。 –

+3

这不会将你的UIView转换成一个三角形,它只会在里面画一个三角形。如果你想定义一个自定义的点击区域,你需要重载'pointInside:withEvent'方法。 –