别问我问为什么不用MBProgressHUD和SVProgressHUD,MBProgressHUD和SVProgressHUD固然好,但不是自己做的,总有种超脱自己控制的感觉,有时候显得不够灵活,考虑到自己项目的情况做了SCProgressHUD。
SCProgressHUD.gif
SCProgressHUD结构
- 显示
+ (void)showMsg:(NSString *)msg inView:(UIView*)theView afterDelay:(NSTimeInterval)delay completed:(ShowCompleted)completedBlock
{
SCProgressHUD *alert = [[SCProgressHUD alloc] initWithMsg:msg];
if (!theView){
[[self getUnhiddenFrontWindowOfApplication] addSubview:alert];
}
else{
[[SCProgressHUD getWindow] addSubview:alert];
}
[alert showAlertWithMsg:delay];
[alert completed:completedBlock];
}
- (void)completed:(ShowCompleted)completedBlock{
self.completedBlock = completedBlock;
}
显示的动画用的CAKeyframeAnimation
CAKeyframeAnimation* scaleAnimation =[CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.duration = kDefaultDuration;
scaleAnimation.cumulative = YES;
scaleAnimation.repeatCount = 1;
scaleAnimation.removedOnCompletion = NO;
scaleAnimation.fillMode = kCAFillModeForwards;
scaleAnimation.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:self.animationTopScale],
[NSNumber numberWithFloat:1.0f],
[NSNumber numberWithFloat:1.0f],
[NSNumber numberWithFloat:1.0],
nil];
scaleAnimation.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:0.085f],
[NSNumber numberWithFloat:0.92f],
[NSNumber numberWithFloat:1.0f], nil];
scaleAnimation.timingFunctions = [NSArray arrayWithObjects:
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut], nil];
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = duration;
group.delegate = self;
group.animations = [NSArray arrayWithObjects:scaleAnimation, nil];
[self.layer addAnimation:group forKey:@"group"];
动画消失后调用回调
self.completedBlock();
[self removeFromSuperview];
- HUD隐藏
+ (BOOL)hideHUDForView:(UIView *)view
{
SCProgressHUD *hud = [self HUDForView:view];
if (hud != nil) {
UIWindow *window;
if (!view){
window = [self getUnhiddenFrontWindowOfApplication];
}
else{
window = [SCProgressHUD getWindow];
}
UIView *backgroundView = [window viewWithTag:100];
if (backgroundView) {
[backgroundView removeFromSuperview];
}
[hud hidenHUD];
return YES;
}
return NO;
}
- (void)hidenHUD{
[self.HUDActivity stopAnimating];
[self removeFromSuperview];
}
此HUD是配合我们的项目来做的,若要引用到其他项目可在此基础略作修改。
项目地址:GitHub