pull to refresh animation like mail in iOS6

Bahonur Nasritdinov picture Bahonur Nasritdinov · Jul 15, 2012 · Viewed 10.9k times · Source

I am an iOS developer and recently i got installed the newest iOS 6 on testing. As i came across with mail pull to refresh animation, i decided to try to implement it on my own. I have tried to use CAShapeLayer with UIBezierPath to create the same oval shape that can be dragged forward down. And in order to get the down part stretched downwards i have tried to use adding curve with two control points and applied CABasicAnimation. But unfortunately the result is not as i expected. Since the stretched lines get a little oval inside. I suppose the oval animation has to do something with other classes. I would be very appreciative if someone could lead me to some ideas here. Thanks a lot

- (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"];
}

Answer

rickster picture rickster · Sep 23, 2012

The pull-to-refresh control in iOS 6 Mail is a standard UIKit control available to any app: see the refreshControl property on UITableViewController and the UIRefreshControl class. (Also note you can set one up in IB on XCode 4.5 by choosing "Refreshing -> Enabled" for the table view controller.)

ODRefreshControl is a good option if you're deploying to iOS 5 or older, though.