Files
mier_ios/SweetParty/主类/RCMic/Room/Box/第二个转盘/SecondTurntableView.m
2025-08-11 10:43:19 +08:00

171 lines
6.1 KiB
Objective-C
Executable File

//
// SecondTurntableView.m
// YDXTurntable
//
// Created by LIN on 16/11/26.
// Copyright © 2016年 YDX. All rights reserved.
//
#import "SecondTurntableView.h"
#import "BJRoomGiftModel.h"
#import "YZXPieGraphView.h"
#define turnScale_W self.bounds.size.width/300
#define turnScale_H self.bounds.size.height/300
@interface SecondTurntableView ()<CAAnimationDelegate>
@property (nonatomic, strong) UIImageView *rotateWheel;//转盘背景
@property (nonatomic, strong) NSArray<BJRoomGiftModel *> *giftArr; //原始数据
@property (nonatomic, assign) NSInteger resultIndex;
@property (nonatomic, strong) YZXPieGraphView *pieGraphView;
@end
@implementation SecondTurntableView
{
CGRect _viewFrame;
}
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_viewFrame = frame;
}
return self;
}
- (void)loadPoolDataWithType:(NSInteger)type {
NSDictionary *dict = @{@"tid":@(type)};
[BJHttpTool getAwardJiangChiListWithParameters:dict success:^(id response) {
if ([response[@"code"] intValue] == 200) {
NSArray *arr = [BJRoomGiftModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
if (arr.count > 14) {
self.giftArr = [arr subarrayWithRange:NSMakeRange(0, 14)];
}else {
self.giftArr = arr;
}
[self initUI];
}
} failure:^(NSError *error) {
MYLog(@"获取转盘奖池失败");
}];
}
- (void)initUI {
if (self.giftArr.count == 0) {
return;
}
// 转盘
self.rotateWheel = [[UIImageView alloc] initWithFrame:self.bounds];
[self addSubview:self.rotateWheel];
[ControlCreator createImageView:self.rotateWheel rect:CGRectMake(24, 24, 252, 252) imageName:@"sec_zhuan_nei_1" backguoundColor:nil];
// [ControlCreator createImageView:self.rotateWheel rect:CGRectMake(84, 84, 132, 132) imageName:@"zhuan_nei_2" backguoundColor:nil];
// NSMutableArray *percentagesArr = [NSMutableArray arrayWithCapacity:0];
// NSMutableArray *colorsArr = [NSMutableArray arrayWithCapacity:0];
// double single = 100.0/self.giftArr.count;
// NSArray *defaultColors = @[kClearColor, kClearColor];
// for (NSInteger i = 0; i<self.giftArr.count; i++) {
// [percentagesArr addObject:[NSString stringWithFormat:@"%.2f", single]];
// [colorsArr addObject:defaultColors[i%2]];
// }
//
// //扇形背景色
// _pieGraphView = [[YZXPieGraphView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)
// withTitleData:nil
// withPercentageData:[percentagesArr copy]
// withColors:[colorsArr copy]];
// _pieGraphView.backgroundColor = [UIColor clearColor];
// _pieGraphView.circleRadius = 80;
// _pieGraphView.hideTitlt = YES;
// _pieGraphView.hideAnnotation = YES;
// [self.rotateWheel addSubview:_pieGraphView];
NSInteger count = _giftArr.count;
for (int i = 0; i <_giftArr.count; i ++) {
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, M_PI * 300.0/_giftArr.count, 150)];
bgView.layer.anchorPoint = CGPointMake(0.5, 1);
bgView.center = CGPointMake(150, 150);
CGFloat angle = 0;
if (0 == i) {
angle = 0;
}else {
angle = M_PI * 2.0 / count * (_giftArr.count-i);
}
// CGFloat angle = M_PI * 2.0 / count * i;
bgView.transform = CGAffineTransformMakeRotation(angle);
[self.rotateWheel addSubview:bgView];
BJRoomGiftModel *model = (BJRoomGiftModel *)_giftArr[i];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(M_PI * 300.0/count/2.0-15, 10, 30, 30)];
imageView.layer.cornerRadius = 15;
imageView.layer.masksToBounds = YES;
[imageView sd_setImageWithURL:[NSURL URLWithString:model.base_image]];
[bgView addSubview:imageView];
UIImageView *itemBgV = [[UIImageView alloc] initWithImage:ImageNamed(@"sec_zhuan_item_border")];
[bgView insertSubview:itemBgV atIndex:0];
[itemBgV mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(imageView);
}];
UILabel *titleLab = [ControlCreator createLabel:nil rect:CGRectMake(0, 50, M_PI * 300.0/_giftArr.count, 25) text:model.gift_price font:YBMediumFont(12) color:HEXCOLOR(0x34280B) backguoundColor:nil align:NSTextAlignmentCenter lines:1];
[bgView addSubview:titleLab];
}
}
-(void)startAnimationWithResultGid:(NSString *)gid {
NSInteger resultIndex = 0;
for (NSInteger i = 0; i < self.giftArr.count; i++) {
BJRoomGiftModel *model = self.giftArr[i];
if ([gid isEqualToString:model.gid]) {
resultIndex = i;
}
}
if (resultIndex != 0) {
resultIndex = self.giftArr.count - resultIndex;
}
self.resultIndex = resultIndex;
NSInteger turnAngle = 0;
NSInteger turnsNum = 4;//控制圈数
if (self.giftArr.count > 0) {
turnAngle = - (360/self.giftArr.count * resultIndex);
}
CGFloat perAngle = M_PI/180.0;
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat:turnAngle * perAngle + 360 * perAngle * turnsNum];
rotationAnimation.duration = 3.0f;
rotationAnimation.cumulative = YES;
rotationAnimation.delegate = self;
//由快变慢
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
rotationAnimation.fillMode=kCAFillModeForwards;
rotationAnimation.removedOnCompletion = NO;
[self.rotateWheel.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
//转盘停止转动代理方法
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
if ([self.delegate respondsToSelector:@selector(turnTableViewDidFinish)]) {
[self.delegate turnTableViewDidFinish];
}
}
@end