首页 > 代码库 > 转盘功能的详细实现
转盘功能的详细实现
转盘功能的详细实现
一、自定义转盘XIB
+ (instancetype)wheelView
{
return [[NSBundlemainBundle]loadNibNamed:@"HMWheelView"owner:niloptions:nil][0];
}
{
return [[NSBundlemainBundle]loadNibNamed:@"HMWheelView"owner:niloptions:nil][0];
}
二、添加按钮
#warning添加按钮
- (void)awakeFromNib
{
- (void)awakeFromNib
{
// 加载完XIB就开始启动转盘
self.link.paused= NO;
_rotationView.userInteractionEnabled= YES;
// 裁剪的大图片
UIImage *bigImage = [UIImageimageNamed:@"LuckyAstrology"];
UIImage *selectedImage = [UIImageimageNamed:@"LuckyAstrologyPressed"];
// 图片的尺寸
CGFloat imageW =40* [UIScreenmainScreen].scale;
CGFloat imageH =47* [UIScreenmainScreen].scale;
for (inti = 0; i < 12; i++) {
// 创建按钮
HMWheelButton *button = [HMWheelButtonbuttonWithType:UIButtonTypeCustom];
// 锚点
button.layer.anchorPoint= CGPointMake(0.5,1);
// 位置
button.layer.position= CGPointMake(self.bounds.size.width* 0.5, self.bounds.size.height* 0.5);
// 旋转按钮
button.layer.transform= CATransform3DMakeRotation(angle2radian(i * 30), 0, 0, 1);
// 尺寸
button.bounds= CGRectMake(0,0,68,143);
// 设置选中时候的背景图片
[button setBackgroundImage:[UIImageimageNamed:@"LuckyRototeSelected"]forState:UIControlStateSelected];
// 设置按钮的图片
// image:裁剪的图片
// rect:裁剪的尺寸
CGRect clipRect =CGRectMake(i * imageW,0, imageW, imageH);
CGImageRef smallImage =CGImageCreateWithImageInRect(bigImage.CGImage, clipRect);
[button setImage:[UIImageimageWithCGImage:smallImage]forState:UIControlStateNormal];
// 设置选中的图片
CGImageRef selectedSmallImage =CGImageCreateWithImageInRect(selectedImage.CGImage, clipRect);
[button setImage:[UIImageimageWithCGImage:selectedSmallImage]forState:UIControlStateSelected];
// 监听点击事件
[button addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchDown];
_rotationView.userInteractionEnabled= YES;
// 裁剪的大图片
UIImage *bigImage = [UIImageimageNamed:@"LuckyAstrology"];
UIImage *selectedImage = [UIImageimageNamed:@"LuckyAstrologyPressed"];
// 图片的尺寸
CGFloat imageW =40* [UIScreenmainScreen].scale;
CGFloat imageH =47* [UIScreenmainScreen].scale;
for (inti = 0; i < 12; i++) {
// 创建按钮
HMWheelButton *button = [HMWheelButtonbuttonWithType:UIButtonTypeCustom];
// 锚点
button.layer.anchorPoint= CGPointMake(0.5,1);
// 位置
button.layer.position= CGPointMake(self.bounds.size.width* 0.5, self.bounds.size.height* 0.5);
// 旋转按钮
button.layer.transform= CATransform3DMakeRotation(angle2radian(i * 30), 0, 0, 1);
// 尺寸
button.bounds= CGRectMake(0,0,68,143);
// 设置选中时候的背景图片
[button setBackgroundImage:[UIImageimageNamed:@"LuckyRototeSelected"]forState:UIControlStateSelected];
// 设置按钮的图片
// image:裁剪的图片
// rect:裁剪的尺寸
CGRect clipRect =CGRectMake(i * imageW,0, imageW, imageH);
CGImageRef smallImage =CGImageCreateWithImageInRect(bigImage.CGImage, clipRect);
[button setImage:[UIImageimageWithCGImage:smallImage]forState:UIControlStateNormal];
// 设置选中的图片
CGImageRef selectedSmallImage =CGImageCreateWithImageInRect(selectedImage.CGImage, clipRect);
[button setImage:[UIImageimageWithCGImage:selectedSmallImage]forState:UIControlStateSelected];
// 监听点击事件
[button addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchDown];
// 默认选中第一个
if (i == 0) {
[selfbtnClick:button];
}
[_rotationViewaddSubview:button];
}
}
if (i == 0) {
[selfbtnClick:button];
}
[_rotationViewaddSubview:button];
}
}
三、实现监听方法
#warning监听按钮点击
- (void)btnClick:(UIButton*)button
{
_selectedButton.selected= NO;
button.selected= YES;
_selectedButton = button;
}
- (void)btnClick:(UIButton*)button
{
_selectedButton.selected= NO;
button.selected= YES;
_selectedButton = button;
}
四、定义定时器
- (CADisplayLink*)link
{
if (_link== nil) {
CADisplayLink *link = [CADisplayLinkdisplayLinkWithTarget:selfselector:@selector(update)];
[link addToRunLoop:[NSRunLoopmainRunLoop]forMode:NSDefaultRunLoopMode];
_link = link;
}
return _link;
}
- (void)update
{
_rotationView.transform= CGAffineTransformRotate(_rotationView.transform, ((45 / 60.0) / 180.0 * M_PI));
{
if (_link== nil) {
CADisplayLink *link = [CADisplayLinkdisplayLinkWithTarget:selfselector:@selector(update)];
[link addToRunLoop:[NSRunLoopmainRunLoop]forMode:NSDefaultRunLoopMode];
_link = link;
}
return _link;
}
- (void)update
{
_rotationView.transform= CGAffineTransformRotate(_rotationView.transform, ((45 / 60.0) / 180.0 * M_PI));
}
五、实现开始选号功能
- (IBAction)start:(id)sender {
// 1.不要和用户交互
_rotationView.userInteractionEnabled= NO;
// 2.取消慢慢的旋转
_link.paused= YES;
CABasicAnimation *anim = [CABasicAnimationanimation];
anim.keyPath= @"transform.rotation";
anim.toValue= @(M_PI* 2 * 3);
anim.duration= 0.5;
anim.delegate= self;
[_rotationView.layeraddAnimation:animforKey:nil];
}
六、实现 CAAnimationDelegate的代理方法
- (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag
{
_rotationView.userInteractionEnabled= YES;
// 让选中按钮回到最在上面的中间位置:
CGFloat angle =atan2(_selectedButton.transform.b,_selectedButton.transform.a);
// 把我们的转盘反向旋转这么多°
_rotationView.transform= CGAffineTransformMakeRotation(-angle);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2* NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
[selfstartRotating];
});
}
转盘功能的详细实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。