财神随手记账

TXBYCardSwitchView.m 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // TXBYCardSwitchView.m
  3. // TXBYFelicity
  4. //
  5. // Created by YandL on 16/2/2.
  6. // Copyright © 2016年 YandL All rights reserved.
  7. //
  8. #import "TXBYCardSwitchView.h"
  9. @implementation TXBYCardSwitchView
  10. - (instancetype)initWithFrame:(CGRect)frame {
  11. self = [super initWithFrame:frame];
  12. if (self) {
  13. _cardSwitchScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  14. _cardSwitchScrollView.backgroundColor = [UIColor clearColor];
  15. _cardSwitchScrollView.showsHorizontalScrollIndicator = NO;
  16. _cardSwitchScrollView.delegate = self;
  17. [self addSubview:_cardSwitchScrollView];
  18. }
  19. return self;
  20. }
  21. - (void)setIndex:(NSInteger)index {
  22. CGFloat ScreenWidth = [UIScreen mainScreen].bounds.size.width;
  23. [self.cardSwitchScrollView setContentOffset:CGPointMake((230 + (ScreenWidth - 230) / 4) * index, 0) animated:YES];
  24. }
  25. - (void)setCardSwitchViewAry:(NSArray *)cardSwitchViewAry delegate:(id)delegate {
  26. _delegate = delegate;
  27. float space = 0;
  28. float width = 0;
  29. float viewWidth = 0;
  30. for (UIView *view in cardSwitchViewAry) {
  31. NSInteger index = [cardSwitchViewAry indexOfObject:view];
  32. if (index == 0) {
  33. viewWidth = view.frame.size.width;
  34. }
  35. space = self.frame.size.width - viewWidth;
  36. width = viewWidth + space/4;
  37. view.frame = CGRectMake(space/2 + (view.frame.size.width + space/4)*index, 0, viewWidth, view.frame.size.height);
  38. CGFloat y = index * width;
  39. CGFloat value = (0-y)/width;
  40. CGFloat scale = fabs(cos(fabs(value)*M_PI/5));
  41. view.transform = CGAffineTransformMakeScale(1.0, scale);
  42. view.alpha = scale * scale * scale;
  43. [_cardSwitchScrollView addSubview:view];
  44. }
  45. _cardSwitchScrollView.contentSize = CGSizeMake((space/2 + width*cardSwitchViewAry.count)+space/4, _cardSwitchScrollView.frame.size.height);
  46. }
  47. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  48. CGFloat offset = scrollView.contentOffset.x;
  49. if (offset < 0) {
  50. return;
  51. }
  52. float space = 0;
  53. float viewWidth = 0;
  54. for (UIView *view in _cardSwitchScrollView.subviews) {
  55. NSInteger index = [_cardSwitchScrollView.subviews indexOfObject:view];
  56. if (index == 0) {
  57. viewWidth = view.frame.size.width;
  58. }
  59. space = self.frame.size.width - viewWidth;
  60. CGFloat width = viewWidth + space/4;
  61. CGFloat y = index * width;
  62. CGFloat value = (offset-y)/width;
  63. CGFloat scale = fabs(cos(fabs(value)*M_PI/5));
  64. view.transform = CGAffineTransformMakeScale(1.0, scale);
  65. view.alpha = scale * scale * scale;
  66. }
  67. float a = offset/(viewWidth + space/4);
  68. if (a - (int)a > 0.5) {
  69. _currentIndex = (int)a + 1;
  70. } else {
  71. _currentIndex = (int)a;
  72. }
  73. }
  74. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  75. // [self scrollToViewCenter];
  76. }
  77. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  78. [self scrollToViewCenter];
  79. }
  80. - (void)scrollToViewCenter {
  81. float space = 0;
  82. float viewWidth = 0;
  83. for (UIView *view in _cardSwitchScrollView.subviews) {
  84. NSInteger index = [_cardSwitchScrollView.subviews indexOfObject:view];
  85. if (index == 0) {
  86. viewWidth = view.frame.size.width;
  87. }
  88. space = self.frame.size.width - viewWidth;
  89. }
  90. [_cardSwitchScrollView scrollRectToVisible:CGRectMake(_currentIndex*(viewWidth + space/4), 0, _cardSwitchScrollView.frame.size.width, _cardSwitchScrollView.frame.size.height) animated:YES];
  91. if (_delegate && [_delegate respondsToSelector:@selector(cardSwitchViewDidScroll:index:)]) {
  92. [_delegate cardSwitchViewDidScroll:self index:_currentIndex];
  93. }
  94. }
  95. @end