我正在关注创建弧的Ray Wenderlich教程:
http://www.raywenderlich.com/33193/core-graphics-tutorial-arcs-and-paths
- 我要做的是从a点开始(一个固定点)并找到用户触摸屏幕的位置,如果它是+ A点,那么我想画一条弧到那一点。虽然我没有返回任何错误,但我也没有得到抚摸的路径。有人可以查看下面的代码,看看我做错了什么?
@implementation KIP_Arc
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void) setArc {
//set the frame
float frameX = _startPoint.x;
float frameY = _startPoint.y;
float frameW = _endPoint.x;
float frameH = 50.0;
[self setFrame:CGRectMake(frameX, frameY, frameW, frameH)];
self.backgroundColor = [UIColor clearColor];
}
- (BOOL)isFlipped {
return YES;
}
- (void)drawRect:(CGRect)rect {
[[UIColor blackColor] set];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGRect arcRect = self.frame;
CGMutablePathRef arcPath = [self createArcPathFromBottomOfRect:arcRect:25.0];
CGContextAddPath(context, arcPath);
CGContextClip(context);
CGContextFillPath(context);
CGContextRestoreGState(context);
CFRelease(arcPath);
}
- (CGMutablePathRef) createArcPathFromBottomOfRect : (CGRect) rect : (CGFloat) arcHeight {
CGRect arcRect = CGRectMake(rect.origin.x, rect.origin.y + rect.size.height - arcHeight, rect.size.width, arcHeight);
CGFloat arcRadius = (arcRect.size.height/2) + (pow(arcRect.size.width, 2) / (8*arcRect.size.height));
CGPoint arcCenter = CGPointMake(arcRect.origin.x + arcRect.size.width/2, arcRect.origin.y + arcRadius);
CGFloat angle = acos(arcRect.size.width / (2*arcRadius));
CGFloat startAngle = radians(180) + angle;
CGFloat endAngle = radians(360) - angle;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, arcCenter.x, arcCenter.y, arcRadius, startAngle, endAngle, 0);
CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMinY(rect));
CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect));
return path;
}
static inline double radians (double degrees){
return degrees * M_PI/180;
}
答案 0 :(得分:1)
CGContextClip
, as a side effect, empties the current path.当您紧接着致电CGContextFillPath
时,当前路径为空,因此您什么都不填。
顾名思义,CGContextFillPath
将自己限制在上下文的当前路径中。 (如果没有,它的名字就是CGContextFill
。)所以,你不需要剪辑。
放弃CGContextClip
来电并使用CGContextFillPath
。