使用bezierPathWithArcCenter为UIViews设置动画

时间:2014-03-18 05:08:46

标签: ios iphone objective-c uiview core-animation

我在bezier路径上制作了两个UView动画:

UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:160 startAngle:5.008935 endAngle:0.217025 clockwise:YES];
UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:160 startAngle:5.008935 endAngle:0.588981 clockwise:YES];

5.008935 rad = 286.990814 degrees
0.217025 rad = 12.434639 degrees
0.588981 rad = 33.746109 degrees

- (CAAnimationGroup *)animationForItem:(QuadCurveMenuItem *)item
{
CGFloat startAngle = DEGREES_2_RADIANS([Utilities pointPairToBearingDegrees:item.centerPoint secondPoint:item.startPoint]);
CGFloat endAngle = DEGREES_2_RADIANS([Utilities pointPairToBearingDegrees:item.centerPoint secondPoint:item.endPoint]);

UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:item.mainPoint radius:kQuadCurveMenuDefaultEndRadius startAngle:startAngle endAngle:endAngle clockwise:YES];

CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
positionAnimation.duration = self.duration;
positionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[positionAnimation setFillMode:kCAFillModeForwards];
CGMutablePathRef path = CGPathCreateMutable();
positionAnimation.path = bezierPath.CGPath;
CGPathRelease(path);

CAAnimationGroup *animationgroup = [CAAnimationGroup animation];
animationgroup.animations = [NSArray arrayWithObjects:positionAnimation, nil];
animationgroup.duration = self.duration;
[animationgroup setFillMode:kCAFillModeForwards];
animationgroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animationgroup.removedOnCompletion = NO;

return animationgroup;
}

- (void)performExpandMenuAnimated:(BOOL)animated duration:(float)duration
{

    NSArray *itemToBeAnimated = [self allMenuItemsBeingDisplayed];

    id<QuadCurveAnimation> animation = self.noAnimation;
    if (animated) { animation = self.expandItemAnimation; }

    [animation setDuration:duration];

    for (int x = 0; x < [itemToBeAnimated count]; x++) {
        QuadCurveMenuItem *item = [itemToBeAnimated objectAtIndex:x];
        NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:item,@"menuItem",animation,@"animation", nil];

        [self performSelector:@selector(animateMenuItemToEndPoint:) withObject:dictionary afterDelay:0];
    }
}

- (void)animateMenuItemToEndPoint:(NSDictionary *)itemAndAnimation {
    id<QuadCurveAnimation> animation = [itemAndAnimation objectForKey:@"animation"];
    QuadCurveMenuItem *item = [itemAndAnimation objectForKey:@"menuItem"];

    CAAnimationGroup *expandAnimation = [animation animationForItem:item];

    [item.layer addAnimation:expandAnimation forKey:[animation animationName]];
    item.center = item.endPoint;
}

所有视图都有相同的动画。唯一的区别是终点角度(endAngle)。

在path2上查看比在path1上查看更快的动画。为什么会这样?

我注意到只有当endAngle和startAngle之间的差异大于90度时才会发生这种情况(本例中为案例视图2)。

查看视频:https://dl.dropboxusercontent.com/u/24732347/video.mov

2 个答案:

答案 0 :(得分:0)

bezierPathWithArcCenter:radius:startAngle:endAngle:clockwise:文档中查看图1:对于&#34;饼图&#34;在右上角你必须使用参数

startAngle:degreesToRadians(-90)endAngle:0顺时针:是 (原因是iOS上的默认坐标系有一个指向右边的x轴,一个指向下方的y轴。)

希望这些信息对您有所帮助。

答案 1 :(得分:0)

您需要提供有关您正在做的事情的更完整描述。我猜你是否正在使用2条路径作为动画的路径属性来创建CAKeyframeAnimation?

您是说当使用这些路径为2个不同的视图设置动画时,使用path2动画制作的视图比使用path1动画制作的视图更多的点/秒传播?

在动画持续时间内,两个动画是否都在弧线的整个长度上移动?

理所当然地,沿着较长路径的动画将以点/秒的速度比较短的弧线上的动画移动得更快,因为它必须在同一时间内移动更远的距离。