#import
@interfaceWheelButton :UIButton
@end
#import "WheelButton.h"
@implementation WheelButton
-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event{
CGRect rect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height * 0.5);
if(CGRectContainsPoint(rect, point)) {
//在指定范围内,按钮能点击
return[superhitTest:pointwithEvent:event];
}else{
returnnil;
}
}
//返回当前按钮当中的ImageView尺寸位置
//contentRect 当前按钮的位置尺寸
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
CGFloatw =40;
CGFloath =48;
CGFloatx = (contentRect.size.width- w) *0.5;
CGFloaty =20;
return CGRectMake(x, y, w, h);
}
//返回当前按钮当中的Label尺寸位置
//-(CGRect)titleRectForContentRect:(CGRect)contentRect{
//
//}
//取消按钮高亮状态下做的事
- (void)setHighlighted:(BOOL)highlighted{
}
#import
@interfaceWheelView :UIView
+(instancetype)wheelView;
-(void)rotation;
-(void)stop;
@end
#import "WheelView.h"
#import "WheelButton.h"
#define angle2Rad(angle) ((angle /100.0* M_PI))
@interface WheelView()<CAAnimationDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *contentView;
@property (nonatomic, strong) CADisplayLink *link;
//当前选中按钮
@property (nonatomic, strong) UIButton *selectBtn;
@end
@implementation WheelView
-(CADisplayLink *)link{
if(_link==nil) {
//添加定时器,保持一直旋转
CADisplayLink*link = [CADisplayLinkdisplayLinkWithTarget:selfselector:@selector(update)];
[linkaddToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
_link= link;
}
return _link;
}
+ (instancetype)wheelView{
return [[[NSBundle mainBundle] loadNibNamed:@"WheelView" owner:nil options:nil] lastObject];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self= [superinitWithFrame:frame];
if(self) {
self = [[[NSBundle mainBundle] loadNibNamed:@"WheelView" owner:nil options:nil] lastObject];
}
return self;
}
-(void)awakeFromNib{
self.contentView.userInteractionEnabled = YES;
CGFloatbtnW =30;
CGFloatbtnH =130;
CGFloatangle =0;
//加载原始大图片
UIImage*oriImage = [UIImageimageNamed:@"name12"];
//加载原始选中大图片
UIImage*oriSelectImage = [UIImageimageNamed:@"name8"];
CGFloatx =0;
CGFloaty =0;
CGFloatclipW = oriImage.size.width/12.0;
CGFloatclipH = oriImage.size.height;
for(inti =0; i <12; i++) {
WheelButton *btn = [WheelButton buttonWithType:UIButtonTypeCustom];
btn.bounds=CGRectMake(0,0, btnW, btnH);
//设置按钮选中状态下的背景图片
[btnsetBackgroundImage:[UIImage imageNamed:@"name8"] forState:UIControlStateSelected];
//设置按钮正常状态下的图片
//截取指定区域范围内的图片
x = i * clipW;
//CGImageCreateWithImageInRect
//在iOS当中使用的都是点坐标
CGImageRefclipImage =CGImageCreateWithImageInRect(oriImage.CGImage,CGRectMake(x, y, clipW, clipH));
//设置按钮正常状态下显示的图片
[btnsetImage:[UIImage imageWithCGImage:clipImage] forState:UIControlStateNormal];
CGImageRefclipSelectImage =CGImageCreateWithImageInRect(oriSelectImage.CGImage,CGRectMake(x, y, clipW, clipH));
//设置按钮选中状态下显示的图片
[btnsetImage:[UIImage imageWithCGImage:clipSelectImage] forState:UIControlStateSelected];
// CGRect rect = CGRectMake(x, y, clipW, clipH);
// CGImageCreateWithImageInRect(oriImage.CGImage, rect);
//设置按钮位置
btn.layer.anchorPoint=CGPointMake(0.5,1);
btn.layer.position = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);
//让每一个按钮在上一个基础旋转30度
btn.transform = CGAffineTransformMakeRotation(angle2Rad(angle));
angle +=30;
//监听按钮点击
[btnaddTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.contentViewaddSubview:btn];
//默认第一个按钮选中
if(i ==0) {
[selfbtnClick:btn];
}
}
}
-(void)btnClick:(UIButton*)btn{
//让当前点击按钮选中状态
btn.selected=YES;
//1、让当前选中按钮取消选中状态
self.selectBtn.selected= NO;
//2、让当前点击按钮成为选中状态
btn.selected=YES;
//3、让当前点击的按钮成为选中状态
self.selectBtn= btn;
}
//让转盘开始旋转
-(void)rotation{
self.link.paused=NO;
// CABasicAnimation *anim = [CABasicAnimation animation];
// anim.keyPath = @"transform.rotation";
// anim.toValue = @(M_PI * 3);
// anim.duration = 5;
// anim.repeatCount = MAXFLOAT;
//
// [self.contentView.layer addAnimation:anim forKey:nil];
}
-(void)update{
self.contentView.transform = CGAffineTransformRotate(self.contentView.transform, M_PI / 300.0);
}
- (void)stop{
self.link.paused=YES;
}
//开始选号
- (IBAction)start:(id)sender {
//让转盘快速旋转几圈,当前选中按钮指向最上方
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @"transform.rotation";
anim.toValue=@(M_PI*4);
anim.duration=0.5;
anim.delegate=self;
//anim.repeatCount = 2;
[self.contentView.layer addAnimation:anim forKey:nil];
}
//当动画结束时调用
- (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag{
//动画结束时当前选中呢按钮指向最上方
//让当前选中的按钮的父控件倒着旋转回去
CGAffineTransform transform = self.selectBtn.transform;
//通过transform获取当前按钮旋转度数
CGFloatangle =atan2(transform.a, transform.b);
self.contentView.transform = CGAffineTransformMakeRotation(-angle);
}
@end
#import "ViewController.h"
#import "WheelView.h"
@interface ViewController ()
@property (nonatomic, strong) WheelView *wheelV;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
WheelView*wheelV = [WheelViewwheelView];
wheelV.center=self.view.center;
self.wheelV= wheelV;
[self.viewaddSubview:wheelV];
}
- (IBAction)rotation:(id)sender {
[self.wheelVrotation];
}
- (IBAction)stop:(id)sender {
[self.wheelVstop];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end