当前位置:网站首页>自定义Loading动画
自定义Loading动画
2022-07-13 17:47:00 【Lu_Ca】
自定义loading
.h文件中有两个类方法,显示以及移除
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface LCSLoading : UIView
+ (void)showInView:(UIView *)parentView;
+ (void)dismissFromView:(UIView *)parentView;
@end
NS_ASSUME_NONNULL_END
.m里是方法的实现
@implementation LCSLoading
+ (void)showInView:(UIView *)parentView {
CGFloat parentWidth = parentView.frame.size.width;
CGFloat parentHeight = parentView.frame.size.height;
//防止多个loading叠加在一起
for (LCSLoading *view in parentView.subviews) {
if([view isMemberOfClass:[LCSLoading class]]) return;
}
//创建hud
LCSLoading *hudView = [[LCSLoading alloc] initWithFrame:CGRectMake(0, 0, parentWidth, parentHeight)];
[hudView setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.1]];
[parentView addSubview:hudView];
//添加旋转图片背景
CGFloat contentWidth = 80, contentHeight = 80;
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, contentWidth, contentHeight)];
[contentView setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.8]];
contentView.center = CGPointMake(parentWidth/2, parentHeight/2);
contentView.layer.cornerRadius = 10;
[hudView addSubview:contentView];
//添加旋转图片
UIImageView *loadingImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
loadingImageView.center = CGPointMake(contentView.frame.size.width/2, contentView.frame.size.height/2);
loadingImageView.image = [UIImage imageNamed:@"punch_loading"];
[contentView addSubview:loadingImageView];
//添加旋转动画
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0, 0.0, 1.0)];
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animation.duration = 1;
animation.cumulative = YES;
animation.repeatCount = MAXFLOAT;
//在图片边缘添加透明区域,去掉图片锯齿
CGRect imageRect = CGRectMake(0, 0, loadingImageView.frame.size.width, loadingImageView.frame.size.height);
UIGraphicsBeginImageContext(imageRect.size);
[loadingImageView.image drawInRect:CGRectMake(1, 1, loadingImageView.frame.size.width-2, loadingImageView.frame.size.height-2)];
loadingImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//给图片添加动画
[loadingImageView.layer addAnimation:animation forKey:nil];
}
+ (void)dismissFromView:(UIView *)parentView {
for (LCSLoading *view in parentView.subviews) {
if([view isMemberOfClass:[LCSLoading class]]) {
[UIView animateWithDuration:0.5 animations:^{
view.alpha = 0.01;
} completion:^(BOOL finished) {
[view removeFromSuperview];
}];
}
}
}
/* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */
@end
接下来是具体调用,显示后5s移除
[LCSLoading showInView:self.view];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[LCSLoading dismissFromView:self.view];
});
边栏推荐
- PLSQL problem solving ora-12154: tns: unable to resolve the specified connection identifier
- Convert list data to tree data
- Operating system notes - Windows emergency response and security hardening
- Expanding knowledge -- hijacking technology of JS
- Hand to hand teaching - using native POI to import and export excel and the basic operations of Alibaba's easyexcel
- Analysis, capture and simulation attack of Sniffer Pro on ARP Protocol
- 密态数字经济:用密码去“深海淘珍珠”
- Graphic and image programming practice course report
- Open source internship | compiler sig internship task is officially released. Welcome to apply!
- 37.js -- member operation of object and operation of prototype object (mainly related program examples)
猜你喜欢

SNMP started

MSF infiltrates Win2003 with eternal blue

Network security emergency response - common tools

Intranet penetration notes - Registry self startup and MSI rights lifting

Nftscan Developer Platform launches Multi Chain NFT data pro API service

How to solve the "Impossible Triangle" problem of data flow?

How is the advertising model with 100 billion parameters refined?

内网渗透笔记——msf

MySQL master-slave server configuration experiment centos7
![[ASIS 2019]Unicorn shop](/img/e2/c295a83e03eeaa720a631f6d736eb2.png)
[ASIS 2019]Unicorn shop
随机推荐
[wustctf2020] plain
Buuctf nine links
Log blacklist can really save you money!
Es6--string (string)
Php+jmeter simulates high concurrency scenarios. Code attached.
Convert bufferedimage into byte[] array, and the pro test is available
编译原理-词法分析器设计
Analysis, capture and simulation attack of Sniffer Pro on ARP Protocol
MySQL索引简介 - InnoDB和MyISAM索引模型
Euler talk | developer community experience bureau starts at about 19:30 on July 14
Convert list data to tree data
2. Memory leak and memory overflow
解读AFNetworking4.0请求原理
Intranet penetration notes - Registry self startup and MSI rights lifting
MSF Foundation
Find a value of tree data
什么是慢查询?如何优化?
[Huang ah code] redis realizes fuzzy query and deletes | redis obtains the key according to the prefix
7. Cache breakdown, cache penetration, cache
JS time object