文章目錄
  1. 1. bikeRegreshConrtol.h
  2. 2. bikeRegreshConrtol.m
  3. 3. 使用
  4. 4. 更新結束

##裡面用到了幾個技示巧

1。 旋轉的animation
2。 移動的animation
3。 移除不必要的UIActivityIndicator

bikeRegreshConrtol.h

#import <UIKit/UIKit.h>

@interface bikeRegreshConrtol : UIRefreshControl


-(void)makeAnimate;

@end

bikeRegreshConrtol.m

#import “bikeRegreshConrtol.h”

@implementation bikeRegreshConrtol

-(instancetype)init{

    self = [super init];
    if (self) {

    }


    return  self;

}

-(void)makeAnimate{

    for (UIView *V in self.subviews) {
        if ([[V class] isSubclassOfClass:[UIImageView class]]) {
            NSLog(@"%@",[V class]);
        }else{
            [V removeFromSuperview];
        }
    }

    UIImageView *wheel1 = [[UIImageView alloc] initWithImage:
                           [UIImage imageNamed: @"wheel.png"]];

    [wheel1 setFrame:CGRectMake(50, self.center.y, 30, 30)];


    UIImageView *wheel2 = [[UIImageView alloc] initWithImage:
                           [UIImage imageNamed: @"wheel.png"]];

    [wheel2 setFrame:CGRectMake(90, self.center.y, 30, 30)];


    if ([wheel1.layer animationForKey:@"SpinAnimation"] == nil) {
        CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        animation.fromValue = [NSNumber numberWithFloat:0.0f];
        animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
        animation.duration = 2.4f;
        animation.repeatCount = INFINITY;
        [wheel1.layer addAnimation:animation forKey:@"SpinAnimation"];
    }

    if ([wheel2.layer animationForKey:@"SpinAnimation"] == nil) {
        CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        animation.fromValue = [NSNumber numberWithFloat:0.0f];
        animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
        animation.duration = 2.2f;
        animation.repeatCount = INFINITY;
        [wheel2.layer addAnimation:animation forKey:@"SpinAnimation"];
    }

    [self insertSubview:wheel1 atIndex:3];
    [self insertSubview:wheel2 atIndex:2];

    UIImageView *back1 = [[UIImageView alloc] initWithImage:
                           [UIImage imageNamed: @"background.png"]];

    [back1 setFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];

    [self insertSubview:back1 atIndex:1];



    UIImageView *back2 = [[UIImageView alloc] initWithImage:
                          [UIImage imageNamed: @"background.png"]];

    [back2 setFrame:CGRectMake(320, 0, self.bounds.size.width, self.bounds.size.height)];

    [self insertSubview:back2 atIndex:0];

    [self bringSubviewToFront:wheel1];
    [self bringSubviewToFront:wheel2];
//    

    [self doAnimate:back1 :back2];


}

-(void)doAnimate:(UIImageView *)img :(UIImageView *)img2{
    [UIView animateWithDuration:32.0f animations:^{
        [img setFrame:CGRectMake(-320, 0, img.frame.size.width, img.frame.size.height)];
        [img2 setFrame:CGRectMake(0, 0, img.frame.size.width, img.frame.size.height)];
    } completion:^(BOOL finished) {
        [img setFrame:CGRectMake(0, 0, img.frame.size.width, img.frame.size.height)];
        [img2 setFrame:CGRectMake(320, 0, img.frame.size.width, img.frame.size.height)];
        [self doAnimate:img :img2];
    }];
}

使用

refreshControl = [[bikeRegreshConrtol alloc] init];
[refreshControl addTarget:self action:@selector(refresh)
              forControlEvents:UIControlEventValueChanged];
[mTable addSubview:refreshControl];

[refreshControl makeAnimate];

更新結束

[refreshControl endRefreshing];
文章目錄
  1. 1. bikeRegreshConrtol.h
  2. 2. bikeRegreshConrtol.m
  3. 3. 使用
  4. 4. 更新結束