MLMSegmentManager.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // MLMSegmentManager.m
  3. // MLMSegmentPage
  4. //
  5. // Created by my on 2017/2/5.
  6. // Copyright © 2017年 my. All rights reserved.
  7. //
  8. #import "MLMSegmentManager.h"
  9. @implementation MLMSegmentManager
  10. + (void)associateHead:(MLMSegmentHead *)head
  11. withScroll:(MLMSegmentScroll *)scroll
  12. completion:(void(^)())completion {
  13. [MLMSegmentManager associateHead:head withScroll:scroll contentChangeAni:YES completion:completion selectEnd:nil];
  14. }
  15. + (void)associateHead:(MLMSegmentHead *)head
  16. withScroll:(MLMSegmentScroll *)scroll
  17. contentChangeAni:(BOOL)ani
  18. completion:(void(^)())completion
  19. selectEnd:(void(^)(NSInteger index))selectEnd {
  20. NSInteger showIndex;
  21. showIndex = head.showIndex?head.showIndex:scroll.showIndex;
  22. head.showIndex = showIndex;
  23. [head defaultAndCreateView];
  24. WEAK(weakScroll, scroll)
  25. head.selectedIndex = ^(NSInteger index) {
  26. dispatch_async(dispatch_get_main_queue(), ^{
  27. [weakScroll setContentOffset:CGPointMake(index*weakScroll.width, 0) animated:ani];
  28. });
  29. };
  30. if (completion) {
  31. completion();
  32. }
  33. WEAK(weakHead, head)
  34. scroll.scrollEnd = ^(NSInteger index) {
  35. [weakHead setSelectIndex:index];
  36. [weakHead animationEnd];
  37. if (selectEnd) {
  38. selectEnd(index);
  39. }
  40. };
  41. scroll.animationEnd = ^(NSInteger index) {
  42. [weakHead setSelectIndex:index];
  43. [weakHead animationEnd];
  44. if (selectEnd) {
  45. selectEnd(index);
  46. }
  47. };
  48. scroll.offsetScale = ^(CGFloat scale) {
  49. [weakHead changePointScale:scale];
  50. };
  51. scroll.showIndex = showIndex;
  52. [scroll createView];
  53. UIView *view = head.nextResponder?head:scroll;
  54. UIViewController *currentVC = [view viewController];
  55. currentVC.automaticallyAdjustsScrollViewInsets = NO;
  56. }
  57. @end