大转盘抽奖
首先感謝那些有關抽獎demo的開源代碼作者,感謝你們的共享!
這個demo不僅可以隨機進行抽獎,也可以設置想要的獎品進行抽獎,廢話不多說,直接上代碼。。。
主要代碼 #import <UIKit/UIKit.h>//抽獎結果(獎品下標) typedef void(^Completion)(NSInteger index);@interface LuckyDrawView : UIView/*** 自定義初始化方法** @param frame frame坐標* @param prizeArr 獎品數組* @param completion 抽獎結果獎品* @param selectPrize 設置抽選固定獎品** @return*/ - (id)initWithFrame:(CGRect)frame prizeArray:(NSArray<NSString *> *)prizeArray completion:(Completion)completion selectPrize:(NSString *)selectPrize first:(BOOL)first;@end下面是主要實現的代碼 #import "LuckyDrawView.h"#define kDegreesToRadians(degrees) ((M_PI * degrees)/ 180) #define AKAngle(radian) (radian / M_PI * 180.f) #define AKCos(a) cos(a / 180.f * M_PI) #define AKSin(a) sin(a / 180.f * M_PI)static NSString *string = @"0";@interface PrizePlateView : UIView@property(nonatomic ,assign)float degrees;@end@implementation PrizePlateView//自定初始化獎盤 -(id)initWithFrame:(CGRect)frame degrees:(float)degrees {self = [super initWithFrame:frame];if (self) {_degrees = degrees;self.backgroundColor = [UIColor clearColor];}return self; }-(void)drawRect:(CGRect)rect {float width = rect.size.width;float height = rect.size.height;float moveAngle = self.degrees + 270 > 360 ? self.degrees - 90 : self.degrees + 270 ;CGContextRef cont = UIGraphicsGetCurrentContext();CGContextSetStrokeColorWithColor(cont, [UIColor colorWithRed:1 green:1 blue:1 alpha:0.3].CGColor);CGContextSetLineWidth(cont, width / 3.f);CGContextAddArc(cont, width / 2.f, height / 2.f, width / 3.f, kDegreesToRadians(270), kDegreesToRadians(moveAngle), 0);CGContextDrawPath(cont, kCGPathStroke);CGContextRef cont1 = UIGraphicsGetCurrentContext();CGContextSetStrokeColorWithColor(cont1, [UIColor redColor].CGColor);CGContextSetLineWidth(cont1, width / 20.f);CGContextAddArc(cont1, width / 2.f, height / 2.f, 11 * width / 40.f, kDegreesToRadians(270), kDegreesToRadians(moveAngle), 0);CGContextDrawPath(cont, kCGPathStroke); }@end@interface LuckyDrawView()@property (nonatomic, strong) NSArray *prizeArray; @property (nonatomic, strong) PrizePlateView *prizeView; @property (nonatomic, assign) NSInteger prizeIndex; @property (nonatomic, assign) NSInteger rotatCount; @property (nonatomic, assign) NSInteger progressCount; @property (nonatomic, strong) NSTimer *timer; @property (nonatomic, strong) NSTimer *timerTwo; @property (nonatomic, copy) Completion competion; @property (nonatomic, assign) BOOL canBegin; @property (nonatomic, assign) BOOL first; @property (nonatomic, strong) NSString *selectPrize; @property (nonatomic, assign) int number; @property (nonatomic, assign) float time;@end@implementation LuckyDrawView-(id)initWithFrame:(CGRect)frame prizeArray:(NSArray<NSString *> *)prizeArray completion:(Completion)completion selectPrize:(NSString *)selectPrize first:(BOOL)first {self = [super initWithFrame:frame];if (self) {//設置轉動圈數的初始值self.number = 0;//設置轉一圈的時間self.time = 0.4;//獎品數組self.prizeArray = prizeArray;//block設置self.competion = completion;//設置加速加速圈數self.progressCount = self.prizeArray.count * 2;//是否在抽獎self.canBegin = YES;//轉動總圈數self.rotatCount = prizeArray.count * 10;//設置默認獎品self.selectPrize = selectPrize;//設置背景色self.backgroundColor = [UIColor clearColor];//設置首次self.first = first;}return self; }-(void)drawRect:(CGRect)rect {float width = rect.size.width;float height = rect.size.height;float degrees = (1 / (float)self.prizeArray.count) * 360.f;float statAngle = kDegreesToRadians(270.f);self.prizeView = [[PrizePlateView alloc] initWithFrame:rect degrees:degrees];[self addSubview:self.prizeView];float radius = 2 * width / 5.f + width / 10.f;float radius1 = width / 5.f;for (int i = 0; i < self.prizeArray.count; i++) {//計算弧度float moveAngle = degrees * (i + 1) + 270 > 360 ? degrees * (i + 1) - 90 : degrees * (i + 1) + 270 ;//繪制弧度CGContextRef cont = UIGraphicsGetCurrentContext();CGContextSetStrokeColorWithColor(cont, [UIColor colorWithRed:255 / 255.f green:166 / 255.f blue:49 / 255.f alpha:1].CGColor);CGContextSetLineWidth(cont, width / 5.f);CGContextAddArc(cont, width / 2.f, height / 2.f, 2 * width / 5.f, statAngle, kDegreesToRadians(moveAngle), 0);CGContextDrawPath(cont, kCGPathStroke);statAngle = kDegreesToRadians(moveAngle);//繪制分割線CGContextRef cont1 = UIGraphicsGetCurrentContext();CGContextMoveToPoint(cont1, radius + AKSin(i * degrees) * radius, radius - AKCos(i * degrees) * radius);CGContextAddLineToPoint(cont1, (radius + AKSin(i * degrees) * radius) - AKSin(i * degrees) * radius1, (radius - AKCos(i * degrees) * radius) + AKCos(i * degrees) * radius1);CGContextSetStrokeColorWithColor(cont1, [UIColor whiteColor].CGColor);CGContextSetLineWidth(cont1, 1);CGContextDrawPath(cont1, kCGPathStroke);//添加獎品名稱UILabel *label = [[UILabel alloc] init];label.text = [self.prizeArray objectAtIndex:i];label.font = [UIFont systemFontOfSize:13];label.textColor = [UIColor blackColor];label.numberOfLines = 0;label.lineBreakMode = NSLineBreakByTruncatingTail;CGSize size = [label sizeThatFits:CGSizeMake(100, 99999)];label.frame = CGRectMake(0, 0, size.width, size.height);float labelDegees = i * degrees + degrees / 2.f;float centerX = AKSin(labelDegees) * 2 * width / 5.f + radius;float centerY = radius - AKCos(labelDegees) * 2 * width / 5.f;label.textAlignment = NSTextAlignmentCenter;label.center = CGPointMake(centerX, centerY);[self addSubview:label];label.transform = CGAffineTransformRotate(label.transform, kDegreesToRadians(labelDegees));}//中間按鈕部分CGContextRef cont2 = UIGraphicsGetCurrentContext();CGContextSetFillColorWithColor(cont2, [UIColor colorWithRed:255 / 255.f green:70 / 255.f blue:31 / 255.f alpha:1].CGColor);CGContextAddArc(cont2, width / 2.f, height / 2.f, 3 * width / 10.f, 0, 2 * M_PI, 0);CGContextDrawPath(cont2, kCGPathFill);UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];button.frame = CGRectMake(0, 0, 3 * width / 10.f, 3 * width / 10.f);button.center = CGPointMake(width / 2.f, height / 2.f);[self addSubview:button];[button setTitle:@"啟動" forState:UIControlStateNormal];[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];[button addTarget:self action:@selector(beginPrize) forControlEvents:UIControlEventTouchUpInside]; }//點擊開始抽獎 - (void)beginPrize {if (self.canBegin) {if (self.selectPrize) {for (int j = 0; j < self.prizeArray.count; j++) {NSString *prize = [self.prizeArray objectAtIndex:j];if ([prize isEqualToString:self.selectPrize]) {self.prizeIndex = j;}}string = @"1";}else{string = @"0";self.prizeIndex = arc4random() % (self.prizeArray.count -1);}[self rollingPrize];} }//抽獎動畫 - (void)rollingPrize {self.canBegin = NO;self.prizeView.transform = CGAffineTransformRotate(self.prizeView.transform, kDegreesToRadians(self.prizeView.degrees));self.number++;if (self.number == self.prizeArray.count*2) {self.time = 0.08;}else if (self.number < self.prizeArray.count*2){self.time = self.time - (0.3/self.progressCount);}else if (self.number > self.prizeArray.count * 8){self.time = self.time + (0.3/self.progressCount);} NSInteger allNumber = 0;if ([string isEqualToString:@"1"]) {if (self.first) {allNumber = self.rotatCount + self.prizeIndex;}else{allNumber = self.rotatCount + self.prizeIndex + (self.prizeArray.count - self.prizeIndex);}}else{allNumber = self.rotatCount + self.prizeIndex;}if (self.number == allNumber) {[self.timer invalidate];self.timer = nil;self.competion(((self.rotatCount + self.prizeIndex) % self.prizeArray.count));self.canBegin = YES;self.first = NO;self.number = 0;self.time = 0.4;}else{[self.timer invalidate];self.timer = [NSTimer scheduledTimerWithTimeInterval:self.time target:self selector:@selector(rollingPrize) userInfo:nil repeats:NO];} }@end下面是效果圖
下面是源碼連接
點擊這里下載,希望對大家有幫助
總結
- 上一篇: .net mvc 使用盛派Senparc
- 下一篇: Centos7基于PG13.6+PgPo