objective-c 抽奖转盘(原创)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了objective-c 抽奖转盘(原创)相关的知识,希望对你有一定的参考价值。

一、 说明

   转盘一共由四部分组成,底盘,旋转盘,转盘指针,开始按钮,全部贴在 self.view上,另外有12个展示中奖等级的UILabel贴在旋转盘上,动画使用CABasecAnimation类实现。 

二、代码 

@interface AViewController ()
{
    UIButton *centerButton;
    
    CGAffineTransform rotatePointView ;
    UIImageView *pointView;
    UIImageView *backImageView;
    UIImageView *secondImageView;
    UIImageView *ImageView;
    /*动画*/
    CABasicAnimation *animationSecond ;
    CABasicAnimation *animationPoint ;
}


@end

@implementation  AViewController

 
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    ImageView=[[UIImageView alloc]initWithFrame:CGRectMake(DEVICE_Width/2-(DEVICE_Width*0.97/2), DEVICE_Height/2-(DEVICE_Width*0.97/2), DEVICE_Width*0.97, DEVICE_Width*0.97 )];
    
    [ImageView setBackgroundColor:[UIColor clearColor]];
    ImageView.image=[UIImage imageNamed:@"LuckyBaseBackground"];
    
  [self.view addSubview:ImageView];
    
     secondImageView=[[UIImageView alloc]initWithFrame:CGRectMake(DEVICE_Width/2-(DEVICE_Width*0.92/2), DEVICE_Height/2-(DEVICE_Width*0.92/2), DEVICE_Width*0.92, DEVICE_Width*0.92 )];
    
    [secondImageView setBackgroundColor:[UIColor clearColor]];
    secondImageView.image=[UIImage imageNamed:@"LuckyRotateWheel"];
    secondImageView.center=ImageView.center;
    
    animationSecond = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animationSecond.duration=7;
    animationSecond.repeatCount = 1;
    animationSecond.fromValue = [NSNumber numberWithFloat:0.0]; // 起始角度
    animationSecond.toValue = [NSNumber numberWithFloat:55 * M_PI]; // 终止角度
   [self.view   addSubview:secondImageView];
    
    NSArray   *[email protected][@"一等奖",@"错过",@"二等奖",@"三等奖",@"四等奖",@"五等奖",@"错过",@"六等奖",@"七等奖",@"八等奖",@"九等奖",@"十等奖"];
    for(int i=0;i< encourageArray.count;i++)
    {
       UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(secondImageView.frame.size.width/2-15,secondImageView.frame.size.height/2-secondImageView.frame.size.height/2, 30, secondImageView.frame.size.height )];
        titleLabel.backgroundColor=[UIColor clearColor];
        titleLabel.text=[encourageArray objectAtIndex:i ];
        titleLabel.textColor=COLOR(193, 10, 7);
        titleLabel.textAlignment=NSTextAlignmentCenter;
       CGAffineTransform rotateTitle = CGAffineTransformMakeRotation(M_PI*0.1663*i);
        titleLabel.transform=rotateTitle;
        titleLabel.font=[UIFont systemFontOfSize:10];
        titleLabel.layer.anchorPoint=CGPointMake(0.5, 0.85);
         [secondImageView addSubview:titleLabel];
        
    }
    
     
    pointView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 23, 50)];
    pointView.center=secondImageView.center;
     UIImage *imageXX=[UIImage imageNamed:@"LuckyRototeSelected"];
    pointView.image=imageXX;
   
    pointView.layer.anchorPoint=CGPointMake(0.5, 1);
        rotatePointView = CGAffineTransformMakeRotation(M_PI*0.1663*0 );
     [pointView setTransform:rotatePointView];
 
    [self.view addSubview:pointView];
    

    animationPoint = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animationPoint.duration=7;
    animationPoint.repeatCount = 1;
    animationPoint.fromValue = [NSNumber numberWithFloat:0.0]; // 起始角度
    animationPoint.toValue = [NSNumber numberWithFloat:-55 * M_PI]; // 终止角度
    animationPoint.delegate = self;
 
   centerButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [centerButton setFrame:CGRectMake(0, 0, DEVICE_Width*0.2, DEVICE_Width*0.2)];
    [centerButton setImage: [UIImage imageNamed:@"LuckyCenterButtonPressed"] forState:UIControlStateNormal];
    [centerButton setImage: [UIImage imageNamed:@"LuckyCenterButton"] forState:UIControlStateSelected];
    [centerButton setTintColor:[UIColor clearColor]];
    centerButton.selected=YES;
     centerButton.adjustsImageWhenHighlighted = NO;
    centerButton.backgroundColor=[UIColor clearColor];
    [centerButton addTarget:self action:@selector(beginRotationClick:) forControlEvents:UIControlEventTouchUpInside];
   centerButton.center=ImageView.center;
    [self.view addSubview:centerButton];
    
    
    

}
-(void)beginRotationClick:(id)sender
{
  
   [centerButton removeTarget:self action:@selector(beginRotationClick:) forControlEvents:UIControlEventTouchUpInside];
    [centerButton setImage: [UIImage imageNamed:@"LuckyCenterButtonPressed"] forState:UIControlStateNormal];
    [centerButton setImage: [UIImage imageNamed:@"LuckyCenterButtonPressed"] forState:UIControlStateSelected];
    [pointView.layer addAnimation:animationPoint forKey:@"rotate-layer"];
     [secondImageView.layer addAnimation:animationSecond forKey:@"rotate-layer"];
     [ImageView.layer addAnimation:animationSecond forKey:@"rotate-layer"];
    
    
}


/*这是个代理*/
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    
       [pointView.layer removeAnimationForKey:@"rotate-layer"];
       [secondImageView.layer removeAnimationForKey:@"rotate-layer"];
       [ImageView.layer removeAnimationForKey:@"rotate-layer"];
   
    [centerButton setImage: [UIImage imageNamed:@"LuckyCenterButtonPressed"] forState:UIControlStateNormal];
    [centerButton setImage: [UIImage imageNamed:@"LuckyCenterButton"] forState:UIControlStateSelected];
    //2  7
       int value = (arc4random() % 14) + 1;
    if (value<=12 &&value!=2 &&value!=7) {
         rotatePointView = CGAffineTransformMakeRotation(M_PI*0.1663*value );
        pointView.transform=rotatePointView;
        [centerButton addTarget:self action:@selector(beginRotationClick:) forControlEvents:UIControlEventTouchUpInside];
        return;
    }
    if (value>12) {
    
        NSArray *[email protected][@"2",@"7"];
           int value = (arc4random() % 2) + 0;
         rotatePointView = CGAffineTransformMakeRotation(M_PI*0.1663* [[tempIndexArray objectAtIndex:value]intValue] );
    }
    
    pointView.transform=rotatePointView;
     [centerButton addTarget:self action:@selector(beginRotationClick:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)animationDidStart:(CAAnimation *)anim
{
 
}

二、效果截图

  技术分享

    

 

以上是关于objective-c 抽奖转盘(原创)的主要内容,如果未能解决你的问题,请参考以下文章

PHP实现刮刮卡,大转盘抽奖概率,其实很简单!

求Java实现转盘转动功能,抽奖,坐等

微信大转盘抽奖先报名后抽奖怎么做?

Es6语法实现的转盘抽奖效果——可配置转盘的抽奖概率

转盘指定概率抽奖

css 如何“画”一个抽奖转盘