OC(二):加载动画

结果示例图

.h

//
//  gifView.h
//  GifPicture
//
//  Created by HMC on 16/8/23.
//  Copyright © 2016年 SKing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface gifView : UIView
//外圈
@property (weak, nonatomic) IBOutlet UIImageView *cycle;
// logo 图标
@property (weak, nonatomic) IBOutlet UIImageView *logo;
//显示的message
@property (weak, nonatomic) IBOutlet UILabel *messageLabel;

+ (instancetype)initInstance;

/**
 *  动画
 *
 *  @param message          加载的文字
 *  @param isCycleAnimation 圆是否有动画
 *  @param isLogoAnimation  logo 是否有动画
 */
-(void)startAnimationWithMessage:(NSString *) message isCycleAnimation:(BOOL) isCycleAnimation isLogoAnimation:(BOOL)isLogoAnimation;

@end

.m

//
//  gifView.m
//  GifPicture
//
//  Created by HMC on 16/8/23.
//  Copyright © 2016年 SKing. All rights reserved.
//

#import "gifView.h"

@implementation gifView



+ (instancetype)initInstance {
    
    
    return [[[NSBundle mainBundle] loadNibNamed:@"gifView" owner:nil options:nil] lastObject];
}

-(void)startAnimationWithMessage:(NSString *) message isCycleAnimation:(BOOL) isCycleAnimation isLogoAnimation:(BOOL)isLogoAnimation  {
    if(message != nil ){
        
        self.messageLabel.text = message;
        
    }
    
    if(isCycleAnimation){
        CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
        rotationAnimation.duration = 1.5;
        rotationAnimation.cumulative = YES;
        rotationAnimation.repeatCount = MAXFLOAT;
//      切换后台再回来动画不停止
        rotationAnimation.removedOnCompletion = NO;
        [self.cycle.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }
    
    if(isLogoAnimation){
        
        CABasicAnimation * transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
        transformAnimation.removedOnCompletion = NO;
        transformAnimation.duration = 3.0;
        transformAnimation.toValue = @(0);
        transformAnimation.fromValue = @(M_PI);
        transformAnimation.repeatCount = MAXFLOAT;
        [self.logo.layer addAnimation:transformAnimation forKey:@"transformAnimation"];
        

        CABasicAnimation * transformAnimation1 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
        transformAnimation1.removedOnCompletion = NO;
        transformAnimation1.duration = 3.0;
        transformAnimation1.fromValue = @(-M_PI);
        transformAnimation1.toValue = @(0);
        transformAnimation1.repeatCount = MAXFLOAT;
        [self.logo.layer addAnimation:transformAnimation1 forKey:@"transformAnimation1"];

        
    }
}



@end

调用

//
//  ViewController.m
//  GifPicture
//
//  Created by HMC on 16/8/23.
//  Copyright © 2016年 SKing. All rights reserved.
//

#import "ViewController.h"
#import "gifView.h"

@interface ViewController ()

@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    gifView * animateView = [gifView initInstance];
    animateView.center = self.view.center;
    [animateView startAnimationWithMessage:@"跟我一起转圈圈,哈哈" isCycleAnimation:YES isLogoAnimation:YES];
    [self.view addSubview:animateView];
}


@end

代码地址: 点这里

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,239评论 25 709
  • ## 可重入函数 ### 可重入性的理解 若一个程序或子程序可以安全的被并行执行,则称其为可重入的;即当该子程序正...
    夏至亦韵阅读 4,101评论 0 0
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 5,916评论 0 9
  • 之前武志红推荐过这本书《不要用爱控制我》。最近几天才开始看。书里的内容挺有启发性的,但是语句有些生硬,句子间的逻辑...
    BelovedNutan阅读 4,019评论 0 3
  • 我们大学有一对同学成为了夫妻,男的又帅又优秀,女的高挑美丽。 我们都觉得天生一对,对他们充满了羡慕嫉妒恨。 结婚后...
    6c676d940028阅读 2,481评论 0 0

友情链接更多精彩内容