iOS UIBezierAnimation曲线,如何创建某种弧?

我敢肯定,这对于以前曾经尝试过的任何人来说都是一个相当简单和直接的问题,但我对你称之为“高级”动画的新手很有帮助。

我试图通过使用CAKeyframeAnimation(使用“位置”键路径)创建对象的以下移动:

http://www.sumopaint.com/files/images800/aeegfexznpohlehd.jpg

我已经尝试用UIBezierPath设置路径,但是很快就陷入困惑和沮丧,没有找到它背后的逻辑:)

如果您对此有任何意见,我很乐意听到...

这是我的基本代码(如果有更好的想法可能会从头开始编写:P)

另外我想在完成时淡出对象。 有没有这样的动画完成的选择器? (如[UIView animateWithDuration])?

UIBezierPath *thumbPath = [UIBezierPath bezierPath];
[thumbPath moveToPoint: P(99,270)];
[thumbPath addCurveToPoint:P(164,260) controlPoint1:P(164,280) controlPoint2:P(164,280)];
[thumbPath addCurveToPoint:P(164,260) controlPoint1:P(260,310) controlPoint2:P(260,310)];

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.path = thumbPath.CGPath;
pathAnimation.duration = 2.0;

只是想分享我为此做出的最简单的方式,对于像我这样的CAAnimation中的noob的任何人来说。

我没有使用UIBezierPath,而是在屏幕(x,y)上手动编写路径的点,并使用这些路径创建路径,从而创建所需的曲线。 非常有用和简单。

希望你觉得这有帮助:

NSArray *myPoints = [NSArray arrayWithObjects: 
                             [NSValue valueWithCGPoint:P(77,287)],
                             [NSValue valueWithCGPoint:P(97,270)],
                             [NSValue valueWithCGPoint:P(112,260)],
                             [NSValue valueWithCGPoint:P(130,250)],
                             [NSValue valueWithCGPoint:P(154,245)],
                             [NSValue valueWithCGPoint:P(174,250)],
                             [NSValue valueWithCGPoint:P(193,260)],
                             [NSValue valueWithCGPoint:P(210,270)],
                             [NSValue valueWithCGPoint:P(231,287)],
                             nil];

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 77, 280);

for(NSValue *val in myPoints){
    CGPoint p = [val CGPointValue];
    CGPathAddLineToPoint(path, NULL, p.x, p.y);
}

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.path = path;
CGPathRelease(path);

实现触摸方法,在begin方法中添加一个起点

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:myImageView];
    StartDrawPoint = point;
}

现在在TouchMoved中调用draw方法

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPoint = [touch locationInView:myImageView];
    [self drawline:currentTouchPoint ToPoint:StartDrawPoint];
}

Draw方法

- (void)drawline:(CGPoint)FromPoint ToPoint:(CGPoint)ToPoint{
   UIGraphicsBeginImageContext(myImageView.frame.size);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0,1.0,1.0, 1.0);
    [imgView.image drawInRect:CGRectMake(0, 0, myImageView.frame.size.width, myImageView.frame.size.height)];
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:FromPoint];
    [path addLineToPoint:ToPoint];
    [path setLineWidth:5.0f];
    [path stroke];
    [path closePath];
    myImageView.image = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
}
链接地址: http://www.djcxy.com/p/54371.html

上一篇: iOS UIBezierAnimation curves, howto to create some sort of arc?

下一篇: Calculate age from date in textbox in C#