在项目的开发过程中,我们经常会遇到使用动画的时候,比如我们在请求接口直接开始一个动画,请求结束后结束动画,下面我就给大家展示一个很方便的帧动画的用法:
代码如下:
.h
#import@interface hActiveView : NSObject+(void)startAnimatedInView:(UIView *)aView;+(void)stopAnimatedInView:(UIView *)aView;@end
.m
#import "hActiveView.h"@implementation hActiveView+(void)startAnimatedInView:(UIView *)aView{ UIView *topView=[UIView new]; topView.backgroundColor=[UIColor clearColor]; topView.tag=987654321; topView.userInteractionEnabled=YES; [aView addSubview:topView]; [topView mas_makeConstraints:^(MASConstraintMaker *make) { // make.edges.equalTo(aView).with.insets(UIEdgeInsetsMake(0, 44, 0, 0)); make.height.equalTo(@64); make.top.equalTo(@0); make.left.equalTo(@44); make.right.equalTo(@0); }]; UIView *backgroundView=[UIView new]; backgroundView.backgroundColor=[[UIColor whiteColor] colorWithAlphaComponent:0.7]; backgroundView.tag=987654322; backgroundView.userInteractionEnabled=YES; [aView addSubview:backgroundView]; [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(aView).with.insets(UIEdgeInsetsMake(64, 0, 0, 0)); }]; //黑色view UIView *hView=[UIView new]; hView.tag=987654323; hView.backgroundColor=[UIColor clearColor]; hView.alpha = 0.7; hView.layer.borderWidth = 1; hView.layer.borderColor = [UIColor clearColor].CGColor; hView.layer.cornerRadius = 5; [aView addSubview:hView]; [hView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(80, 80)); make.centerX.mas_equalTo(aView.mas_centerX); make.centerY.mas_equalTo(aView.mas_centerY); }]; UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)]; gifImageView.tag=987654324; NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"1_1"], [UIImage imageNamed:@"2_1"], [UIImage imageNamed:@"3_1"], [UIImage imageNamed:@"4_1"], [UIImage imageNamed:@"5_1"], [UIImage imageNamed:@"6_1"], [UIImage imageNamed:@"7_1"], [UIImage imageNamed:@"8_1"], [UIImage imageNamed:@"9_1"], [UIImage imageNamed:@"10_1"], [UIImage imageNamed:@"11_1"], [UIImage imageNamed:@"12_1"], [UIImage imageNamed:@"13_1"], [UIImage imageNamed:@"14_1"], [UIImage imageNamed:@"15_1"], [UIImage imageNamed:@"16_1"], [UIImage imageNamed:@"17_1"], [UIImage imageNamed:@"18_1"], [UIImage imageNamed:@"19_1"], [UIImage imageNamed:@"20_1"], [UIImage imageNamed:@"21_1"], [UIImage imageNamed:@"22_1"], [UIImage imageNamed:@"23_1"], [UIImage imageNamed:@"24_1"], [UIImage imageNamed:@"25_1"],nil]; gifImageView.animationImages = gifArray; //动画图片数组 gifImageView.animationDuration = 1; //执行一次完整动画所需的时长 gifImageView.animationRepeatCount = 0; //动画重复次数 [gifImageView startAnimating]; [hView addSubview:gifImageView]; }+(void)stopAnimatedInView:(UIView *)aView{ [[aView viewWithTag:987654320] removeFromSuperview]; [[aView viewWithTag:987654321] removeFromSuperview]; [[aView viewWithTag:987654322] removeFromSuperview]; [[aView viewWithTag:987654323] removeFromSuperview]; NSArray * arr = [aView subviews]; [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[UIImageView class]]) { UIImageView *active = (UIImageView *)obj; [active stopAnimating]; } }];}@end
别问我为什么不for循环添加图片,代码比较早了,最近想起来,所以在博客里面记录了一下!
在pch文件里面:
//请求动画开始#define HStart(h) [hActiveView startAnimatedInView:h];//请求动画结束#define HStop(h) [hActiveView stopAnimatedInView:h];
这样,就可以在想使用动画的地方直接使用了!