当我们项目要搞活动,吸引用户时,抽奖不免为一种很好的方法,吸引用户的同时增添用户的乐趣,抽奖形式各式各样,有很多种,下面介一种转盘形式;
转盘抽奖就是让转盘转动,然后随机停止在某个位置,利用CABasicAnimation
基本动画使转盘转动,CABasicAnimation动画很简单,设置好相关属性就可以,也有停止动画的代理方法;要做到如上效果图,首先添加好背景图和Go按钮,由于是八等份,我们在每一等份添加抽奖设置:
#pragma mark 初始化View
-(void)initView{
//转盘背景
_bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-50,SCREEN_WIDTH-50)];
_bgImageView.center = self.view.center;
_bgImageView.image = [UIImage imageNamed:@"bg2"];
[self.view addSubview:_bgImageView];
//添加GO按钮图片
UIImageView *btnimage = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, SCREEN_HEIGHT/2-70, 100, 118)];
btnimage.image = [UIImage imageNamed:@"btn"];
[self.view addSubview:btnimage];
_bgImageView.userInteractionEnabled = YES;
btnimage.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(btnClick)];
[btnimage addGestureRecognizer:tap];
//添加文字
NSArray *_prizeArray = @[@"谢谢参与",@"一等奖",@"谢谢参与",@"二等奖",@"谢谢参与",@"三等奖",@"谢谢参与",@"特等奖"];
for (int i = 0; i < 8; i ++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,M_PI * CGRectGetHeight(_bgImageView.frame)/8,CGRectGetHeight(_bgImageView.frame)*3/5)];
label.layer.anchorPoint = CGPointMake(0.5, 1.0);
label.center = CGPointMake(CGRectGetHeight(_bgImageView.frame)/2, CGRectGetHeight(_bgImageView.frame)/2);
label.text = [NSString stringWithFormat:@"%@", _prizeArray[i]];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:14];
CGFloat angle = M_PI*2/8 * i;
label.transform = CGAffineTransformMakeRotation(angle);
[_bgImageView addSubview:label];
}
}
接下来就是要写动画了,首先理清一下思路,我们要让转盘转动肯定是转几圈,那是不是我们随机设置一个圈数就可以了?不是的,我们随机生成的不管多少圈都是回到了同一个位置!所以我们的思路是可以设定一个固定的圈数,然后再转01圈,就是0360度,我们在最后一圈产生随机的度数就可以了,由图可知,箭头一直朝上不动,转动的是背景转盘,箭头对着一块的中心,再接下来转的时候也让其对着某一块的中心,如特等奖对应着45度,三等奖对应着135度,以此类推。。。在我们iOS动画当中,是以弧度计算的,我们要相应转为度计算,1度=M_PI/180弧度;
#pragma mark 点击Go按钮
-(void)btnClick{
NSLog(@"点击Go");
//判断是否正在转
if (_isAnimation) {
return;
}
_isAnimation = YES;
//控制概率[0,80)
NSInteger lotteryPro = arc4random()%80;
//设置转圈的圈数
NSInteger circleNum = 6;
if (lotteryPro < 10) {
_circleAngle = 0;
}else if (lotteryPro < 20){
_circleAngle = 45;
}else if (lotteryPro < 30){
_circleAngle = 90;
}else if (lotteryPro < 40){
_circleAngle = 135;
}else if (lotteryPro < 50){
_circleAngle = 180;
}else if (lotteryPro < 60){
_circleAngle = 225;
}else if (lotteryPro < 70){
_circleAngle = 270;
}else if (lotteryPro < 80){
_circleAngle = 315;
}
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 = 3.0f;
rotationAnimation.cumulative = YES;
rotationAnimation.delegate = self;
//由快变慢
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
rotationAnimation.fillMode=kCAFillModeForwards;
rotationAnimation.removedOnCompletion = NO;
[_bgImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
我们设置了转的圈数是6圈,然后随机生成一个0~80的数字,每一个10对应转盘八等份的其中一份,然后设置最后一圈转的角度,这些概率都是自由设置的,你可以把一等奖中奖的概率设高点,我们这里谢谢参数有50%的概率,所以转到谢谢参与的机会比较多!所以CABasicAnimation的目标值toValue
设为_circleAngle * perAngle + 360 * perAngle * circleNum
,你应该很容易理解了,就是多少圈乘以360度再加上最后的度数。最后我们在动画停止的时候讨论最后转的度数再做相应的处理:
#pragma mark 动画结束
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
NSLog(@"动画停止");
NSString *title;
if (_circleAngle == 0) {
title = @"谢谢参与!";
}else if (_circleAngle == 45){
title = @"恭喜你,获得特等奖!";
}else if (_circleAngle == 90){
title = @"谢谢参与!";
}else if (_circleAngle == 135){
title = @"恭喜你,获得三等奖!";
}else if (_circleAngle == 180){
title = @"谢谢参与!";
}else if (_circleAngle == 225){
title = @"恭喜你,获得二等奖!";
}else if (_circleAngle == 270){
title = @"谢谢参与!";
}else if (_circleAngle == 315){
title = @"恭喜你,获得一等奖!";
}
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:title delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
转动后的效果图:
其实在我们实际项目当中,一些数据都是可以做活的,我这是静态写了几个数据。除了转盘形式得抽奖,还有一种老虎机形式用的也比较多,想要了解,请查阅我另一篇博客iOS--老虎机的抽奖实现
声明: 转载请注明出处http://www.jianshu.com/p/46bcfcf53055