Skip to content

Commit

Permalink
new:(*) UIView loading animation
Browse files Browse the repository at this point in the history
  • Loading branch information
niyongsheng committed Feb 7, 2024
1 parent b250034 commit 87e8b78
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 29 deletions.
6 changes: 6 additions & 0 deletions Examples/Objective-C/NYSExampleObjC.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
C891E85E24B8562C00AF0019 /* UMManager.m in Sources */ = {isa = PBXBuildFile; fileRef = C891E85D24B8562C00AF0019 /* UMManager.m */; };
C891E86324B856DF00AF0019 /* UserManager.m in Sources */ = {isa = PBXBuildFile; fileRef = C891E86224B856DF00AF0019 /* UserManager.m */; };
C891E86824B85C0900AF0019 /* NYSNewfeatureViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C891E86724B85C0900AF0019 /* NYSNewfeatureViewController.m */; };
C89E827E2B733B28002BEC52 /* NYSNoticeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C89E827D2B733B28002BEC52 /* NYSNoticeViewController.m */; };
C8C431A724BC351E0041311C /* themejson_night.json in Resources */ = {isa = PBXBuildFile; fileRef = C8C431A424BC351E0041311C /* themejson_night.json */; };
C8C431A824BC351E0041311C /* themejson_day.json in Resources */ = {isa = PBXBuildFile; fileRef = C8C431A624BC351E0041311C /* themejson_day.json */; };
C8C58D2B24B94A1200CFB68F /* NYSOpenViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C8C58D2924B94A1200CFB68F /* NYSOpenViewController.m */; };
Expand Down Expand Up @@ -88,6 +89,8 @@
C891E86224B856DF00AF0019 /* UserManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = UserManager.m; sourceTree = "<group>"; };
C891E86624B85C0900AF0019 /* NYSNewfeatureViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NYSNewfeatureViewController.h; sourceTree = "<group>"; };
C891E86724B85C0900AF0019 /* NYSNewfeatureViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NYSNewfeatureViewController.m; sourceTree = "<group>"; };
C89E827C2B733B28002BEC52 /* NYSNoticeViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NYSNoticeViewController.h; sourceTree = "<group>"; };
C89E827D2B733B28002BEC52 /* NYSNoticeViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NYSNoticeViewController.m; sourceTree = "<group>"; };
C8C431A424BC351E0041311C /* themejson_night.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = themejson_night.json; sourceTree = "<group>"; };
C8C431A624BC351E0041311C /* themejson_day.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = themejson_day.json; sourceTree = "<group>"; };
C8C58D2824B94A1200CFB68F /* NYSOpenViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NYSOpenViewController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -346,6 +349,8 @@
children = (
C82DD0F52B6F48ED00FD2BD8 /* NYSHomeViewController.h */,
C82DD0F62B6F48ED00FD2BD8 /* NYSHomeViewController.m */,
C89E827C2B733B28002BEC52 /* NYSNoticeViewController.h */,
C89E827D2B733B28002BEC52 /* NYSNoticeViewController.m */,
);
path = Controller;
sourceTree = "<group>";
Expand Down Expand Up @@ -532,6 +537,7 @@
C891E85E24B8562C00AF0019 /* UMManager.m in Sources */,
C891E80624B8112200AF0019 /* NYSLoginViewController.m in Sources */,
C8C58D4A24B9647500CFB68F /* NYSNewViewController.m in Sources */,
C89E827E2B733B28002BEC52 /* NYSNoticeViewController.m in Sources */,
C8C58D5924B9B16B00CFB68F /* NYSUserInfo.m in Sources */,
C891E80224B8110400AF0019 /* NYSTabBarViewController.m in Sources */,
C8C58D5A24B9B16B00CFB68F /* NYSProfile.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "0DE69E98-788A-46A0-880E-BB5E2E04CDA6"
type = "0"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.ExceptionBreakpoint">
<BreakpointContent
uuid = "E10C3BC5-DD0A-4903-AF66-0BB71875FB2F"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
breakpointStackSelectionBehavior = "1"
scope = "1"
stopOnStyle = "0">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "NYSHomeViewController.h"
#import "NYSNoticeViewController.h"

@interface NYSHomeViewController ()

Expand All @@ -24,16 +25,11 @@ - (void)viewDidLoad {

[self.view addSubview:self.tableView];

[self initNavItem];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"notification_icon"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStyleDone target:self action:@selector(rightItemOnclicked)];
}

- (void)initNavItem {
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"notification_icon"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStyleDone target:self action:nil];
WS(weakSelf);
[rightItem setActionBlock:^(id _Nonnull sender) {
[weakSelf.navigationController pushViewController:NYSBaseViewController.new animated:YES];
}];
self.navigationItem.rightBarButtonItem = rightItem;
- (void)rightItemOnclicked {
[self.navigationController pushViewController:NYSNoticeViewController.new animated:YES];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// NYSNoticeViewController.h
// NYSExampleObjC
//
// Created by niyongsheng on 2024/2/7.
// Copyright ยฉ 2024 niyongsheng. All rights reserved.
//

#import <NYSUIKit/NYSUIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface NYSNoticeViewController : NYSBaseViewController

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// NYSNoticeViewController.m
// NYSExampleObjC
//
// Created by niyongsheng on 2024/2/7.
// Copyright ยฉ 2024 niyongsheng. All rights reserved.
//

#import "NYSNoticeViewController.h"

@interface NYSNoticeViewController ()

@end

@implementation NYSNoticeViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self.view addSubview:self.collectionView];
[UIView showLoadingWithTintColor:nil];
}

@end
3 changes: 2 additions & 1 deletion NYSUIKit/Category/UITextField+NYS.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

#import <UIKit/UIKit.h>

IB_DESIGNABLE
@interface UITextField (NYS)

/// ๆœ€ๅคงๅฏ่พ“ๅ…ฅไฝๆ•ฐ
@property (assign, nonatomic) NSInteger maxLength;
@property (assign, nonatomic) IBInspectable NSInteger maxLength;

@end

11 changes: 7 additions & 4 deletions NYSUIKit/Category/UIView+NYS.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ IB_DESIGNABLE
/// ๅœ†่ง’ๅŠๅพ„
@property (nonatomic, assign) IBInspectable CGFloat cornerRadius;

/// ๆทปๅŠ ๅŠ ่ฝฝๅŠจ็”ป
+ (UIView*)loadingAnimation;
/// ็งป้™คๅŠ ่ฝฝๅŠจ็”ป
+ (void)removeLoadingAnimation;
/// ๆ˜พ็คบๅŠ ่ฝฝๅŠจ็”ป(ๆฏ›็Žป็’ƒๆ•ˆๆžœ)
/// - Parameter tintColor: ๆŒ‡็คบๅ™จ้ขœ่‰ฒ
+ (UIView*)showLoadingWithTintColor:(UIColor *)tintColor;

/// ๆ˜พ็คบๅŠ ่ฝฝๅŠจ็”ป
/// - Parameter image: ่‡ชๅฎšไน‰ๆ—‹่ฝฌๅ›พ
+ (UIView*)showLoadingWithImage:(UIImage *)image;

@property (nonatomic, copy) NYSTapActionBlock tapActionBlock;
/// ๆทปๅŠ ็‚นๅ‡ปไบ‹ไปถ
Expand Down
87 changes: 71 additions & 16 deletions NYSUIKit/Category/UIView+NYS.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
//

#import "UIView+NYS.h"
#import "UIImage+NYS.h"
#import "NYSUIKitUtilities.h"
#import <objc/runtime.h>

#define kLinePix ( 1 / [UIScreen mainScreen].scale)

#import <objc/runtime.h>
static const char *NYSTapActionKey = "NYSTapActionKey";

@implementation UIView (NYS)
Expand Down Expand Up @@ -39,39 +42,91 @@ - (CGFloat)cornerRadius {
}

#pragma mark - Loading Animation
+ (UIView*)loadingAnimation {
+ (UIView*)showLoadingWithImage:(UIImage *)image {
UIWindow *keywindow = nil;
for (UIWindow *window in [UIApplication sharedApplication].windows) {
if (window.isKeyWindow) {
keywindow = window;
break;
}
}

CGFloat width = keywindow.frame.size.width;
CGFloat height = keywindow.frame.size.height;
UIView *animationBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
animationBg.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
UIImageView *animation = [[UIImageView alloc] init];
animation.frame = CGRectMake(0, 0, 80, 80);
animation.center = animationBg.center;
[animationBg addSubview:animation];
// NSURL *gifImageUrl = [[NSBundle mainBundle] URLForResource:@"loading_gif_plane" withExtension:@"gif"];
// [animation setImageURL:gifImageUrl];
animationBg.tag = 1000001;
[keywindow addSubview:animationBg];
return animationBg;
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.45];
[bgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeLoadingAnimation:)]];
bgView.userInteractionEnabled = YES;
bgView.alpha = 0;

UIImageView *animation = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
animation.center = CGPointMake(width / 2.0, height / 2.0);
animation.contentMode = UIViewContentModeCenter;
[animation setImage:image ? image : [[NYSUIKitUtilities imageNamed:@"waiting_icon"] imageByResizeToSize:CGSizeMake(40, 40)]];
[bgView addSubview:animation];

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotationAnimation.fromValue = [NSNumber numberWithFloat:0];
rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
rotationAnimation.duration = 1.25; // ๆ—‹่ฝฌไธ€ๅ‘จๆ‰€้œ€ๆ—ถ้—ด
rotationAnimation.repeatCount = HUGE_VALF; // ๆ— ้™ๅพช็Žฏ
[animation.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

[keywindow addSubview:bgView];
[UIView transitionWithView:bgView duration:0.75 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
bgView.alpha = 1.0;
} completion:nil];

return bgView;
}

+ (void)removeLoadingAnimation {
+ (UIView*)showLoadingWithTintColor:(UIColor *)tintColor {
UIWindow *keywindow = nil;
for (UIWindow *window in [UIApplication sharedApplication].windows) {
if (window.isKeyWindow) {
keywindow = window;
break;
}
}
UIView *animationBg = [keywindow viewWithTag:1000001];
[animationBg removeFromSuperview];

CGFloat width = keywindow.frame.size.width;
CGFloat height = keywindow.frame.size.height;
UIBlurEffectStyle blurEffectStyle = UIBlurEffectStyleLight;
UIColor *color = UIColor.grayColor;
if (@available(iOS 13.0, *)) {
if (UITraitCollection.currentTraitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
blurEffectStyle = UIBlurEffectStyleDark;
color = UIColor.whiteColor;
} else {
blurEffectStyle = UIBlurEffectStyleLight;
}
}
UIVisualEffectView *bgView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:blurEffectStyle]];
bgView.frame = CGRectMake(0, 0, width, height);
[bgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeLoadingAnimation:)]];
bgView.userInteractionEnabled = YES;
bgView.alpha = 0;

UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleLarge];
activityIndicator.color = tintColor ? tintColor : color;
activityIndicator.center = CGPointMake(width / 2.0, height / 2.0);
[activityIndicator startAnimating];
[bgView.contentView addSubview:activityIndicator];

[keywindow addSubview:bgView];
[UIView transitionWithView:bgView duration:0.75 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
bgView.alpha = 1.0;
} completion:nil];

return bgView;
}

+ (void)removeLoadingAnimation:(UITapGestureRecognizer *)gesture {
[UIView animateWithDuration:0.75 animations:^{
gesture.view.alpha = 0.0;
} completion:^(BOOL finished) {
[gesture.view removeFromSuperview];
}];
}

#pragma mark - Tap Action
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ NYSUIKit
โ”‚ โ”œโ”€ NYSUIKit.bundle
โ”‚ โ”‚ โ”œโ”€ Info.plist
โ”‚ โ”‚ โ”œโ”€ en.lproj
โ”‚ โ”‚ โ”œโ”€ es.lproj
โ”‚ โ”‚ โ”œโ”€ ja.lproj
โ”‚ โ”‚ โ”œโ”€ ko.lproj
โ”‚ โ”‚ โ”œโ”€ lao.lproj
โ”‚ โ”‚ โ”œโ”€ zh-Hans.lproj
โ”‚ โ”‚ โ””โ”€ zh-Hant.lproj
Expand Down

0 comments on commit 87e8b78

Please sign in to comment.