抽奖转盘
来源:互联网 发布:淘宝怎么撤销退款申请 编辑:程序博客网 时间:2024/04/30 06:39
#import <UIKit/UIKit.h>
@interface WheelView :UIView
+ (instancetype)wheelView;
- (void)startAnimation;
- (void)stopAnimation;
@end
#import "WheelView.h"
#import "WButton.h"
@interface WheelView ()
@property (weak, nonatomic) IBOutletUIImageView *centerView;
@property (nonatomic,strong) UIButton *selectedBtn;
@property (nonatomic,strong) CADisplayLink *link;
- (IBAction)chooseNumber:(UIButton *)sender;
@end
@implementation WheelView
+ (instancetype)wheelView{
return [[[NSBundlemainBundle] loadNibNamed:@"WheelView"owner:niloptions:nil]lastObject];
}
//添加按钮
- (void)awakeFromNib{
self.centerView.userInteractionEnabled =YES;
//大图片
UIImage *img = [UIImageimageNamed:@"LuckyAstrology"];
UIImage *imgSelected = [UIImageimageNamed:@"LuckyAstrologyPressed"];
//从大图片中裁剪对应星座的图片
CGFloat smallW = img.size.width /12 * [UIScreenmainScreen].scale;
CGFloat smallH = img.size.height * [UIScreenmainScreen].scale;
// 添加12个按钮
for (int index =0; index < 12; index++) {
WButton *btn = [WButtonbuttonWithType:UIButtonTypeCustom];
CGRect smallRect = CGRectMake(index * smallW, 0, smallW, smallH);
// CGImageCreateWithImageInRect只认像素
CGImageRef smallImage = CGImageCreateWithImageInRect(img.CGImage, smallRect);
[btn setImage:[UIImageimageWithCGImage:smallImage] forState:UIControlStateNormal];
CGImageRef smallImageSelected = CGImageCreateWithImageInRect(imgSelected.CGImage, smallRect);
[btn setImage:[UIImageimageWithCGImage:smallImageSelected] forState:UIControlStateSelected];
[btn setBackgroundImage:[UIImageimageNamed:@"LuckyRototeSelected"]forState:UIControlStateSelected];
btn.bounds =CGRectMake(0,0, 68, 143);
// 设置锚点和位置
btn.layer.anchorPoint =CGPointMake(0.5,1);
btn.layer.position =CGPointMake(self.centerView.frame.size.width * 0.5, self.centerView.frame.size.height * 0.5);
//设置旋转角度(绕着锚点进行旋转)
CGFloat angle = (30 * index) /180.0 * M_PI;
btn.transform =CGAffineTransformMakeRotation(angle);
btn.layer.masksToBounds =YES;
// 监听按钮点击
[btn addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchDown];
[self.centerViewaddSubview:btn];
btn.tag = index;
//
if (index == 0) {
[selfbtnClick:btn];
}
}
}
#pragma mark - btnAction
- (void)btnClick:(WButton *)btn{
self.selectedBtn.selected =NO;
btn.selected =YES;
self.selectedBtn = btn;
}
//核心动画不会改图层的属性,旋转式假象
//开始不停旋转
- (void)startAnimation{
if (self.link) {
return;
}
//1秒刷新60次
CADisplayLink *link = [CADisplayLinkdisplayLinkWithTarget:selfselector:@selector(update)];
[link addToRunLoop:[NSRunLoopmainRunLoop] forMode:NSDefaultRunLoopMode];
self.link = link;
}
- (void)update{
self.centerView.transform = CGAffineTransformRotate(self.centerView.transform,M_PI / 100);
}
- (void)stopAnimation{
[self.linkinvalidate];
self.link =nil;
}
- (IBAction)chooseNumber:(UIButton *)sender {
[selfstopAnimation];
CABasicAnimation *anim = [CABasicAnimationanimation];
anim.keyPath =@"transform.rotation";
anim.toValue =@(2 * M_PI *3);
anim.duration =2;
anim.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
anim.delegate =self;
[self.centerView.layeraddAnimation:anim forKey:nil];
self.userInteractionEnabled =NO;
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
self.userInteractionEnabled =YES;
//选中的视图置顶居中
self.centerView.transform = CGAffineTransformMakeRotation(-(self.selectedBtn.tag * M_PI / 6));
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
[selfstartAnimation];//1秒之后开始旋转
});
}
@end
#import <UIKit/UIKit.h>
@interface WButton :UIButton
@end
#import "WButton.h"
@implementation WButton
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat imageW = 40;
CGFloat imageH = 47;
CGFloat imageX = (contentRect.size.width - imageW) *0.5;
CGFloat imageY = 20;
return CGRectMake(imageX, imageY, imageW, imageH);
}
- (void)setHighlighted:(BOOL)highlighted
{
}
@end
使用
#import "ViewController.h"
#import "WheelView.h"
@interface ViewController ()
- (IBAction)stop:(id)sender;
@property (nonatomic,strong) WheelView *wheel;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
WheelView *wheel = [WheelViewwheelView];
wheel.center =CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height /2);
[wheel startAnimation];
[self.viewaddSubview:wheel];
self.wheel = wheel;
}
- 转盘抽奖
- 抽奖转盘
- 抽奖转盘
- 转盘抽奖
- 抽奖转盘
- 转盘抽奖
- 抽奖转盘
- 抽奖转盘
- 转盘抽奖
- 抽奖转盘
- html5抽奖转盘生成器
- 转盘抽奖案例
- Android抽奖转盘代码
- 抽奖大转盘
- 转盘抽奖游戏
- 大转盘抽奖算法
- java实现转盘抽奖;
- SurfaceView 实现 转盘抽奖
- 设计模式(6):代理模式
- 1461 手机
- awk脚本 最简介
- CSS基础-23选择器-选择器详解(2)
- leetcode--Binary Tree Upside Down
- 抽奖转盘
- Android Studio官方视频教程笔记
- 51串口通信发送和接收
- XJOI NOIP2015模拟赛Day1 T2 ctps bitset优化 或 排序+cdq分治+树状数组+平衡树
- 为什么寄存器比内存块
- JavaStart
- 06_桥接模式
- Leetcode: Combination Sum III
- HDU 4883 TIANKENG’s restaurant【贪心+日期】