【问题标题】:Circle center of arc created by CGPathAddArc has inconsistent coordinatesCGPathAddArc 创建的圆心坐标不一致
【发布时间】:2014-01-12 03:41:07
【问题描述】:

我有一个自定义 UIControl,如下所示:

白环在drawRect中绘制如下:

//Get current context
CGContextRef mainContext = UIGraphicsGetCurrentContext();

/** Draw the Path **/

//Create the path
CGContextAddArc(mainContext, self.frame.size.width/2, self.frame.size.height/2, radius, 0, 2*M_PI, 0);

//Set the stroke color to white
[[UIColor whiteColor]setStroke];

//Define line width and cap
CGContextSetLineWidth(mainContext, HOUR_PICKER_BACKGROUND_WIDTH);
CGContextSetLineCap(mainContext, kCGLineCapButt);

//Draw the path
CGContextDrawPath(mainContext, kCGPathStroke);

黑色圆圈为CAShapeLayer,绘制如下:

hourSelector = [CAShapeLayer layer];
hourSelector.path = [UIBezierPath bezierPathWithRoundedRect:selectorPosition cornerRadius:11.0].CGPath;
hourSelector.fillColor = [UIColor colorWithRed:65.0f/255.0f green:75.0f/255.0f blue:86.0f/255.0f alpha:1.0f].CGColor;
hourSelector.strokeColor = [UIColor colorWithRed:65.0f/255.0f green:75.0f/255.0f blue:86.0f/255.0f alpha:1.0f].CGColor;
hourSelector.lineWidth = 1;
[self.layer addSublayer:hourSelector];

我已经能够实现在白环上任意位置拖动黑圈。当我放开黑色圆圈时,我希望能够将黑色圆圈动画到环上的某个位置。当手指从圆圈中抬起时,我得到了结束触摸位置的弧度值以及圆圈的所需最终位置。我也有矩形/笛卡尔坐标中的点。我正在尝试通过在两点之间绘制弧线并使用CAKeyFrameAnimation 更改圆弧的位置来实现所需的行为。

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 1.0;


CGMutablePathRef arcPath = CGPathCreateMutable();
CGPathAddArc(arcPath, NULL, self.frame.size.width/2, self.frame.size.height/2, radius, endingTouchRadian, finalPositionRadian,0);

pathAnimation.path = arcPath;
CGPathRelease(arcPath);

[hourSelector addAnimation:pathAnimation forKey:@"moveHour"];

我的问题是创建的弧在白环上无处可寻。 self.frame.size.height(和相应的宽度)与用于创建白色环的圆心相同,但圆弧是在屏幕外创建的。此外,对 x,y 使用 0,0 不会返回框架的左上角,而是返回主窗口的确切中心。每次旋转圆放手时,圆弧的中心似乎也发生了变化。

我是否错过了如何使创建的弧的中心与白色圆圈的中心相同?

【问题讨论】:

  • 您从哪个方法调用CAKeyframeAnimation 并创建CGPath
  • 来自-(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event。我认为问题在于我不是从drawRect 打来的。我希望从我抬起手指的位置到戒指上的不同点创建路径。

标签: ios objective-c cgpath


【解决方案1】:

我想通了。我在 drawRect 之外创建了 CGPath,它用不同的原点重绘了视图。我不明白下面到底发生了什么,但将 CGPath 创建代码移动到 drawRect 解决了这个问题。感谢@Unheilig 用他的评论为我指明了正确的方向。

【讨论】:

    【解决方案2】:

    如果你的圈子不是中心

    CGRectGetMidX(selectorPosition), CGRectGetMidY(selectorPosition)
    

    您使用的是视图宽度和高度的一半,而忽略了它的原点。

    【讨论】:

    • selectorPosition 存储了黑圈的最终位置。我需要创建一个以白环的中心为中心的弧。我可以获得坐标,但将其传递给 CGPathAddArc 似乎不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多