Sin descripción

WMMenuView.m 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. //
  2. // WMMenuView.m
  3. // WMPageController
  4. //
  5. // Created by Mark on 15/4/26.
  6. // Copyright (c) 2015年 yq. All rights reserved.
  7. //
  8. #import "WMMenuView.h"
  9. #define WMMENUITEM_TAG_OFFSET 6250
  10. #define WMBADGEVIEW_TAG_OFFSET 1212
  11. #define WMDEFAULT_VAULE(value, defaultValue) (value != WMUNDEFINED_VALUE ? value : defaultValue)
  12. @interface WMMenuView ()
  13. @property (nonatomic, weak) WMMenuItem *selItem;
  14. @property (nonatomic, strong) NSMutableArray *frames;
  15. @property (nonatomic, assign) NSInteger selectIndex;
  16. @property (nonatomic, readonly) NSInteger titlesCount;
  17. @end
  18. @implementation WMMenuView
  19. @synthesize progressHeight = _progressHeight;
  20. @synthesize progressViewCornerRadius = _progressViewCornerRadius;
  21. #pragma mark - Setter
  22. - (void)setLayoutMode:(WMMenuViewLayoutMode)layoutMode {
  23. _layoutMode = layoutMode;
  24. if (!self.superview) { return; }
  25. [self reload];
  26. }
  27. - (void)setFrame:(CGRect)frame {
  28. // Adapt iOS 11 if is a titleView
  29. if (@available(iOS 11.0, *)) {
  30. if (self.showOnNavigationBar) { frame.origin.x = 0; }
  31. }
  32. [super setFrame:frame];
  33. if (!self.scrollView) { return; }
  34. CGFloat leftMargin = self.contentMargin + self.leftView.frame.size.width;
  35. CGFloat rightMargin = self.contentMargin + self.rightView.frame.size.width;
  36. CGFloat contentWidth = self.scrollView.frame.size.width + leftMargin + rightMargin;
  37. CGFloat startX = self.leftView ? self.leftView.frame.origin.x : self.scrollView.frame.origin.x - self.contentMargin;
  38. // Make the contentView center, because system will change menuView's frame if it's a titleView.
  39. if (startX + contentWidth / 2 != self.bounds.size.width / 2) {
  40. CGFloat xOffset = (self.bounds.size.width - contentWidth) / 2;
  41. self.leftView.frame = ({
  42. CGRect frame = self.leftView.frame;
  43. frame.origin.x = xOffset;
  44. frame;
  45. });
  46. self.scrollView.frame = ({
  47. CGRect frame = self.scrollView.frame;
  48. frame.origin.x = self.leftView ? CGRectGetMaxX(self.leftView.frame) + self.contentMargin : xOffset;
  49. frame;
  50. });
  51. self.rightView.frame = ({
  52. CGRect frame = self.rightView.frame;
  53. frame.origin.x = CGRectGetMaxX(self.scrollView.frame) + self.contentMargin;
  54. frame;
  55. });
  56. }
  57. }
  58. - (void)setProgressViewCornerRadius:(CGFloat)progressViewCornerRadius {
  59. _progressViewCornerRadius = progressViewCornerRadius;
  60. if (self.progressView) {
  61. self.progressView.cornerRadius = _progressViewCornerRadius;
  62. }
  63. }
  64. - (void)setSpeedFactor:(CGFloat)speedFactor {
  65. _speedFactor = speedFactor;
  66. if (self.progressView) {
  67. self.progressView.speedFactor = _speedFactor;
  68. }
  69. [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  70. if ([obj isKindOfClass:[WMMenuItem class]]) {
  71. ((WMMenuItem *)obj).speedFactor = self->_speedFactor;
  72. }
  73. }];
  74. }
  75. - (void)setProgressWidths:(NSArray *)progressWidths {
  76. _progressWidths = progressWidths;
  77. if (!self.progressView.superview) { return; }
  78. [self resetFramesFromIndex:0];
  79. }
  80. - (void)setLeftView:(UIView *)leftView {
  81. if (self.leftView) {
  82. [self.leftView removeFromSuperview];
  83. _leftView = nil;
  84. }
  85. if (leftView) {
  86. [self addSubview:leftView];
  87. _leftView = leftView;
  88. }
  89. [self resetFrames];
  90. }
  91. - (void)setRightView:(UIView *)rightView {
  92. if (self.rightView) {
  93. [self.rightView removeFromSuperview];
  94. _rightView = nil;
  95. }
  96. if (rightView) {
  97. [self addSubview:rightView];
  98. _rightView = rightView;
  99. }
  100. [self resetFrames];
  101. }
  102. - (void)setContentMargin:(CGFloat)contentMargin {
  103. _contentMargin = contentMargin;
  104. if (self.scrollView) {
  105. [self resetFrames];
  106. }
  107. }
  108. #pragma mark - Getter
  109. - (CGFloat)progressHeight {
  110. switch (self.style) {
  111. case WMMenuViewStyleLine:
  112. case WMMenuViewStyleTriangle:
  113. return WMDEFAULT_VAULE(_progressHeight, 2);
  114. case WMMenuViewStyleFlood:
  115. case WMMenuViewStyleSegmented:
  116. case WMMenuViewStyleFloodHollow:
  117. return WMDEFAULT_VAULE(_progressHeight, ceil(self.frame.size.height * 0.8));
  118. default:
  119. return _progressHeight;
  120. }
  121. }
  122. - (CGFloat)progressViewCornerRadius {
  123. return WMDEFAULT_VAULE(_progressViewCornerRadius, self.progressHeight / 2.0);
  124. }
  125. - (UIColor *)lineColor {
  126. if (!_lineColor) {
  127. _lineColor = [self colorForState:WMMenuItemStateSelected atIndex:0];
  128. }
  129. return _lineColor;
  130. }
  131. - (NSMutableArray *)frames {
  132. if (_frames == nil) {
  133. _frames = [NSMutableArray array];
  134. }
  135. return _frames;
  136. }
  137. - (UIColor *)colorForState:(WMMenuItemState)state atIndex:(NSInteger)index {
  138. if ([self.delegate respondsToSelector:@selector(menuView:titleColorForState:atIndex:)]) {
  139. return [self.delegate menuView:self titleColorForState:state atIndex:index];
  140. }
  141. return [UIColor blackColor];
  142. }
  143. - (CGFloat)sizeForState:(WMMenuItemState)state atIndex:(NSInteger)index {
  144. if ([self.delegate respondsToSelector:@selector(menuView:titleSizeForState:atIndex:)]) {
  145. return [self.delegate menuView:self titleSizeForState:state atIndex:index];
  146. }
  147. return 15.0;
  148. }
  149. - (UIView *)badgeViewAtIndex:(NSInteger)index {
  150. if (![self.dataSource respondsToSelector:@selector(menuView:badgeViewAtIndex:)]) {
  151. return nil;
  152. }
  153. UIView *badgeView = [self.dataSource menuView:self badgeViewAtIndex:index];
  154. if (!badgeView) {
  155. return nil;
  156. }
  157. badgeView.tag = index + WMBADGEVIEW_TAG_OFFSET;
  158. return badgeView;
  159. }
  160. #pragma mark - Public Methods
  161. - (WMMenuItem *)itemAtIndex:(NSInteger)index {
  162. return (WMMenuItem *)[self viewWithTag:(index + WMMENUITEM_TAG_OFFSET)];
  163. }
  164. - (void)setProgressViewIsNaughty:(BOOL)progressViewIsNaughty {
  165. _progressViewIsNaughty = progressViewIsNaughty;
  166. if (self.progressView) {
  167. self.progressView.naughty = progressViewIsNaughty;
  168. }
  169. }
  170. - (void)reload {
  171. [self.frames removeAllObjects];
  172. [self.progressView removeFromSuperview];
  173. [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  174. [obj removeFromSuperview];
  175. }];
  176. [self addItems];
  177. [self makeStyle];
  178. [self addBadgeViews];
  179. }
  180. - (void)slideMenuAtProgress:(CGFloat)progress {
  181. if (self.progressView) {
  182. self.progressView.progress = progress;
  183. }
  184. NSInteger tag = (NSInteger)progress + WMMENUITEM_TAG_OFFSET;
  185. CGFloat rate = progress - tag + WMMENUITEM_TAG_OFFSET;
  186. WMMenuItem *currentItem = (WMMenuItem *)[self viewWithTag:tag];
  187. WMMenuItem *nextItem = (WMMenuItem *)[self viewWithTag:tag+1];
  188. if (rate == 0.0) {
  189. [self.selItem setSelected:NO withAnimation:NO];
  190. self.selItem = currentItem;
  191. [self.selItem setSelected:YES withAnimation:NO];
  192. [self refreshContenOffset];
  193. return;
  194. }
  195. currentItem.rate = 1-rate;
  196. nextItem.rate = rate;
  197. }
  198. - (void)selectItemAtIndex:(NSInteger)index {
  199. NSInteger tag = index + WMMENUITEM_TAG_OFFSET;
  200. NSInteger currentIndex = self.selItem.tag - WMMENUITEM_TAG_OFFSET;
  201. self.selectIndex = index;
  202. if (index == currentIndex || !self.selItem) { return; }
  203. WMMenuItem *item = (WMMenuItem *)[self viewWithTag:tag];
  204. [self.selItem setSelected:NO withAnimation:NO];
  205. self.selItem = item;
  206. [self.selItem setSelected:YES withAnimation:NO];
  207. [self.progressView setProgressWithOutAnimate:index];
  208. if ([self.delegate respondsToSelector:@selector(menuView:didSelectedIndex:currentIndex:)]) {
  209. [self.delegate menuView:self didSelectedIndex:index currentIndex:currentIndex];
  210. }
  211. [self refreshContenOffset];
  212. }
  213. - (void)updateTitle:(NSString *)title atIndex:(NSInteger)index andWidth:(BOOL)update {
  214. if (index >= self.titlesCount || index < 0) { return; }
  215. WMMenuItem *item = (WMMenuItem *)[self viewWithTag:(WMMENUITEM_TAG_OFFSET + index)];
  216. item.text = title;
  217. if (!update) { return; }
  218. [self resetFrames];
  219. }
  220. - (void)updateAttributeTitle:(NSAttributedString *)title atIndex:(NSInteger)index andWidth:(BOOL)update {
  221. if (index >= self.titlesCount || index < 0) { return; }
  222. WMMenuItem *item = (WMMenuItem *)[self viewWithTag:(WMMENUITEM_TAG_OFFSET + index)];
  223. item.attributedText = title;
  224. if (!update) { return; }
  225. [self resetFrames];
  226. }
  227. - (void)updateBadgeViewAtIndex:(NSInteger)index {
  228. UIView *oldBadgeView = [self.scrollView viewWithTag:WMBADGEVIEW_TAG_OFFSET + index];
  229. if (oldBadgeView) {
  230. [oldBadgeView removeFromSuperview];
  231. }
  232. [self addBadgeViewAtIndex:index];
  233. [self resetBadgeFrame:index];
  234. }
  235. // 让选中的item位于中间
  236. - (void)refreshContenOffset {
  237. CGRect frame = self.selItem.frame;
  238. CGFloat itemX = frame.origin.x;
  239. CGFloat width = self.scrollView.frame.size.width;
  240. CGSize contentSize = self.scrollView.contentSize;
  241. if (itemX > width/2) {
  242. CGFloat targetX;
  243. if ((contentSize.width-itemX) <= width/2) {
  244. targetX = contentSize.width - width;
  245. } else {
  246. targetX = frame.origin.x - width/2 + frame.size.width/2;
  247. }
  248. // 应该有更好的解决方法
  249. if (targetX + width > contentSize.width) {
  250. targetX = contentSize.width - width;
  251. }
  252. [self.scrollView setContentOffset:CGPointMake(targetX, 0) animated:YES];
  253. } else {
  254. [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
  255. }
  256. }
  257. #pragma mark - Data source
  258. - (NSInteger)titlesCount {
  259. return [self.dataSource numbersOfTitlesInMenuView:self];
  260. }
  261. #pragma mark - Private Methods
  262. - (instancetype)initWithFrame:(CGRect)frame {
  263. if (self = [super initWithFrame:frame]) {
  264. self.progressViewCornerRadius = WMUNDEFINED_VALUE;
  265. self.progressHeight = WMUNDEFINED_VALUE;
  266. }
  267. return self;
  268. }
  269. - (void)willMoveToSuperview:(UIView *)newSuperview {
  270. [super willMoveToSuperview:newSuperview];
  271. if (self.scrollView) { return; }
  272. [self addScrollView];
  273. [self addItems];
  274. [self makeStyle];
  275. [self addBadgeViews];
  276. [self resetSelectionIfNeeded];
  277. }
  278. - (void)resetSelectionIfNeeded {
  279. if (self.selectIndex == 0) { return; }
  280. [self selectItemAtIndex:self.selectIndex];
  281. }
  282. - (void)resetFrames {
  283. CGRect frame = self.bounds;
  284. if (self.rightView) {
  285. CGRect rightFrame = self.rightView.frame;
  286. rightFrame.origin.x = frame.size.width - rightFrame.size.width;
  287. self.rightView.frame = rightFrame;
  288. frame.size.width -= rightFrame.size.width;
  289. }
  290. if (self.leftView) {
  291. CGRect leftFrame = self.leftView.frame;
  292. leftFrame.origin.x = 0;
  293. self.leftView.frame = leftFrame;
  294. frame.origin.x += leftFrame.size.width;
  295. frame.size.width -= leftFrame.size.width;
  296. }
  297. frame.origin.x += self.contentMargin;
  298. frame.size.width -= self.contentMargin * 2;
  299. self.scrollView.frame = frame;
  300. [self resetFramesFromIndex:0];
  301. }
  302. - (void)resetFramesFromIndex:(NSInteger)index {
  303. [self.frames removeAllObjects];
  304. [self calculateItemFrames];
  305. for (NSInteger i = index; i < self.titlesCount; i++) {
  306. [self resetItemFrame:i];
  307. [self resetBadgeFrame:i];
  308. }
  309. if (!self.progressView.superview) { return; }
  310. self.progressView.frame = [self calculateProgressViewFrame];
  311. self.progressView.cornerRadius = self.progressViewCornerRadius;
  312. self.progressView.itemFrames = [self convertProgressWidthsToFrames];
  313. [self.progressView setNeedsDisplay];
  314. }
  315. - (CGRect)calculateProgressViewFrame {
  316. switch (self.style) {
  317. case WMMenuViewStyleDefault: {
  318. return CGRectZero;
  319. }
  320. case WMMenuViewStyleLine:
  321. case WMMenuViewStyleTriangle: {
  322. return CGRectMake(0, self.frame.size.height - self.progressHeight - self.progressViewBottomSpace, self.scrollView.contentSize.width, self.progressHeight);
  323. }
  324. case WMMenuViewStyleFloodHollow:
  325. case WMMenuViewStyleSegmented:
  326. case WMMenuViewStyleFlood: {
  327. return CGRectMake(0, (self.frame.size.height - self.progressHeight) / 2, self.scrollView.contentSize.width, self.progressHeight);
  328. }
  329. }
  330. }
  331. - (void)resetItemFrame:(NSInteger)index {
  332. WMMenuItem *item = (WMMenuItem *)[self viewWithTag:(WMMENUITEM_TAG_OFFSET + index)];
  333. CGRect frame = [self.frames[index] CGRectValue];
  334. item.frame = frame;
  335. if ([self.delegate respondsToSelector:@selector(menuView:didLayoutItemFrame:atIndex:)]) {
  336. [self.delegate menuView:self didLayoutItemFrame:item atIndex:index];
  337. }
  338. }
  339. - (void)resetBadgeFrame:(NSInteger)index {
  340. CGRect frame = [self.frames[index] CGRectValue];
  341. UIView *badgeView = [self.scrollView viewWithTag:(WMBADGEVIEW_TAG_OFFSET + index)];
  342. if (badgeView) {
  343. CGRect badgeFrame = [self badgeViewAtIndex:index].frame;
  344. badgeFrame.origin.x += frame.origin.x;
  345. badgeView.frame = badgeFrame;
  346. }
  347. }
  348. - (NSArray *)convertProgressWidthsToFrames {
  349. if (!self.frames.count) { NSAssert(NO, @"BUUUUUUUG...SHOULDN'T COME HERE!!"); }
  350. if (self.progressWidths.count < self.titlesCount) return self.frames;
  351. NSMutableArray *progressFrames = [NSMutableArray array];
  352. NSInteger count = (self.frames.count <= self.progressWidths.count) ? self.frames.count : self.progressWidths.count;
  353. for (int i = 0; i < count; i++) {
  354. CGRect itemFrame = [self.frames[i] CGRectValue];
  355. CGFloat progressWidth = [self.progressWidths[i] floatValue];
  356. CGFloat x = itemFrame.origin.x + (itemFrame.size.width - progressWidth) / 2;
  357. CGRect progressFrame = CGRectMake(x, itemFrame.origin.y, progressWidth, 0);
  358. [progressFrames addObject:[NSValue valueWithCGRect:progressFrame]];
  359. }
  360. return progressFrames.copy;
  361. }
  362. - (void)addBadgeViews {
  363. for (int i = 0; i < self.titlesCount; i++) {
  364. [self addBadgeViewAtIndex:i];
  365. }
  366. }
  367. - (void)addBadgeViewAtIndex:(NSInteger)index {
  368. UIView *badgeView = [self badgeViewAtIndex:index];
  369. if (badgeView) {
  370. [self.scrollView addSubview:badgeView];
  371. }
  372. }
  373. - (void)makeStyle {
  374. CGRect frame = [self calculateProgressViewFrame];
  375. if (CGRectEqualToRect(frame, CGRectZero)) {
  376. return;
  377. }
  378. [self addProgressViewWithFrame:frame
  379. isTriangle:(self.style == WMMenuViewStyleTriangle)
  380. hasBorder:(self.style == WMMenuViewStyleSegmented)
  381. hollow:(self.style == WMMenuViewStyleFloodHollow)
  382. cornerRadius:self.progressViewCornerRadius];
  383. }
  384. - (void)deselectedItemsIfNeeded {
  385. [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  386. if (![obj isKindOfClass:[WMMenuItem class]] || obj == self.selItem) { return; }
  387. [(WMMenuItem *)obj setSelected:NO withAnimation:NO];
  388. }];
  389. }
  390. - (void)addScrollView {
  391. CGFloat width = self.frame.size.width - self.contentMargin * 2;
  392. CGFloat height = self.frame.size.height;
  393. CGRect frame = CGRectMake(self.contentMargin, 0, width, height);
  394. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
  395. scrollView.showsHorizontalScrollIndicator = NO;
  396. scrollView.showsVerticalScrollIndicator = NO;
  397. scrollView.backgroundColor = [UIColor clearColor];
  398. scrollView.scrollsToTop = NO;
  399. if (@available(iOS 11.0, *)) {
  400. scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  401. }
  402. [self addSubview:scrollView];
  403. self.scrollView = scrollView;
  404. }
  405. - (void)addItems {
  406. [self calculateItemFrames];
  407. for (int i = 0; i < self.titlesCount; i++) {
  408. CGRect frame = [self.frames[i] CGRectValue];
  409. WMMenuItem *item = [[WMMenuItem alloc] initWithFrame:frame];
  410. item.tag = (i + WMMENUITEM_TAG_OFFSET);
  411. item.delegate = self;
  412. item.text = [self.dataSource menuView:self titleAtIndex:i];
  413. item.textAlignment = NSTextAlignmentCenter;
  414. item.userInteractionEnabled = YES;
  415. item.backgroundColor = [UIColor clearColor];
  416. item.normalSize = [self sizeForState:WMMenuItemStateNormal atIndex:i];
  417. item.selectedSize = [self sizeForState:WMMenuItemStateSelected atIndex:i];
  418. item.normalColor = [self colorForState:WMMenuItemStateNormal atIndex:i];
  419. item.selectedColor = [self colorForState:WMMenuItemStateSelected atIndex:i];
  420. item.speedFactor = self.speedFactor;
  421. if (self.fontName) {
  422. item.font = [UIFont fontWithName:self.fontName size:item.selectedSize];
  423. } else {
  424. item.font = [UIFont systemFontOfSize:item.selectedSize];
  425. }
  426. if ([self.dataSource respondsToSelector:@selector(menuView:initialMenuItem:atIndex:)]) {
  427. item = [self.dataSource menuView:self initialMenuItem:item atIndex:i];
  428. }
  429. if (i == 0) {
  430. [item setSelected:YES withAnimation:NO];
  431. self.selItem = item;
  432. } else {
  433. [item setSelected:NO withAnimation:NO];
  434. }
  435. [self.scrollView addSubview:item];
  436. }
  437. }
  438. // 计算所有item的frame值,主要是为了适配所有item的宽度之和小于屏幕宽的情况
  439. // 这里与后面的 `-addItems` 做了重复的操作,并不是很合理
  440. - (void)calculateItemFrames {
  441. CGFloat contentWidth = [self itemMarginAtIndex:0];
  442. for (int i = 0; i < self.titlesCount; i++) {
  443. CGFloat itemW = 60.0;
  444. if ([self.delegate respondsToSelector:@selector(menuView:widthForItemAtIndex:)]) {
  445. itemW = [self.delegate menuView:self widthForItemAtIndex:i];
  446. }
  447. CGRect frame = CGRectMake(contentWidth, 0, itemW, self.frame.size.height);
  448. // 记录frame
  449. [self.frames addObject:[NSValue valueWithCGRect:frame]];
  450. contentWidth += itemW + [self itemMarginAtIndex:i+1];
  451. }
  452. // 如果总宽度小于屏幕宽,重新计算frame,为item间添加间距
  453. if (contentWidth < self.scrollView.frame.size.width) {
  454. CGFloat distance = self.scrollView.frame.size.width - contentWidth;
  455. CGFloat (^shiftDis)(int);
  456. switch (self.layoutMode) {
  457. case WMMenuViewLayoutModeScatter: {
  458. CGFloat gap = distance / (self.titlesCount + 1);
  459. shiftDis = ^CGFloat(int index) { return gap * (index + 1); };
  460. break;
  461. }
  462. case WMMenuViewLayoutModeLeft: {
  463. shiftDis = ^CGFloat(int index) { return 0.0; };
  464. break;
  465. }
  466. case WMMenuViewLayoutModeRight: {
  467. shiftDis = ^CGFloat(int index) { return distance; };
  468. break;
  469. }
  470. case WMMenuViewLayoutModeCenter: {
  471. shiftDis = ^CGFloat(int index) { return distance / 2; };
  472. break;
  473. }
  474. }
  475. for (int i = 0; i < self.frames.count; i++) {
  476. CGRect frame = [self.frames[i] CGRectValue];
  477. frame.origin.x += shiftDis(i);
  478. self.frames[i] = [NSValue valueWithCGRect:frame];
  479. }
  480. contentWidth = self.scrollView.frame.size.width;
  481. }
  482. self.scrollView.contentSize = CGSizeMake(contentWidth, self.frame.size.height);
  483. }
  484. - (CGFloat)itemMarginAtIndex:(NSInteger)index {
  485. if ([self.delegate respondsToSelector:@selector(menuView:itemMarginAtIndex:)]) {
  486. return [self.delegate menuView:self itemMarginAtIndex:index];
  487. }
  488. return 0.0;
  489. }
  490. // MARK:Progress View
  491. - (void)addProgressViewWithFrame:(CGRect)frame isTriangle:(BOOL)isTriangle hasBorder:(BOOL)hasBorder hollow:(BOOL)isHollow cornerRadius:(CGFloat)cornerRadius {
  492. WMProgressView *pView = [[WMProgressView alloc] initWithFrame:frame];
  493. pView.itemFrames = [self convertProgressWidthsToFrames];
  494. pView.color = self.lineColor.CGColor;
  495. pView.isTriangle = isTriangle;
  496. pView.hasBorder = hasBorder;
  497. pView.hollow = isHollow;
  498. pView.cornerRadius = cornerRadius;
  499. pView.naughty = self.progressViewIsNaughty;
  500. pView.speedFactor = self.speedFactor;
  501. pView.backgroundColor = [UIColor clearColor];
  502. self.progressView = pView;
  503. [self.scrollView insertSubview:self.progressView atIndex:0];
  504. }
  505. #pragma mark - Menu item delegate
  506. - (void)didPressedMenuItem:(WMMenuItem *)menuItem {
  507. if ([self.delegate respondsToSelector:@selector(menuView:shouldSelesctedIndex:)]) {
  508. BOOL should = [self.delegate menuView:self shouldSelesctedIndex:menuItem.tag - WMMENUITEM_TAG_OFFSET];
  509. if (!should) {
  510. return;
  511. }
  512. }
  513. CGFloat progress = menuItem.tag - WMMENUITEM_TAG_OFFSET;
  514. [self.progressView moveToPostion:progress];
  515. NSInteger currentIndex = self.selItem.tag - WMMENUITEM_TAG_OFFSET;
  516. if ([self.delegate respondsToSelector:@selector(menuView:didSelectedIndex:currentIndex:)]) {
  517. [self.delegate menuView:self didSelectedIndex:menuItem.tag - WMMENUITEM_TAG_OFFSET currentIndex:currentIndex];
  518. }
  519. [self.selItem setSelected:NO withAnimation:YES];
  520. [menuItem setSelected:YES withAnimation:YES];
  521. self.selItem = menuItem;
  522. NSTimeInterval delay = self.style == WMMenuViewStyleDefault ? 0 : 0.3f;
  523. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  524. // 让选中的item位于中间
  525. [self refreshContenOffset];
  526. });
  527. }
  528. @end