转盘抽奖

做一个转盘抽奖



这里是网上找的一个转盘图,自己再随便弄了一个指针,丑了点,别见怪。

主要功能就是点击抽奖,转盘开始旋转,然后渐渐停止,指针指向哪一格就弹出提示中了几等奖,很简单的demo,不是吗?


这里使用最基本的Single View Application模板,不用管其它多余的,直接进入BIDViewController.h,声明几个2个UIImageView和2个Float对象

//
//  BIDViewController.h
//  RotationLottery
//
//  Created by xuhaote on 14-8-19.
//  Copyright (c) 2014年 FREE. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface BIDViewController : UIViewController{
    UIImageView *ivRotation,*ivPointer;
    float random;
    float position;
}

@end

下面就是BIDViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    //添加转盘图片
    UIImageView *image_rotation = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rotation.jpg"]];
    image_rotation.frame = CGRectMake(10, 20, 300, 300);
    ivRotation = image_rotation;
    [self.view addSubview:ivRotation];
    
    //添加指针图片
    UIImageView *image_pointer = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pointer.png"]];
    image_pointer.frame = CGRectMake(0, 160, 40, 5);
    ivPointer = image_pointer;
    [self.view addSubview:ivPointer];
    
    //添加开始按钮
    UIButton *btn_start = [UIButton buttonWithType:UIButtonTypeSystem];
    btn_start.frame = CGRectMake(125.0, 350.0, 70, 70);
    [btn_start setTitle:@"抽奖" forState:UIControlStateNormal];
    [btn_start addTarget:self action:@selector(rotation) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn_start];
    
}

这里我依次为view添加2个UIImageView和一个UIButton,使用CGRectMake方法设置位置,再为button设置旋转动画事件


//抽奖按钮,执行旋转动画
- (void) rotation{
    //**********************旋转动画******************************
    //产生随机角度
    srand((unsigned)time(0));  //不加这句会导致每次随机数一样
    random = (rand() % 20) / 10.0;
   
    //random = arc4random() %20 /10.0;
    //设置动画
    CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    [anima setFromValue:[NSNumber numberWithFloat:M_PI * (0.0 + position)]];
    [anima setToValue:[NSNumber numberWithFloat:M_PI * (10.0 + random + position)]];
    [anima setDuration:2.5];
    [anima setDelegate:self];
    
    //速度控制
    [anima setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
    //添加动画
    [[ivRotation layer] addAnimation:anima forKey:nil];
    //锁定结束位置
    ivRotation.transform = CGAffineTransformMakeRotation(M_PI * (10.0 + random + position));
    
    //锁定formValue的位置
    
    position = 10.0 + random + position;
    NSLog(@"--->%f",position);
    position = fmodf(position, 2.0);
}

这里首先产生一个随机数,用于转盘的旋转角度

这里我尝试使用了srand和arc4random2种方式产生随机数

接着我们开始设置动画,使用CABasicAnimation 设置它的动画类型为transform.rotation(旋转角度)具体的一些动画效果:http://blog.csdn.net/mad2man/article/details/17554621

这里设置fromValue起始位置和toValue达到位置,动画时间

setDelegate 设置代理为self,这样我们就可以使用BIDViewController.m的animationDidStop:finish: 函数来弹出提示框

接下来设置速度控制器再添加动画,锁定结束位置为toValue,这样动画结束后转盘就不会恢复原样,最后锁定position。   

这里要注意的是,设置Value  M_PI * 2为旋转一整圈,因此这里position % 2

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    //判断抽奖结果
    NSLog(@"%f",position);
    NSString *message;
    if (position >= 0.0 && position < (2.0 / 20.0 + 0.02)) {
        message = [NSString stringWithFormat:@"您中了 %d 等奖",9];
    }else if (position >= (2.0 / 20.0 + 0.02) && position < (2.0 / 20.0 * 3 + 0.02)){
        message = [NSString stringWithFormat:@"您中了 %d 等奖",8];
    }else if (position >= (2.0 / 20.0 * 3 + 0.02) && position < (2.0 / 20.0 * 5 + 0.02)){
        message = [NSString stringWithFormat:@"您中了 %d 等奖",7];
    }else if (position >= (2.0 / 20.0 * 5 + 0.02) && position < (2.0 / 20.0 * 7 + 0.02)){
        message = [NSString stringWithFormat:@"您中了 %d 等奖",6];
    }else if (position >= (2.0 / 20.0 * 7 + 0.02) && position < (2.0 / 20.0 * 9 + 0.02)){
        message = [NSString stringWithFormat:@"您中了 %d 等奖",5];
    }else if (position >= (2.0 / 20.0 * 9 + 0.02) && position < (2.0 / 20.0 * 11 + 0.02)){
        message = [NSString stringWithFormat:@"您中了 %d 等奖",4];
    }else if (position >= (2.0 / 20.0 * 11 + 0.02) && position < (2.0 / 20.0 * 13 + 0.02)){
        message = [NSString stringWithFormat:@"您中了 %d 等奖",3];
    }else if (position >= (2.0 / 20.0 * 13 + 0.02) && position < (2.0 / 20.0 * 15 + 0.02)){
        message = [NSString stringWithFormat:@"您中了 %d 等奖",2];
    }else if (position >= (2.0 / 20.0 * 15 + 0.02) && position < (2.0 / 20.0 * 17 + 0.02)){
        message = [NSString stringWithFormat:@"您中了 %d 等奖",1];
    }else if (position >= (2.0 / 20.0 * 17 + 0.02) && position < (2.0 / 20.0 * 19 + 0.02)){
        message = [NSString stringWithFormat:@"您中了 %d 等奖",10];
    }else if (position >= (2.0 / 20.0 * 19 + 0.02) && position < (2.0 / 20.0 * 20)){
        message = [NSString stringWithFormat:@"您中了 %d 等奖",9];
    }
    
    
    UIAlertView *result = [[UIAlertView alloc] initWithTitle:@"恭喜中奖!" message:message delegate:self cancelButtonTitle:@"领奖去!" otherButtonTitles: nil];
    [result show];
}

这里动画停止后判断旋转角度,由于我的指针并没有放在转盘垂直中间位置,而是稍微偏上一点,自己做的指针,前面不是尖的,所以就这样啦。

判断的时候,20个部分,由于偏上所以要+0.02,判断旋转了多少然后提示中奖




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值