iOS 抽奖转盘

原创 2015年11月20日 12:08:49
#import "ViewController.h"

@interface ViewController ()
{
    NSString *strPrise;
}

@property (retain, nonatomic) UIView  *popView;
@property (retain, nonatomic) UILabel *labPrise;
@property (retain, nonatomic) UIButton *btn;
@property (retain, nonatomic) UIImageView *zhuanpan;

@end

@implementation ViewController
@synthesize btn;

- (void)viewDidLoad {
    [super viewDidLoad];

    //背景
    UIImageView *imgViewBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    imgViewBg.image = [UIImage imageNamed:@"bg.png"];
    [self.view addSubview:imgViewBg];

    //转盘
    _zhuanpan = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-280)/2,10, 280, 280)];
    _zhuanpan.image = [UIImage imageNamed:@"zhuanpan.png"];
    [self.view addSubview:_zhuanpan];

    //手指
    UIImageView *hander = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    hander.center = CGPointMake(_zhuanpan.center.x, _zhuanpan.center.y-30);
    hander.image = [UIImage imageNamed:@"hander.png"];
    [self.view addSubview:hander];

    _labPrise = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(_zhuanpan.frame), CGRectGetMaxY(_zhuanpan.frame)+50, CGRectGetWidth(_zhuanpan.frame), 20)];
    _labPrise.textColor = [UIColor orangeColor];
    _labPrise.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_labPrise];

    //开始或停止按钮
    btn = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, CGRectGetMaxY(_labPrise.frame)+50, 200, 35)];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"开始" forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor orangeColor]];
    btn.layer.borderColor = [UIColor orangeColor].CGColor;
    btn.layer.borderWidth = 1.0f;
    btn.layer.cornerRadius = 5.0f;
    btn.layer.masksToBounds = YES;
    [self.view addSubview:btn];
}

- (void)btnClick
{
    NSInteger angle;
    NSInteger randomNum = arc4random()%100;

    if (randomNum>=91 && randomNum<=99) {
        angle = 300;
        strPrise = @"一等奖";
    } else if (randomNum>=76 && randomNum<= 90) {
        angle = 60;
        strPrise = @"二等奖";
    } else if (randomNum >=51 && randomNum<=75) {
        angle = 180;
        strPrise = @"三等奖";
    } else {
        angle = 240;
        strPrise = @"没中奖";
    }

    [btn setTitle:@"抽奖中..." forState:UIControlStateNormal];
    _labPrise.text = [NSString stringWithFormat:@"中奖结果:%@",@"等待开奖结果"];
    btn.enabled = NO;


    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:angle*M_PI/180 ];
    rotationAnimation.duration = 1.0f;
    rotationAnimation.cumulative = YES;
    rotationAnimation.delegate = self;

    //以下两行同时设置才能保持移动后的位置状态不变
    rotationAnimation.fillMode=kCAFillModeForwards;
    rotationAnimation.removedOnCompletion = NO;

    [_zhuanpan.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

//动画结束
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    [UIView animateWithDuration:2.0
                     animations:^{

                         _popView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                         _popView.backgroundColor = [UIColor clearColor];
                         _popView.transform = CGAffineTransformMakeScale(2, 2);
                         [self.view addSubview:_popView];

                         UIImageView *popImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 150, self.view.frame.size.width-200, self.view.frame.size.width-200)];
                         popImageView.image = [UIImage imageNamed:@"prise.png"];
                         [_popView addSubview:popImageView];

                     }
                     completion:^(BOOL finished) {
                         [_popView removeFromSuperview];
                         _labPrise.text = [NSString stringWithFormat:@"中奖结果 : %@",strPrise];
                         [btn setTitle:@"开始抽奖" forState:UIControlStateNormal];
                         btn.enabled = YES;

                     }];

}

@end

效果图:

这里写图片描述

Demo下载

iOS 抽奖转盘

  • 2016年06月07日 11:26
  • 157KB
  • 下载

IOS 客户端 转盘指针式抽奖系统

  • 2015年10月12日 11:38
  • 841KB
  • 下载

iOS抽奖转盘动画之转场动画与动画组

将viewdidload里面的代码全部注释掉 - (void)viewDidLoad {     [super viewDidLoad]; } 重写点击交换的事件方法 //交换视图 ...

ios转盘抽奖项目提取实例

  • 2013年11月01日 21:24
  • 167KB
  • 下载

抽奖转盘ios

  • 2015年08月02日 16:24
  • 2.26MB
  • 下载

iOS 抽奖大转盘动画之CALayer图层介绍

1.先来介绍下layer的属性 - (void)initImageView{     //初始化imageview     UIImageView *imageview  = [[U...

ios 抽奖大转盘的实现

ios 抽奖大转盘 完工
  • whf727
  • whf727
  • 2013年07月12日 22:05
  • 5941

转盘抽奖 ios 抽奖转盘

  • 2013年12月10日 10:37
  • 107KB
  • 下载

js框架jquery实现幸运大转盘抽奖程序代码,兼容多种浏览器

js框架jquery实现幸运大转盘抽奖程序代码,兼容多种浏览器 源代码下载地址:http://www.zuidaima.com/share/1779633798073344.htm...

PHP + AJAX实现幸运大转盘抽奖

转自:http://www.yiifcms.com/post/59/
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:iOS 抽奖转盘
举报原因:
原因补充:

(最多只允许输入30个字)