在iOS6中拉动像邮件一样刷新动画

时间:2012-07-15 14:17:21

标签: ios core-animation pull-to-refresh

我是iOS开发人员,最近我安装了最新的iOS 6进行测试。当我遇到邮件拉动刷新动画时,我决定尝试自己实现它。我试图使用带有UIBezierPath的CAShapeLayer来创建可以向前拖动的相同椭圆形状。并且为了使向下部分向下伸展,我尝试使用添加两个控制点的曲线并应用CABasicAnimation。但不幸的是,结果并不像我预期的那样。由于拉伸的线条内部有一点椭圆形。我想椭圆动画必须与其他类做一些事情。如果有人能引导我在这里提出一些想法,我将非常感激。 非常感谢

- (UIBezierPath *)createCircleForRect:(CGRect)bRect
{
    UIBezierPath *circle = [UIBezierPath bezierPath];

    CGFloat rectWidth = bRect.size.width;
    CGFloat rectHeight = bRect.size.height;

    minSize = MIN(rectWidth, rectHeight);

    //center = CGPointMake(minSize/2.0f, minSize/2.0f);

    CGFloat radius = minSize/2.0f - 5.0f;
    startPointOffset = center.x - radius;

    if(startPointOffset == 5.0f)
        testVal = 0;
    else 
        testVal += startPointOffset;

    NSLog(@"startPointOffset =>> %f", startPointOffset);
    NSLog(@"radius =>> %f", radius);

    NSLog(@"after center =>> %f, %f", center.x, center.y);

    [circle addArcWithCenter:center radius:radius startAngle:DEGREES_TO_RADIANS(0) endAngle:DEGREES_TO_RADIANS(180) clockwise:NO];

    [circle moveToPoint:CGPointMake(startPointOffset, center.y)];

    [circle addCurveToPoint:CGPointMake(center.x + radius, center.y) controlPoint1:CGPointMake(center.x - radius, rectHeight + 10.0f) controlPoint2:CGPointMake(center.x + radius, rectHeight + 10.0f)];

    [circle closePath];
    return circle;
}

-(void)startAnimation
{   
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];

    animation.duration = 1.0;

    animation.repeatCount = HUGE_VALF;

    animation.autoreverses = YES;

    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    animation.fromValue = (id)roundBPath;

    CGRect smallerRect = self.frame;

    smallerRect.size.height += 50;
    smallerRect.size.width -= 80;


    UIBezierPath *newRound = [self createCircleForRect:smallerRect];


    animation.toValue = (id)(newRound.CGPath);


    [shapeLayer addAnimation:animation forKey:@"animatePath"];
}

1 个答案:

答案 0 :(得分:5)