iOS 抽獎轉盤

來源:互聯網
上載者:User
#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下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.