123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- //
- // XYRotaryTableView.m
- // Timi
- //
- // Created by 翟玉磊 on 2021/4/14.
- //
- #import "XYRotaryTableView.h"
- @interface XYRotaryTableView ()<CAAnimationDelegate>
- @property (nonatomic, strong) UIImageView *backgroundImageView;
- @property (nonatomic, strong) UIImageView *itemBackgroundImageView;
- @property (nonatomic, strong) UIImageView *pointerImageView;
- @property (nonatomic, strong) UIButton *centerButton;
- @property (nonatomic, strong) UILabel *hideTimeLabel;
- @property (nonatomic, assign) NSInteger lotteryResult;
- @end
- @implementation XYRotaryTableView
- - (void)dealloc
- {
- [self clear];
- }
- #pragma mark — Public
- - (void)clear {
- // 删除layer动画 回归初始状态
- [self.itemBackgroundImageView.layer removeAllAnimations];
-
- for (UIView *view in self.itemBackgroundImageView.subviews) {
- [view removeFromSuperview];
- }
- for (UIView *view in self.subviews) {
- [view removeFromSuperview];
- }
- }
- /// 设置转盘奖项
- - (void)setupRotaryTableItems:(NSArray *)array {
-
- for (UIView *view in self.itemBackgroundImageView.subviews) {
- [view removeFromSuperview];
- }
-
- for (int i = 0; i < array.count; i ++) {
-
- UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, M_PI * self.itemBackgroundImageView.f_heigh/array.count,self.itemBackgroundImageView.f_heigh*3/5)];
- label.layer.anchorPoint = CGPointMake(0.5, 1.0);
- label.center = CGPointMake(self.itemBackgroundImageView.f_heigh/2, self.itemBackgroundImageView.f_heigh/2);
- label.text = [NSString stringWithFormat:@"%@", array[i]];
- label.textAlignment = NSTextAlignmentCenter;
- label.font = Font_B(14);
- // 字体颜色
- if (i%2) {
- label.textColor = ColorFromHexString(@"#AA7BFD");
- }else {
- label.textColor = ColorFromHexString(@"#ED68E2");
- }
- // 旋转角度
- CGFloat angle = M_PI*2/array.count * i;
- label.transform = CGAffineTransformMakeRotation(angle);
- [self.itemBackgroundImageView addSubview:label];
- }
-
- self.state = XYRotaryTableStateNormal;
- }
- /// 设置转盘结果 (类目索引)
- - (void)setupRotaryTableResult:(NSInteger)index {
-
- self.lotteryResult = index;
- self.state = XYRotaryTableStateRotating;
- [self startAnimation];
- }
- - (void)setupHideTime:(NSInteger)time {
- if (self.enabled) {
-
- }else {
- self.hideTimeLabel.text = [NSString stringWithFormat:@"%lds", time];
- }
- }
- #pragma mark — Action
- // 开始抽奖
- - (void)centerButtonAction:(id)sender {
-
- self.centerButton.userInteractionEnabled = NO;
- if (self.delegate && [self.delegate respondsToSelector:@selector(turntableLotteryWithRotaryTable:)]) {
- [self.delegate turntableLotteryWithRotaryTable:self];
- }
- }
- - (void)startAnimation {
-
- //设置转圈的圈数
- NSInteger circleNum = 6;
-
- CGFloat circleAngle = 0;
-
- if (_lotteryResult == 1) {
- circleAngle = 0;
- }else if (_lotteryResult == 2){
- circleAngle = 315;
- }else if (_lotteryResult == 3){
- circleAngle = 270;
- }else if (_lotteryResult == 4){
- circleAngle = 225;
- }else if (_lotteryResult == 5){
- circleAngle = 180;
- }else if (_lotteryResult == 6){
- circleAngle = 135;
- }else if (_lotteryResult == 7){
- circleAngle = 90;
- }else if (_lotteryResult == 8){
- circleAngle = 45;
- }
- // /*
- // 1.因为目前有8等分 一个圆周分成8份就是一份45
- // */
- // circleAngle = 45 * (self.lotteryResult-1)/*后面中奖索引是以1开始的 所以这里要减1*/;
-
- CGFloat perAngle = M_PI/180.0;
-
- NSLog(@"turnAngle = %ld",(long)circleAngle);
-
- CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
- rotationAnimation.toValue = [NSNumber numberWithFloat:circleAngle * perAngle + 360 * perAngle * circleNum];
- rotationAnimation.duration = self.animationDuration;
- rotationAnimation.cumulative = YES;
- rotationAnimation.delegate = self;
-
- //由快变慢
- rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
- rotationAnimation.fillMode=kCAFillModeForwards;
- rotationAnimation.removedOnCompletion = NO;
- [self.itemBackgroundImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
-
- if (self.delegate && [self.delegate respondsToSelector:@selector(didBeginRotatingWithRotaryTable:)]) {
- [self.delegate didBeginRotatingWithRotaryTable:self];
- }
- }
- #pragma mark — CAAnimationDelegate
- - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
- NSLog(@"动画停止");
-
- self.state = XYRotaryTableStateFinish;
-
- if (self.delegate && [self.delegate respondsToSelector:@selector(didEndRotatingWithRotaryTable:)]) {
- [self.delegate didEndRotatingWithRotaryTable:self];
- }
- }
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self initValue];
- [self setupUI];
- }
- return self;
- }
- - (void)initValue {
- self.enabled = YES;
- self.animationDuration = 3;
- }
- #pragma mark — UI
- - (void)setupUI {
-
- self.backgroundColor = Color_Clear;
-
- [self addSubview:self.backgroundImageView];
- [self addSubview:self.itemBackgroundImageView];
- [self addSubview:self.pointerImageView];
- [self addSubview:self.centerButton];
- [self addSubview:self.hideTimeLabel];
- }
- - (void)setEnabled:(BOOL)enabled {
- _enabled = enabled;
- self.state = _state;
- }
- - (void)setState:(XYRotaryTableState)state {
- _state = state;
- self.hideTimeLabel.text = @"";
- switch (state) {
- case XYRotaryTableStateNormal:
- if (self.enabled) {
- self.centerButton.userInteractionEnabled = YES;
- [self.centerButton setTitle:@"点击摇奖" forState:UIControlStateNormal];
- }else {
- self.centerButton.userInteractionEnabled = NO;
- [self.centerButton setTitle:@"等待摇奖" forState:UIControlStateNormal];
- }
- // 删除layer动画 回归初始状态
- [self.itemBackgroundImageView.layer removeAllAnimations];
- break;
- case XYRotaryTableStateReady:
- self.centerButton.userInteractionEnabled = NO;
- [self.centerButton setTitle:@"启动中" forState:UIControlStateNormal];
- break;
- case XYRotaryTableStateRotating:
- self.centerButton.userInteractionEnabled = NO;
- [self.centerButton setTitle:@"摇奖中" forState:UIControlStateNormal];
- break;
- case XYRotaryTableStateFinish:
- if (self.enabled) {
- self.centerButton.userInteractionEnabled = YES;
- [self.centerButton setTitle:@"再来一局" forState:UIControlStateNormal];
- }else {
- self.centerButton.userInteractionEnabled = NO;
- [self.centerButton setTitle:@"已出奖" forState:UIControlStateNormal];
- }
- break;
- }
- }
- #pragma mark — Getter
- - (UIImageView *)backgroundImageView {
- if (!_backgroundImageView) {
- _backgroundImageView = [[UIImageView alloc] initWithFrame:self.bounds];
- _backgroundImageView.backgroundColor = Color_Clear;
- _backgroundImageView.image = ImageNamed(@"xy-chatroom-rotarytable-bg");
- }
- return _backgroundImageView;
- }
- - (UIImageView *)itemBackgroundImageView {
- if (!_itemBackgroundImageView) {
- _itemBackgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20.0f, 20.0f, self.backgroundImageView.f_width-40.0f, self.backgroundImageView.f_heigh-40.0f)];
- _itemBackgroundImageView.center = self.backgroundImageView.center;
- _itemBackgroundImageView.backgroundColor = Color_Clear;
- _itemBackgroundImageView.image = ImageNamed(@"xy-chatroom-rotarytable-item-bg");
- }
- return _itemBackgroundImageView;
- }
- - (UIImageView *)pointerImageView {
- if (!_pointerImageView) {
- CGFloat width = 50.0f;
- CGFloat height = 50.0f;
- _pointerImageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.f_width-width)/2, 0.0f, width, height)];
- _pointerImageView.backgroundColor = Color_Clear;
- _pointerImageView.image = ImageNamed(@"xy-chatroom-rotarytable-pointer");
- }
- return _pointerImageView;
- }
- - (UIButton *)centerButton {
- if (!_centerButton) {
- _centerButton = [UIButton createButtonTextColor:Color_White textFont:Font(12)];
- _centerButton.frame = CGRectMake(0, 0, 78.0f, 78.0f);
- _centerButton.center = self.backgroundImageView.center;
- [_centerButton setBackgroundImage:ImageNamed(@"xy-chatroom-rotarytable-center") forState:UIControlStateNormal];
- [_centerButton addTarget:self action:@selector(centerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _centerButton;
- }
- - (UILabel *)hideTimeLabel {
- if (!_hideTimeLabel) {
- _hideTimeLabel = [UILabel createLabelTextColor:Color_White fount:Font(12)];
- _hideTimeLabel.frame = CGRectMake(0, 0, 78.0f, 15.0f);
- _hideTimeLabel.center = CGPointMake(self.backgroundImageView.centerX, self.backgroundImageView.centerY+12.0f);
- _hideTimeLabel.textAlignment = NSTextAlignmentCenter;
- }
- return _hideTimeLabel;
- }
- @end
|