0
我下面就產生電弧射線Wenderlich教程中創建和中風弧:的Objective-C與CG
http://www.raywenderlich.com/33193/core-graphics-tutorial-arcs-and-paths
- 什麼我希望做的是,在開始點(固定點)並找到用戶觸摸屏幕的位置,如果它是+點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;
}
你想中風或補充?您展示的代碼只會嘗試填充路徑,而不是描述它。即使刪除「CGContextClip」調用也不會使中風發生。 (並說出你可以刪除的東西:如果恢復後你不打算做任何事情,則不需要'gsave'和'grestore'。) –
你可能還想在'setArc中設置'[self setNeedsDisplay]' ',除非你已經將視圖的'contentMode'設置爲'UIViewContentModeRedraw'。 –