RootViewController.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // RootViewController.m
  3. // XenonSDK
  4. //
  5. // Created by SAGESSE on 2019/5/29.
  6. // Copyright © 2019 SAGESSE. All rights reserved.
  7. //
  8. #import "XenonSDK.h"
  9. #import "XSUtils.h"
  10. #import "RootViewController.h"
  11. #import "NavigationAnimator.h"
  12. #import "HelperViewController.h"
  13. #import "UserLoginPhoneViewController.h"
  14. #import "UserLoginAccountViewController.h"
  15. #import "UserRegisterAccountViewController.h"
  16. #import "UserProtocolViewController.h"
  17. #import "UserRestPasswordViewController.h"
  18. #import "UserRestPassword2ViewController.h"
  19. @interface RootViewController () <UINavigationControllerDelegate>
  20. @property (nonatomic, strong) id keybardInfo;
  21. @end
  22. @implementation RootViewController
  23. - (instancetype)initWithRootViewController:(UIViewController*)rootViewController {
  24. self = [self initWithNibName:nil bundle:XenonSDK.sharedSDK.bundle];
  25. self.embedViewController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
  26. self.modalPresentationStyle = UIModalPresentationOverFullScreen;
  27. self.embedViewController.delegate = self;
  28. return self;
  29. }
  30. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  31. return [super initWithNibName:@"RootViewController" bundle:nibBundleOrNil];
  32. }
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. // Do any additional setup after loading the view.
  36. //
  37. self.view.backgroundColor = UIColor.clearColor;
  38. [self.view addSubview:self.embedView];
  39. [self viewWillLayoutSubviews];
  40. // Update font size dynamically.
  41. if (self.titleLabel != nil) {
  42. self.titleLabel.font = [self.titleLabel.font fontWithSize:CGFloatBasedI414(26)];
  43. // foce load view.
  44. (void)self.embedViewController.topViewController.view;
  45. // self.titleLabel.text = (self.embedViewController.topViewController as? BaseViewController)?.topTitle
  46. }
  47. if (self.theme == nil) {
  48. UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:self.view.bounds];
  49. toolbar.barStyle = UIBarStyleDefault;
  50. toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  51. toolbar.userInteractionEnabled = false;
  52. //toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)
  53. [self.embedView insertSubview:toolbar atIndex:0];
  54. }
  55. //self.embedController.isNavigationBarHidden = true
  56. self.embedViewController.view.frame = self.embedView.bounds;
  57. self.embedViewController.view.backgroundColor = UIColor.clearColor;
  58. self.embedViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  59. // self.embedViewController.navigationBar.isTranslucent = true
  60. // self.embedViewController.navigationBar.setBackgroundImage(.init(), for: .default)
  61. // self.embedViewController.navigationBar.shadowImage = .init()
  62. self.embedViewController.navigationBarHidden = true;
  63. [self addChildViewController:self.embedViewController];
  64. [self.embedView addSubview:self.embedViewController.view];
  65. }
  66. - (void)viewWillAppear:(BOOL)animated {
  67. [super viewWillAppear:animated];
  68. [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  69. [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  70. [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(textFieldWillEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
  71. }
  72. - (void)viewDidDisappear:(BOOL)animated {
  73. [super viewDidDisappear:animated];
  74. [NSNotificationCenter.defaultCenter removeObserver:self];
  75. }
  76. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
  77. return [[NavigationAnimator alloc] initWithOperation:operation titleView:_titleLabel];
  78. }
  79. // MARK: -
  80. - (void)textFieldWillEditing:(id)sender {
  81. if (self.keybardInfo != nil) {
  82. [self keyboardWillShow:self.keybardInfo];
  83. }
  84. }
  85. // Get current firset responder in ui hierarchy.
  86. - (id)firstResponderInResponder:(id)responder {
  87. if ([responder isFirstResponder]) {
  88. return responder;
  89. }
  90. if ([responder isKindOfClass:UIViewController.class]) {
  91. return [self firstResponderInResponder:[responder view]];
  92. }
  93. if ([responder isKindOfClass:UIView.class]) {
  94. for (UIView* view in [responder subviews]) {
  95. id r = [self firstResponderInResponder:view];
  96. if (r != nil) {
  97. return r;
  98. }
  99. }
  100. }
  101. return nil;
  102. }
  103. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  104. [super touchesEnded:touches withEvent:event];
  105. // Resign all first responder.
  106. [self.view endEditing:YES];
  107. }
  108. /// When show keyboard add view animation.
  109. - (void)keyboardWillShow:(NSNotification*)sender {
  110. // Gets the UITextField in first responder.
  111. NSDictionary* userInfo = sender.userInfo;
  112. UITextField* firstResponder = (id)[self firstResponderInResponder:self.view];
  113. if (userInfo == nil || firstResponder == nil) {
  114. return;
  115. }
  116. CGRect textField = [firstResponder convertRect:firstResponder.bounds toView:self.view];
  117. // Get animation duration.
  118. NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  119. CGRect frame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  120. CGFloat offset = fmin((CGRectGetMinY(frame) - textField.size.height) / 2 - CGRectGetMinY(textField), 0);
  121. // Make animation.
  122. [UIView animateWithDuration:duration animations:^{
  123. UIViewAnimationCurve type = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];;
  124. [UIView setAnimationCurve:type];
  125. self.view.transform = CGAffineTransformMakeTranslation(0, offset);
  126. }];
  127. // Save keyboard info.
  128. self.keybardInfo = sender;
  129. }
  130. /// When hide keyboard add view animation.
  131. - (void)keyboardWillHide:(NSNotification*)sender {
  132. // Get animation duration.
  133. NSDictionary* userInfo = sender.userInfo;
  134. NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  135. [UIView animateWithDuration:duration animations:^{
  136. UIViewAnimationCurve type = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];;
  137. [UIView setAnimationCurve:type];
  138. self.view.transform = CGAffineTransformIdentity;
  139. }];
  140. // Clean keybard info.
  141. self.keybardInfo = nil;
  142. }
  143. @end
  144. @implementation UIViewController (XenonSDK)
  145. - (void)sdk_userLoginForFast:(id)sender {
  146. // Resign editing.
  147. [self.view endEditing:YES];
  148. [XSNetwork showHudLoading];
  149. [XSNetwork loginWithComplete:^(id object, NSError *error) {
  150. if (error != nil) {
  151. [XSNetwork showHudFailure:error];
  152. return;
  153. }
  154. if (XenonSDK.sharedSDK.sdk_loginCallback) {
  155. XenonSDK.sharedSDK.sdk_loginCallback(object);
  156. }
  157. }];
  158. }
  159. - (void)sdk_dismissViewController:(id)sender {
  160. if (self.navigationController.viewControllers.count <= 1) {
  161. [self dismissViewControllerAnimated:YES completion:nil];
  162. return;
  163. }
  164. [self.navigationController popViewControllerAnimated:YES];
  165. }
  166. - (void)sdk_showViewControllerForPhoneLogin:(id)sender {
  167. [self.view endEditing:YES];
  168. [self.navigationController pushViewController:UserLoginPhoneViewController.new animated:YES];
  169. }
  170. - (void)sdk_showViewControllerForAccountLogin:(id)sender {
  171. [self.view endEditing:YES];
  172. [self.navigationController pushViewController:UserLoginAccountViewController.new animated:YES];
  173. }
  174. - (void)sdk_showViewControllerForUserRegister:(id)sender {
  175. [self.view endEditing:YES];
  176. [self.navigationController pushViewController:UserRegisterAccountViewController.new animated:YES];
  177. }
  178. // MARK: -
  179. - (void)sdk_showViewControllerForForgetPasswod:(id)sender {
  180. [self.view endEditing:YES];
  181. [self.navigationController pushViewController:UserRestPasswordViewController.new animated:YES];
  182. }
  183. - (void)sdk_showViewControllerForResetPasswod:(id)sender {
  184. [self.view endEditing:YES];
  185. [self.navigationController pushViewController:UserRestPassword2ViewController.new animated:YES];
  186. }
  187. // MARK: -
  188. - (void)sdk_showViewControllerForUserProtocol:(id)sender {
  189. [self.view endEditing:YES];
  190. [self.navigationController pushViewController:UserProtocolViewController.new animated:YES];
  191. }
  192. // @IBAction func sdk_showViewController(forUserCenter sender: Any) {
  193. // self.view.endEditing(true)
  194. //
  195. // let viewController = HelperViewController()
  196. // self.navigationController?.pushViewController(viewController, animated: true)
  197. // }
  198. //
  199. //
  200. - (void)sdk_showViewControllerForCustomerService:(id)sender {
  201. [self.view endEditing:YES];
  202. [self.navigationController pushViewController:HelperViewController.new animated:YES];
  203. }
  204. @end
  205. @implementation UITextField (XenonSDK)
  206. - (void)setPlaceholderColor:(UIColor*)color {
  207. if (color == nil) {
  208. self.placeholder = self.placeholder;
  209. return;
  210. }
  211. if (self.placeholder == nil) {
  212. return;
  213. }
  214. id mas = [[NSMutableAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName: color}];
  215. self.attributedPlaceholder = mas;
  216. }
  217. - (UIColor*)placeholderColor {
  218. return nil;
  219. }
  220. @end
  221. @implementation CALayer (XenonSDK)
  222. - (void)setCornerRadiusBasedI375:(CGFloat)radius {
  223. self.cornerRadius = CGFloatBasedI375(radius);
  224. }
  225. - (CGFloat)cornerRadiusBasedI375 {
  226. return self.cornerRadius;
  227. }
  228. @end