省钱达人

MLMSegmentManager.m 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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