簡體   English   中英

iOS-更改四邊形曲線的控制點?

[英]iOS - Changing control points of Quad Curve?

我用以下代碼繪制了一條四邊形曲線,我還繪制了三個橢圓,一個為左角,一個為右角,一個為控制點。

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextSaveGState(context);

CGMutablePathRef curve = CGPathCreateMutable();

CGPathMoveToPoint(curve, nil, 5, 40);
CGPathAddQuadCurveToPoint(curve, nil, 30, 20, 55, 40);

CGContextAddPath(context, curve);
CGContextSetStrokeColorWithColor(context,[UIColor whiteColor].CGColor);
CGContextStrokePath(context);

CGContextSaveGState(context);

CGSize sz = self.frame.size;
CGPoint pt = CGPointMake(sz.width/2.0, sz.height/2.0);
CGFloat r = 5;

middleRect_ = CGRectMake(pt.x - r, pt.y - r, r * 2, r * 2);

CGContextAddEllipseInRect(context, middleRect_);
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextFillPath(context);

CGContextAddEllipseInRect(context, middleRect_);
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextStrokePath(context);

//left side

sz = self.frame.size;
pt = CGPointMake(sz.width/2.0, sz.height/2.0);
r = 5;

leftRect_ = CGRectMake(pt.x - 27, pt.y + 3, r * 2, r * 2);

CGContextAddEllipseInRect(context, leftRect_);
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextFillPath(context);

CGContextAddEllipseInRect(context, leftRect_);
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextStrokePath(context);

//right side
sz = self.frame.size;
pt = CGPointMake(sz.width/2.0, sz.height/2.0);
r = 5;
rightRect_ = CGRectMake(pt.x + 20, pt.y + 3, r * 2, r * 2);
CGContextAddEllipseInRect(context, rightRect_);
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextFillPath(context);

CGContextAddEllipseInRect(context, rightRect_);
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextStrokePath(context);

// Cleanup Code
CGColorSpaceRelease(colorSpace);
CGContextRestoreGState(context);

}

如您所見,我正在使用3個CGRects存儲頭寸。 現在,我希望每次觸摸並拖動橢圓時,它都會改變曲線。 例如,如果觸摸中間的橢圓,則會更改控制點;如果觸摸leftRect,則會更改左側位置;如果觸摸rightRect,則會更改右側位置。 怎么做? 我沒有這些橢圓和曲線的參考,我該怎么做? 謝謝

您需要跟蹤觸摸事件,以了解用戶何時開始拖動橢圓。 UIPanGestureRecognizer ,請使用UIPanGestureRecognizer 您還需要變量來跟蹤橢圓的位置。 每當用戶拖動橢圓時,都可以通過調用[self setNeedsDisplay]重繪所有內容。

抱歉,簡短的答案,但是從繪制四邊形曲線到允許用戶控制它還有很長的路要走。 不過,該算法非常簡單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM