#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下載