VTMagicController.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // VTMagicController.m
  3. // VTMagicView
  4. //
  5. // Created by tianzhuo on 14-11-11.
  6. // Copyright (c) 2014年 tianzhuo. All rights reserved.
  7. //
  8. #import "VTMagicController.h"
  9. @interface VTMagicController ()
  10. @end
  11. @implementation VTMagicController
  12. #pragma mark - Lifecycle
  13. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  14. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  15. if (self) {
  16. if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
  17. self.edgesForExtendedLayout = UIRectEdgeNone;
  18. } else if ([self respondsToSelector:@selector(setWantsFullScreenLayout:)]) {
  19. [self setValue:@YES forKey:@"wantsFullScreenLayout"];
  20. }
  21. }
  22. return self;
  23. }
  24. - (void)loadView {
  25. [super loadView];
  26. self.view = self.magicView;
  27. }
  28. - (void)viewWillAppear:(BOOL)animated {
  29. [super viewWillAppear:animated];
  30. self.appearanceState = VTAppearanceStateWillAppear;
  31. if (!_magicView.isSwitching) {
  32. [_currentViewController beginAppearanceTransition:YES animated:animated];
  33. }
  34. }
  35. - (void)viewDidAppear:(BOOL)animated {
  36. [super viewDidAppear:animated];
  37. self.appearanceState = VTAppearanceStateDidAppear;
  38. if (!_magicView.isSwitching) {
  39. [_currentViewController endAppearanceTransition];
  40. }
  41. }
  42. - (void)viewWillDisappear:(BOOL)animated {
  43. [super viewWillDisappear:animated];
  44. self.appearanceState = VTAppearanceStateWillDisappear;
  45. if (!_magicView.isSwitching) {
  46. [_currentViewController beginAppearanceTransition:NO animated:animated];
  47. }
  48. }
  49. - (void)viewDidDisappear:(BOOL)animated {
  50. [super viewDidDisappear:animated];
  51. self.appearanceState = VTAppearanceStateDidDisappear;
  52. if (!_magicView.isSwitching) {
  53. [_currentViewController endAppearanceTransition];
  54. }
  55. }
  56. #pragma mark - forward appearance methods
  57. - (BOOL)shouldAutomaticallyForwardAppearanceMethods {
  58. return NO;
  59. }
  60. #pragma mark - functional methods
  61. - (UIViewController *)viewControllerAtPage:(NSUInteger)pageIndex {
  62. return [self.magicView viewControllerAtPage:pageIndex];
  63. }
  64. - (void)switchToPage:(NSUInteger)pageIndex animated:(BOOL)animated {
  65. [self.magicView switchToPage:pageIndex animated:animated];
  66. }
  67. #pragma mark - VTMagicViewDataSource
  68. - (NSArray<NSString *> *)menuTitlesForMagicView:(VTMagicView *)magicView {
  69. return @[];
  70. }
  71. - (UIButton *)magicView:(VTMagicView *)magicView menuItemAtIndex:(NSUInteger)itemIndex {
  72. static NSString *itemIdentifier = @"itemIdentifier";
  73. UIButton *menuItem = [magicView dequeueReusableItemWithIdentifier:itemIdentifier];
  74. if (!menuItem) {
  75. menuItem = [UIButton buttonWithType:UIButtonTypeCustom];
  76. [menuItem setTitleColor:RGBCOLOR(50, 50, 50) forState:UIControlStateNormal];
  77. [menuItem setTitleColor:RGBCOLOR(169, 37, 37) forState:UIControlStateSelected];
  78. menuItem.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:15.f];
  79. }
  80. return menuItem;
  81. }
  82. - (UIViewController *)magicView:(VTMagicView *)magicView viewControllerAtPage:(NSUInteger)pageInde {
  83. static NSString *pageId = @"page.identifier";
  84. UIViewController *viewController = [magicView dequeueReusablePageWithIdentifier:pageId];
  85. if (!viewController) {
  86. viewController = [[UIViewController alloc] init];
  87. }
  88. return viewController;
  89. }
  90. #pragma mark - VTMagicViewDelegate
  91. - (void)magicView:(VTMagicView *)magicView viewDidAppear:(__kindof UIViewController *)viewController atPage:(NSUInteger)pageIndex {
  92. VTLog(@"index:%ld viewControllerDidAppear:%@", (long)pageIndex, viewController.view);
  93. }
  94. - (void)magicView:(VTMagicView *)magicView viewDidDisappear:(__kindof UIViewController *)viewController atPage:(NSUInteger)pageIndex {
  95. VTLog(@"index:%ld viewControllerDidDisappear:%@", (long)pageIndex, viewController.view);
  96. }
  97. - (void)magicView:(VTMagicView *)magicView didSelectItemAtIndex:(NSUInteger)itemIndex {
  98. VTLog(@"didSelectItemAtIndex:%ld", (long)itemIndex);
  99. }
  100. #pragma mark - accessor methods
  101. - (VTMagicView *)magicView {
  102. if (!_magicView) {
  103. _magicView = [[VTMagicView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  104. _magicView.autoresizesSubviews = YES;
  105. _magicView.magicController = self;
  106. _magicView.delegate = self;
  107. _magicView.dataSource = self;
  108. [self.view setNeedsLayout];
  109. }
  110. return _magicView;
  111. }
  112. - (NSArray<UIViewController *> *)viewControllers {
  113. return self.magicView.viewControllers;
  114. }
  115. @end