用法:
先添加指针视图,转盘背景和开始按钮。按钮的点击事件里设置开始动画,在动画开始的代理方法中让开始按钮不响应点击事件,在动画结束的代理方法中弹出中奖结果并让开始按钮响应点击事件。
- (void) beginRoundRotate {
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
[anim setFromValue:[NSNumber numberWithFloat:M_PI*2 * orign]];
orign = randoms / ITEM_COUNT ;
[anim setToValue:[NSNumber numberWithFloat:M_PI * 2*( ROTATION_EXTEND +orign)]];
//-M_PI*2/ITEM_COUNT
//ROTATION_EXTEND +
anim.duration = ANIM_TIME;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[anim setDelegate:self];
[anim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[_btnIndex.layer addAnimation:anim forKey:@"rotation"];
}
具体代码:
.h代码
#import@interface SAViewController : UIViewController
/** 旋转的位置 */
@property (nonatomic, strong) UISegmentedControl *segmentedItem;
/** 旋转的方向 */
@property (nonatomic, strong) UISegmentedControl *segmentedDirection;
/** 提示文本 */
@property (nonatomic, strong) UILabel *lblResult;
/** 底盘 */
@property (nonatomic, strong) UIImageView *imgRound;
/** 指针 */
@property (nonatomic, strong) UIButton *btnIndex;
/** 触发的按钮 */
@property (nonatomic, strong) UIButton *DidBtns;
@property (nonatomic ,strong) NSString *ZhuanPanturn;
@end
.m代码
//#import "SAViewController.h"
#import "CCActivityHUD.h"
#import "ZhongjiangDetailViewController.h"
const static CGFloat ANIM_TIME = 3.0; // 动画时间
const static CGFloat ROTATION_EXTEND = 10; // 旋转圈数延长 // 2 * M_PI = 1圈
const static NSInteger ITEM_COUNT = 7; // 转盘等比分割
@interface SAViewController (){
UIScrollView *bigScrollView;
UIAlertView *alert;
float randoms; // 位置,
float orign; // 角度
UILabel *JX_Lab;
//奖项展示
UILabel *_oneLab;
UILabel *_twoLab;
UILabel *_threeLab;
UILabel *_fourLab;
UILabel *_fiveLab;
UILabel *_sixLab;
UILabel *_SevenLab;
//具体奖项
UILabel *_oneDetailLab;
UILabel *_twoDetailLab;
UILabel *_threeDetailLab;
UILabel *_fourDetailLab;
UILabel *_fiveDetailLab;
UILabel *_sixDetailLab;
UILabel *_SevenDetailLab;
//声明
UILabel *_shengmingLab;
}
@property (strong, nonatomic) CCActivityHUD *activityHUD;
@end
@implementation SAViewController
-(void)loadView
{
[super loadView];
[self.navigationController setNavigationBarHidden:NO animated:YES];
return;
}
-(void)viewWillAppear:(BOOL)animated{
[self requFenPei];
}
-(void)requFenPei{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:JiangXiangFenpPeiUrl parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *content = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"content==%@",content);
_ZhuanPanturn = content[@"consume"];
//奖项分配显示
if ([content[@"error"]integerValue]==1) {
_oneDetailLab.text = content[@"data"][@"a"];
_twoDetailLab.text = content[@"data"][@"b"];
_threeDetailLab.text = content[@"data"][@"c"];
_fourDetailLab.text = content[@"data"][@"d"];
_fiveDetailLab.text = content[@"data"][@"e"];
_sixDetailLab.text = content[@"data"][@"f"];
_SevenDetailLab.text =content[@"data"][@"g"];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}//点击
-(void)request{
[self.activityHUD show];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:ChouJiangUrl parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
//pp/turn/get_v 确认转盘
// 返回: switch,1容许,0不容许
// prize 奖项 1234567
//
// $return['error']=0;
// $return['msg']="您的余额不足,请及时充值!";
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[self.activityHUD dismiss];
NSDictionary *content = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"content==%@",content);
if ([content[@"error"]integerValue]==1)
{
if (content[@"prize"] != nil) {
randoms = [content[@"prize"] intValue]-1 ;
// NSLog(@"randoms is %f",randoms);
[self beginRoundRotate];
}
}else{
_DidBtns.userInteractionEnabled = YES;
alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:content[@"msg"]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[self.activityHUD dismiss];
alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"网络连接错误"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.activityHUD = [CCActivityHUD new];
self.activityHUD.isTheOnlyActiveView = NO;
self.title = @"幸运大转盘";
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.automaticallyAdjustsScrollViewInsets = NO;
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:108/255.0 green:254/255.0 blue:244/255.0 alpha:1];
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
//
// UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:(UIBarButtonItemStyleDone) target:self action:nil];
// self.navigationItem.backBarButtonItem = backItem;
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithImage:nil style:(UIBarButtonItemStyleDone) target:self action:@selector(pushAction)];
[button setTitle:@"中奖纪录"];
self.navigationItem.rightBarButtonItem = button;
// self.navigationController.navigationBar.barTintColor = MainColor;
// [self.navigationController.navigationBar setTitleTextAttributes:
// @{NSFontAttributeName:MainFont(19),NSForegroundColorAttributeName:[UIColor whiteColor]}];
// UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:(UIBarButtonItemStyleDone) target:self action:nil];
// self.navigationItem.backBarButtonItem = backItem;
bigScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
bigScrollView.bounces = NO;
bigScrollView.contentSize = CGSizeMake(KScreenWidth, KScreenHeight+150);
// bigScrollView.scrollEnabled = YES;
bigScrollView.showsVerticalScrollIndicator = YES;
[self.view addSubview:bigScrollView];
//
// NSInteger orign_Y = 660;
//// [bigScrollView setContentSize:CGSizeMake(0, orign_Y)];
//
// int width = [UIScreen mainScreen].bounds.size.height;
//
// if (orign_Y < width) {
//
// bigScrollView.scrollEnabled = NO;
// }else
// {
// bigScrollView.scrollEnabled = YES;
// }
UIImageView *bgImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"beiji"]];
bgImg.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight*2);
bgImg.userInteractionEnabled = YES;
[bigScrollView addSubview:bgImg];
_shengmingLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 67, KScreenWidth-20, 15)];
_shengmingLab.text = @"声明: 本次抽奖活动与苹果公司无关";
_shengmingLab.textColor = [UIColor redColor];
_shengmingLab.font = [UIFont systemFontOfSize:15];
_shengmingLab.textAlignment = NSTextAlignmentCenter;
[bgImg addSubview:_shengmingLab];
[self addView];
[self initUI];
}
-(void)pushAction
{
ZhongjiangDetailViewController * zhongjiang = [[ZhongjiangDetailViewController alloc]init];
[self.navigationController pushViewController:zhongjiang animated:NO];
}
/** 添加控件 */
- (void) addView {
// _imgRound = [[UIImageView alloc]initWithFrame:CGRectMake(10, 74, MainScreen_width-20, MainScreen_width-20)];
_imgRound = [[UIImageView alloc]initWithFrame:CGRectMake(KScreenWidth/2-150, 80+10, 300, 300)];
_imgRound.image = [UIImage imageNamed:@"zhuanpan"];
[bigScrollView addSubview:_imgRound];
_btnIndex = [UIButton buttonWithType:UIButtonTypeCustom];
_btnIndex.frame = CGRectMake(KScreenWidth/2-30, 110+80+10, 60, 80);
[_btnIndex setImage:[UIImage imageNamed:@"zhen-1"] forState:UIControlStateNormal];
_btnIndex.adjustsImageWhenHighlighted = NO;
[bigScrollView addSubview:_btnIndex];
//触发的按钮
_DidBtns = [UIButton buttonWithType:UIButtonTypeCustom];
_DidBtns.frame = CGRectMake(KScreenWidth/2-30, 374+10+20, 60, 60);
_DidBtns.userInteractionEnabled = YES;
_DidBtns.backgroundColor = [UIColor clearColor];
[_DidBtns setImage:[UIImage imageNamed:@"anniu"] forState:UIControlStateNormal];
[_DidBtns addTarget:self action:@selector(didClick) forControlEvents:UIControlEventTouchUpInside];
[bigScrollView addSubview:_DidBtns];
UILabel *qLab = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth/2-50, 374+10+10+60+6+13, 100, 15)];
qLab.font = [UIFont systemFontOfSize:14];
qLab.text = @"点击启动";
qLab.textColor = [UIColor redColor];
qLab.textAlignment = NSTextAlignmentCenter;
[bigScrollView addSubview:qLab];
JX_Lab = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth/2-50, 394+60+15+10+19, 100, 15)];
JX_Lab.font =[UIFont systemFontOfSize:16];
JX_Lab.text = @"奖项说明";
JX_Lab.textColor = [UIColor grayColor];
JX_Lab.textAlignment = NSTextAlignmentCenter;
[bigScrollView addSubview:JX_Lab];
}
//初始化UI
-(void)initUI
{
int interval_x = KScreenWidth/2-140;
int interval_y = 394+60+15+10+10+25;
int interval_detail_x = KScreenWidth/2-80;
_oneLab = [[UILabel alloc]initWithFrame:CGRectMake(interval_x, interval_y, 60, 30)];
_oneLab.text = @"一等奖:";
_oneLab.textColor = [UIColor whiteColor];
_oneLab.font = [UIFont systemFontOfSize:16];
_oneLab.backgroundColor = [UIColor clearColor];
_oneLab.textAlignment = NSTextAlignmentCenter;
[bigScrollView addSubview:_oneLab];
//一等奖奖项
_oneDetailLab = [[UILabel alloc]initWithFrame:CGRectMake(interval_detail_x, interval_y, 60, 30)];
// _oneDetailLab.text = @"2000";
_oneDetailLab.textColor = [UIColor grayColor];
_oneDetailLab.font = [UIFont systemFontOfSize:16];
_oneDetailLab.textAlignment = NSTextAlignmentCenter;
_oneDetailLab.backgroundColor = [UIColor clearColor];
[bigScrollView addSubview:_oneDetailLab];
_threeLab = [[UILabel alloc]initWithFrame:CGRectMake(interval_x, interval_y+40, 60, 30)];
_threeLab.text = @"三等奖:";
_threeLab.textColor = [UIColor whiteColor];
_threeLab.font = [UIFont systemFontOfSize:16];
_threeLab.textAlignment = NSTextAlignmentCenter;
[bigScrollView addSubview:_threeLab];
//三等奖奖项
_threeDetailLab = [[UILabel alloc]initWithFrame:CGRectMake(interval_detail_x, interval_y+40, 60, 30)];
// _threeDetailLab.text = @"500";
_threeDetailLab.textColor = [UIColor grayColor];
_threeDetailLab.font = [UIFont systemFontOfSize:16];
_threeDetailLab.textAlignment = NSTextAlignmentCenter;
_threeDetailLab.backgroundColor = [UIColor clearColor];
[bigScrollView addSubview:_threeDetailLab];
_fiveLab = [[UILabel alloc]initWithFrame:CGRectMake(interval_x, interval_y+80, 60, 30)];
_fiveLab.text = @"五等奖:";
_fiveLab.textColor = [UIColor whiteColor];
_fiveLab.font = [UIFont systemFontOfSize:16];
_fiveLab.textAlignment = NSTextAlignmentCenter;
[bigScrollView addSubview:_fiveLab];
//五等奖奖项
_fiveDetailLab = [[UILabel alloc]initWithFrame:CGRectMake(interval_detail_x, interval_y+80, 60, 30)];
// _fiveDetailLab.text = @"100";
_fiveDetailLab.textColor = [UIColor grayColor];
_fiveDetailLab.font = [UIFont systemFontOfSize:16];
_fiveDetailLab.textAlignment = NSTextAlignmentCenter;
_fiveDetailLab.backgroundColor = [UIColor clearColor];
[bigScrollView addSubview:_fiveDetailLab];
int interval_r_x = KScreenWidth/2+20;
int interval_detail_r_x = KScreenWidth/2+80;
_twoLab = [[UILabel alloc]initWithFrame:CGRectMake(interval_r_x, interval_y, 60, 30)];
_twoLab.text = @"二等奖:";
_twoLab.textColor = [UIColor whiteColor];
_twoLab.font = [UIFont systemFontOfSize:16];
_twoLab.backgroundColor = [UIColor clearColor];
_twoLab.textAlignment = NSTextAlignmentCenter;
[bigScrollView addSubview:_twoLab];
//二等奖奖项
_twoDetailLab = [[UILabel alloc]initWithFrame:CGRectMake(interval_detail_r_x, interval_y, 60, 30)];
// _twoDetailLab.text = @"1000";
_twoDetailLab.textColor = [UIColor grayColor];
_twoDetailLab.font = [UIFont systemFontOfSize:16];
_twoDetailLab.textAlignment = NSTextAlignmentCenter;
_twoDetailLab.backgroundColor = [UIColor clearColor];
[bigScrollView addSubview:_twoDetailLab];
_fourLab = [[UILabel alloc]initWithFrame:CGRectMake(interval_r_x, interval_y+40, 60, 30)];
_fourLab.text = @"四等奖:";
_fourLab.textColor = [UIColor whiteColor];
_fourLab.font = [UIFont systemFontOfSize:16];
_fourLab.backgroundColor = [UIColor clearColor];
_fourLab.textAlignment = NSTextAlignmentCenter;
[bigScrollView addSubview:_fourLab];
//四等奖奖项
_fourDetailLab = [[UILabel alloc]initWithFrame:CGRectMake(interval_detail_r_x, interval_y+40, 60, 30)];
// _fourDetailLab.text = @"200";
_fourDetailLab.textColor = [UIColor grayColor];
_fourDetailLab.font = [UIFont systemFontOfSize:16];
_fourDetailLab.textAlignment = NSTextAlignmentCenter;
_fourDetailLab.backgroundColor = [UIColor clearColor];
[bigScrollView addSubview:_fourDetailLab];
_sixLab = [[UILabel alloc]initWithFrame:CGRectMake(interval_r_x, interval_y+80, 60, 30)];
_sixLab.text = @"六等奖:";
_sixLab.textColor = [UIColor whiteColor];;
_sixLab.font = [UIFont systemFontOfSize:16];
_sixLab.backgroundColor = [UIColor clearColor];
_sixLab.textAlignment = NSTextAlignmentCenter;
[bigScrollView addSubview:_sixLab];
//六等奖奖项
_sixDetailLab = [[UILabel alloc]initWithFrame:CGRectMake(interval_detail_r_x, interval_y+80, 60, 30)];
// _sixDetailLab.text = @"50";
_sixDetailLab.textColor = [UIColor grayColor];;
_sixDetailLab.font = [UIFont systemFontOfSize:16];
_sixDetailLab.textAlignment = NSTextAlignmentCenter;
_sixDetailLab.backgroundColor = [UIColor clearColor];
[bigScrollView addSubview:_sixDetailLab];
// interval_x, interval_y,
// interval_detail_x, interval_y+40
_SevenLab = [[UILabel alloc]initWithFrame:CGRectMake(interval_x, interval_y+120, 60, 30)];
_SevenLab.text = @"七等奖:";
_SevenLab.textColor = [UIColor whiteColor];;
_SevenLab.font = [UIFont systemFontOfSize:16];
_SevenLab.backgroundColor = [UIColor clearColor];
_SevenLab.textAlignment = NSTextAlignmentCenter;
[bigScrollView addSubview:_SevenLab];
//六等奖奖项
_SevenDetailLab = [[UILabel alloc]initWithFrame:CGRectMake(interval_detail_x, interval_y+120, 60, 30)];
// _sixDetailLab.text = @"50";
_SevenDetailLab.textColor = [UIColor grayColor];;
_SevenDetailLab.font = [UIFont systemFontOfSize:16];
_SevenDetailLab.textAlignment = NSTextAlignmentCenter;
_SevenDetailLab.backgroundColor = [UIColor clearColor];
[bigScrollView addSubview:_SevenDetailLab];
}
/** 模拟从服务器请求结果 */
#warning 这里是数据请求
- (void) getResult {
// 产生随机数,作为停止项
// 这里从0开始 0-ITEM_COUNT-1
// randoms = (arc4random() % ITEM_COUNT)+1;
NSLog(@"----%f",randoms);
// randoms = 5+1;
_lblResult.text = [NSString stringWithFormat:@"停止项=%.0lf", randoms];
NSLog(@"停止项是:%f",randoms);
[self beginRoundRotate];
}
//触发抽奖动画的按钮
-(void)didClick
{
_DidBtns.userInteractionEnabled = NO;
NSString *mess = [NSString stringWithFormat:@"您将消耗%@金币",_ZhuanPanturn];
UIAlertController *alertview = [UIAlertController alertControllerWithTitle:@"提示" message:mess preferredStyle:(UIAlertControllerStyleAlert)];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self request];
}];
[alertview addAction:cancel];
[self presentViewController:alertview animated:YES completion:nil];
// [self getResult];
}
/** 开始旋转-底盘 */
- (void) beginRoundRotate {
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
[anim setFromValue:[NSNumber numberWithFloat:M_PI*2 * orign]];
orign = randoms / ITEM_COUNT ;
[anim setToValue:[NSNumber numberWithFloat:M_PI * 2*( ROTATION_EXTEND +orign)]];
//-M_PI*2/ITEM_COUNT
//ROTATION_EXTEND +
anim.duration = ANIM_TIME;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[anim setDelegate:self];
[anim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[_btnIndex.layer addAnimation:anim forKey:@"rotation"];
}
/** 动画开始 */
- (void)animationDidStart:(CAAnimation *)anim {
[self.activityHUD dismiss];
_btnIndex.userInteractionEnabled = NO;
_segmentedItem.userInteractionEnabled = NO;
_segmentedDirection.userInteractionEnabled = NO;
_DidBtns.userInteractionEnabled = NO;
}
/** 动画结束 */
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
[self.activityHUD dismiss];
_btnIndex.userInteractionEnabled = YES;
_segmentedItem.userInteractionEnabled = YES;
_segmentedDirection.userInteractionEnabled = YES;
_DidBtns.userInteractionEnabled = YES;
int a ;
a = (int)randoms+1;
NSString *mesStr = [NSString stringWithFormat:@"你抽中%d等奖", a];
NSLog(@"mesStr is %@--%d",mesStr,a);
if ([mesStr isEqualToString:@"你抽中1等奖"]) {
if (!alert) {
alert = [[UIAlertView alloc]initWithTitle:@"恭喜"
message:@""
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
}
alert.message = @"你抽中了一等奖";
[alert show];
}else if ([mesStr isEqualToString:@"你抽中2等奖"]) {
if (!alert) {
alert = [[UIAlertView alloc]initWithTitle:@"恭喜"
message:@""
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
}
alert.message = @"你抽中了二等奖";
[alert show];
}else if ([mesStr isEqualToString:@"你抽中3等奖"]) {
if (!alert) {
alert = [[UIAlertView alloc]initWithTitle:@"恭喜"
message:@""
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
}
alert.message = @"你抽中了三等奖";
[alert show];
}else if ([mesStr isEqualToString:@"你抽中4等奖"]) {
if (!alert) {
alert = [[UIAlertView alloc]initWithTitle:@"恭喜"
message:@""
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
}
alert.message = @"你抽中了四等奖";
[alert show];
}else if ([mesStr isEqualToString:@"你抽中5等奖"]) {
if (!alert) {
alert = [[UIAlertView alloc]initWithTitle:@"恭喜"
message:@""
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
}
alert.message = @"你抽中了五等奖";
[alert show];
}else if ([mesStr isEqualToString:@"你抽中6等奖"]) { //错开一位,"抽中0等奖",展示1
if (!alert) {
alert = [[UIAlertView alloc]initWithTitle:@"恭喜"
message:@""
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
}
alert.message = @"你抽中了六等奖";
[alert show];
}else if ([mesStr isEqualToString:@"你抽中7等奖"]) { //错开一位,"抽中0等奖",展示1
if (!alert) {
alert = [[UIAlertView alloc]initWithTitle:@"恭喜"
message:@""
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
}
alert.message = @"你抽中了七等奖";
[alert show];
}
}
-(void)viewWillDisappear:(BOOL)animated{
[self.activityHUD dismiss];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
网友评论