【发布时间】: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