【网络动画效果】
众所周知,包括但不限于网络处理,很多使用APP的时机都需要展示Loading或者Toast提示的形式来提升用户的交互体验。
- 自定义Loading类:是继承UIActivityIndicatorView的子类。简化创建与管理。指定了布局与样式等。对外暴露创建与消失方法。
#import "LJZLoading.h"
@implementation LJZLoading
- (instancetype)initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style
{
self = [super initWithActivityIndicatorStyle:style];
if(self){
self.hidesWhenStopped = YES;
self.frame = CGRectMake(0, 0, 150, 150);
self.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
self.backgroundColor = [UIColor blackColor];
self.alpha = 0.5;
[[[UIApplication sharedApplication] keyWindow] addSubview:self];
[self startAnimating];
}
return self;
}
- (void)StopShowLoading
{
[self stopAnimating];
[self removeFromSuperview];
}
@end
- 自定义Toast类”LJZToast“:设计为以单例模式调用,控制内容与展示时长,在固定位置展示出提示信息。其头文件如下:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface ToastLabel : UILabel
- (void)setMessageText:(NSString *)text;
@end
@interface LJZToast : NSObject
@property (nonatomic,strong)ToastLabel *toastLabel;
+ (instancetype)shareInstance;
- (void)ShowToast:(NSString *)message duration:(CGFloat)duration;
@end
- Toast的实现上,UILabel子类的样式自定义,dispatch_once执行静态单例。dispatch_after控制展示animateWithDuration实现消失动画,具体实现如下:
#import "LJZToast.h"
@implementation ToastLabel
- (instancetype)init
{
self = [super init];
if(self){
self.layer.cornerRadius = 8;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor blackColor];
self.numberOfLines = 0;
self.textAlignment = NSTextAlignmentCenter;
self.textColor = [UIColor whiteColor];
self.font = [UIFont systemFontOfSize:15];
}
return self;
}
- (void)setMessageText:(NSString *)text
{
[self setText:text];
self.frame = CGRectMake(0, 0, 150, 50);
self.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, 150);
}
@end
@implementation LJZToast
+ (instancetype)shareInstance
{
static LJZToast *singleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singleton = [[LJZToast alloc] init];
});
return singleton