【发布时间】:2015-01-14 23:17:33
【问题描述】:
我有可以展开以显示一些子细节的表格单元格,我希望出现一个向下的三角形,让用户知道这是可能的。
这是创建三角形视图的代码...
-(UIView *)triangularViewWithWidth:(CGFloat)width height:(CGFloat)height pointsUp:(BOOL)pointsUp {
UIBezierPath *trianglePath = [UIBezierPath bezierPath];
if(pointsUp){
[trianglePath moveToPoint:CGPointMake(width / 2, 0)];
[trianglePath moveToPoint:CGPointMake(width, height)];
[trianglePath moveToPoint:CGPointMake(0, height)];
[trianglePath closePath];
} else {
[trianglePath moveToPoint:CGPointMake(0, 0)];
[trianglePath moveToPoint:CGPointMake(width, 0)];
[trianglePath moveToPoint:CGPointMake(width / 2, height)];
[trianglePath closePath];
}
CAShapeLayer *l = [CAShapeLayer layer];
l.path = trianglePath.CGPath;
UIView *triangleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
triangleView.layer.mask = l;
return triangleView;
}
以及实现它的代码...
-(void)drawTriangle {
if([self.detail.subDetails count]){
self.openCloseTriangle = [self triangularViewWithWidth:100 height:100 pointsUp:NO];
NSLog(@"triangle origin is: %@", NSStringFromCGPoint(self.openCloseTriangle.frame.origin));
NSLog(@"triangle size is: %@", NSStringFromCGSize(self.openCloseTriangle.frame.size));
self.openCloseTriangle.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.openCloseTriangle];
[self.contentView bringSubviewToFront:self.openCloseTriangle];
}
}
如果我注释掉这一行,代码会按预期工作(创建一个大的红色矩形):
l.path = trianglePath.CGPath;
所以我想我对形状层有些不理解,但我在我的应用程序的另一部分使用基本相同的绘图代码,它工作正常。
point 和 rect NSLog 输出也检查得很好(即使它没有出现):
三角形原点是:{0, 0} 三角形大小为:{100, 100}
【问题讨论】:
-
你在实现自己的 UITableViewCell 类吗?因为你应该这样做。
标签: ios objective-c cocoa-touch core-animation