OC(二):加载动画
2016-08-24 本文已影响94人
IMSong
结果示例图
.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
代码地址: 点这里