XYRotaryTableView.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // XYRotaryTableView.m
  3. // Timi
  4. //
  5. // Created by 翟玉磊 on 2021/4/14.
  6. //
  7. #import "XYRotaryTableView.h"
  8. @interface XYRotaryTableView ()<CAAnimationDelegate>
  9. @property (nonatomic, strong) UIImageView *backgroundImageView;
  10. @property (nonatomic, strong) UIImageView *itemBackgroundImageView;
  11. @property (nonatomic, strong) UIImageView *pointerImageView;
  12. @property (nonatomic, strong) UIButton *centerButton;
  13. @property (nonatomic, strong) UILabel *hideTimeLabel;
  14. @property (nonatomic, assign) NSInteger lotteryResult;
  15. @end
  16. @implementation XYRotaryTableView
  17. - (void)dealloc
  18. {
  19. [self clear];
  20. }
  21. #pragma mark — Public
  22. - (void)clear {
  23. // 删除layer动画 回归初始状态
  24. [self.itemBackgroundImageView.layer removeAllAnimations];
  25. for (UIView *view in self.itemBackgroundImageView.subviews) {
  26. [view removeFromSuperview];
  27. }
  28. for (UIView *view in self.subviews) {
  29. [view removeFromSuperview];
  30. }
  31. }
  32. /// 设置转盘奖项
  33. - (void)setupRotaryTableItems:(NSArray *)array {
  34. for (UIView *view in self.itemBackgroundImageView.subviews) {
  35. [view removeFromSuperview];
  36. }
  37. for (int i = 0; i < array.count; i ++) {
  38. UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, M_PI * self.itemBackgroundImageView.f_heigh/array.count,self.itemBackgroundImageView.f_heigh*3/5)];
  39. label.layer.anchorPoint = CGPointMake(0.5, 1.0);
  40. label.center = CGPointMake(self.itemBackgroundImageView.f_heigh/2, self.itemBackgroundImageView.f_heigh/2);
  41. label.text = [NSString stringWithFormat:@"%@", array[i]];
  42. label.textAlignment = NSTextAlignmentCenter;
  43. label.font = Font_B(14);
  44. // 字体颜色
  45. if (i%2) {
  46. label.textColor = ColorFromHexString(@"#AA7BFD");
  47. }else {
  48. label.textColor = ColorFromHexString(@"#ED68E2");
  49. }
  50. // 旋转角度
  51. CGFloat angle = M_PI*2/array.count * i;
  52. label.transform = CGAffineTransformMakeRotation(angle);
  53. [self.itemBackgroundImageView addSubview:label];
  54. }
  55. self.state = XYRotaryTableStateNormal;
  56. }
  57. /// 设置转盘结果 (类目索引)
  58. - (void)setupRotaryTableResult:(NSInteger)index {
  59. self.lotteryResult = index;
  60. self.state = XYRotaryTableStateRotating;
  61. [self startAnimation];
  62. }
  63. - (void)setupHideTime:(NSInteger)time {
  64. if (self.enabled) {
  65. }else {
  66. self.hideTimeLabel.text = [NSString stringWithFormat:@"%lds", time];
  67. }
  68. }
  69. #pragma mark — Action
  70. // 开始抽奖
  71. - (void)centerButtonAction:(id)sender {
  72. self.centerButton.userInteractionEnabled = NO;
  73. if (self.delegate && [self.delegate respondsToSelector:@selector(turntableLotteryWithRotaryTable:)]) {
  74. [self.delegate turntableLotteryWithRotaryTable:self];
  75. }
  76. }
  77. - (void)startAnimation {
  78. //设置转圈的圈数
  79. NSInteger circleNum = 6;
  80. CGFloat circleAngle = 0;
  81. if (_lotteryResult == 1) {
  82. circleAngle = 0;
  83. }else if (_lotteryResult == 2){
  84. circleAngle = 315;
  85. }else if (_lotteryResult == 3){
  86. circleAngle = 270;
  87. }else if (_lotteryResult == 4){
  88. circleAngle = 225;
  89. }else if (_lotteryResult == 5){
  90. circleAngle = 180;
  91. }else if (_lotteryResult == 6){
  92. circleAngle = 135;
  93. }else if (_lotteryResult == 7){
  94. circleAngle = 90;
  95. }else if (_lotteryResult == 8){
  96. circleAngle = 45;
  97. }
  98. // /*
  99. // 1.因为目前有8等分 一个圆周分成8份就是一份45
  100. // */
  101. // circleAngle = 45 * (self.lotteryResult-1)/*后面中奖索引是以1开始的 所以这里要减1*/;
  102. CGFloat perAngle = M_PI/180.0;
  103. NSLog(@"turnAngle = %ld",(long)circleAngle);
  104. CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
  105. rotationAnimation.toValue = [NSNumber numberWithFloat:circleAngle * perAngle + 360 * perAngle * circleNum];
  106. rotationAnimation.duration = self.animationDuration;
  107. rotationAnimation.cumulative = YES;
  108. rotationAnimation.delegate = self;
  109. //由快变慢
  110. rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  111. rotationAnimation.fillMode=kCAFillModeForwards;
  112. rotationAnimation.removedOnCompletion = NO;
  113. [self.itemBackgroundImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
  114. if (self.delegate && [self.delegate respondsToSelector:@selector(didBeginRotatingWithRotaryTable:)]) {
  115. [self.delegate didBeginRotatingWithRotaryTable:self];
  116. }
  117. }
  118. #pragma mark — CAAnimationDelegate
  119. - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
  120. NSLog(@"动画停止");
  121. self.state = XYRotaryTableStateFinish;
  122. if (self.delegate && [self.delegate respondsToSelector:@selector(didEndRotatingWithRotaryTable:)]) {
  123. [self.delegate didEndRotatingWithRotaryTable:self];
  124. }
  125. }
  126. - (instancetype)initWithFrame:(CGRect)frame
  127. {
  128. self = [super initWithFrame:frame];
  129. if (self) {
  130. [self initValue];
  131. [self setupUI];
  132. }
  133. return self;
  134. }
  135. - (void)initValue {
  136. self.enabled = YES;
  137. self.animationDuration = 3;
  138. }
  139. #pragma mark — UI
  140. - (void)setupUI {
  141. self.backgroundColor = Color_Clear;
  142. [self addSubview:self.backgroundImageView];
  143. [self addSubview:self.itemBackgroundImageView];
  144. [self addSubview:self.pointerImageView];
  145. [self addSubview:self.centerButton];
  146. [self addSubview:self.hideTimeLabel];
  147. }
  148. - (void)setEnabled:(BOOL)enabled {
  149. _enabled = enabled;
  150. self.state = _state;
  151. }
  152. - (void)setState:(XYRotaryTableState)state {
  153. _state = state;
  154. self.hideTimeLabel.text = @"";
  155. switch (state) {
  156. case XYRotaryTableStateNormal:
  157. if (self.enabled) {
  158. self.centerButton.userInteractionEnabled = YES;
  159. [self.centerButton setTitle:@"点击摇奖" forState:UIControlStateNormal];
  160. }else {
  161. self.centerButton.userInteractionEnabled = NO;
  162. [self.centerButton setTitle:@"等待摇奖" forState:UIControlStateNormal];
  163. }
  164. // 删除layer动画 回归初始状态
  165. [self.itemBackgroundImageView.layer removeAllAnimations];
  166. break;
  167. case XYRotaryTableStateReady:
  168. self.centerButton.userInteractionEnabled = NO;
  169. [self.centerButton setTitle:@"启动中" forState:UIControlStateNormal];
  170. break;
  171. case XYRotaryTableStateRotating:
  172. self.centerButton.userInteractionEnabled = NO;
  173. [self.centerButton setTitle:@"摇奖中" forState:UIControlStateNormal];
  174. break;
  175. case XYRotaryTableStateFinish:
  176. if (self.enabled) {
  177. self.centerButton.userInteractionEnabled = YES;
  178. [self.centerButton setTitle:@"再来一局" forState:UIControlStateNormal];
  179. }else {
  180. self.centerButton.userInteractionEnabled = NO;
  181. [self.centerButton setTitle:@"已出奖" forState:UIControlStateNormal];
  182. }
  183. break;
  184. }
  185. }
  186. #pragma mark — Getter
  187. - (UIImageView *)backgroundImageView {
  188. if (!_backgroundImageView) {
  189. _backgroundImageView = [[UIImageView alloc] initWithFrame:self.bounds];
  190. _backgroundImageView.backgroundColor = Color_Clear;
  191. _backgroundImageView.image = ImageNamed(@"xy-chatroom-rotarytable-bg");
  192. }
  193. return _backgroundImageView;
  194. }
  195. - (UIImageView *)itemBackgroundImageView {
  196. if (!_itemBackgroundImageView) {
  197. _itemBackgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20.0f, 20.0f, self.backgroundImageView.f_width-40.0f, self.backgroundImageView.f_heigh-40.0f)];
  198. _itemBackgroundImageView.center = self.backgroundImageView.center;
  199. _itemBackgroundImageView.backgroundColor = Color_Clear;
  200. _itemBackgroundImageView.image = ImageNamed(@"xy-chatroom-rotarytable-item-bg");
  201. }
  202. return _itemBackgroundImageView;
  203. }
  204. - (UIImageView *)pointerImageView {
  205. if (!_pointerImageView) {
  206. CGFloat width = 50.0f;
  207. CGFloat height = 50.0f;
  208. _pointerImageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.f_width-width)/2, 0.0f, width, height)];
  209. _pointerImageView.backgroundColor = Color_Clear;
  210. _pointerImageView.image = ImageNamed(@"xy-chatroom-rotarytable-pointer");
  211. }
  212. return _pointerImageView;
  213. }
  214. - (UIButton *)centerButton {
  215. if (!_centerButton) {
  216. _centerButton = [UIButton createButtonTextColor:Color_White textFont:Font(12)];
  217. _centerButton.frame = CGRectMake(0, 0, 78.0f, 78.0f);
  218. _centerButton.center = self.backgroundImageView.center;
  219. [_centerButton setBackgroundImage:ImageNamed(@"xy-chatroom-rotarytable-center") forState:UIControlStateNormal];
  220. [_centerButton addTarget:self action:@selector(centerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  221. }
  222. return _centerButton;
  223. }
  224. - (UILabel *)hideTimeLabel {
  225. if (!_hideTimeLabel) {
  226. _hideTimeLabel = [UILabel createLabelTextColor:Color_White fount:Font(12)];
  227. _hideTimeLabel.frame = CGRectMake(0, 0, 78.0f, 15.0f);
  228. _hideTimeLabel.center = CGPointMake(self.backgroundImageView.centerX, self.backgroundImageView.centerY+12.0f);
  229. _hideTimeLabel.textAlignment = NSTextAlignmentCenter;
  230. }
  231. return _hideTimeLabel;
  232. }
  233. @end