SlaveScrollView.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2014-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "SlaveScrollView.h"
  21. #import "BallotMatrixLabelView.h"
  22. #import "BallotResultMatrixCell.h"
  23. @interface SlaveScrollView ()
  24. @property ScrollViewContent *contentView;
  25. @property CGFloat maxX;
  26. @property CGFloat maxY;
  27. @end
  28. @implementation SlaveScrollView
  29. - (instancetype)initWithFrame:(CGRect)frame
  30. {
  31. self = [super initWithFrame:frame];
  32. if (self) {
  33. _horizontalScrollingEnabled = YES;
  34. self.clipsToBounds = YES;
  35. self.alwaysBounceVertical = NO;
  36. self.alwaysBounceHorizontal = NO;
  37. self.showsVerticalScrollIndicator = NO;
  38. self.showsHorizontalScrollIndicator = NO;
  39. }
  40. return self;
  41. }
  42. - (void)setContent:(ScrollViewContent *)contentView {
  43. if (_contentView != contentView) {
  44. [_contentView removeFromSuperview];
  45. }
  46. _contentView = contentView;
  47. [self addSubview:_contentView];
  48. [self updateContentSize];
  49. }
  50. - (void)setFrame:(CGRect)frame {
  51. [super setFrame:frame];
  52. [self updateContentSize];
  53. }
  54. - (void)updateContentSize {
  55. [_contentView adaptToWidth: self.frame.size.width];
  56. [self setContentSize: _contentView.frame.size];
  57. _maxX = roundf(_contentView.bounds.size.width - self.bounds.size.width);
  58. _maxY = roundf(_contentView.bounds.size.height - self.bounds.size.height);
  59. }
  60. - (CGPoint)setPosition:(CGPoint)position {
  61. CGFloat x = fmaxf(0.0, position.x);
  62. x = fminf(x, _maxX);
  63. if (_maxX < 0.0 || _horizontalScrollingEnabled == NO) {
  64. x = 0.0;
  65. }
  66. CGFloat y = fmaxf(0.0, position.y);
  67. y = fminf(y, _maxY);
  68. if (_maxY < 0.0) {
  69. y = 0.0;
  70. }
  71. CGPoint resultingPosition = CGPointMake(x, y);
  72. self.contentOffset = resultingPosition;
  73. return resultingPosition;
  74. }
  75. - (CGPoint)position {
  76. return self.contentOffset;
  77. }
  78. - (void)setColor:(UIColor *)color forRowAt:(NSInteger)index {
  79. NSArray *subviews = [self.contentView subviews];
  80. if (index < [subviews count]) {
  81. UIView *view = [subviews objectAtIndex:index];
  82. view.backgroundColor = color;
  83. }
  84. }
  85. - (void)setTextColor:(UIColor *)color forRowAt:(NSInteger)index {
  86. NSArray *subviews = [self.contentView subviews];
  87. if (index < [subviews count]) {
  88. UIView *view = [subviews objectAtIndex:index];
  89. if ([view isKindOfClass:[BallotMatrixLabelView class]] && ([Colors getTheme] == ColorThemeLight || [Colors getTheme] == ColorThemeLightWork)) {
  90. BallotMatrixLabelView *b = (BallotMatrixLabelView *)view;
  91. [b setTextColor:[Colors fontInverted]];
  92. } else {
  93. NSArray *subsub = [view subviews];
  94. [subsub enumerateObjectsUsingBlock:^(id v, NSUInteger idx, BOOL * _Nonnull stop) {
  95. if ([v isKindOfClass:[BallotResultMatrixCell class]]) {
  96. BallotResultMatrixCell *cell = (BallotResultMatrixCell *)v;
  97. [cell setColorForChoice:[Colors fontInverted]];
  98. }
  99. }];
  100. }
  101. }
  102. }
  103. @end