ViewController.m
#import "ViewController.h"
#import "WheelView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
WheelView *wheelView=[[WheelView alloc] initWithFrame:CGRectMake(0, 0, 286, 286)];
wheelView.center=self.view.center;
[self.view addSubview:wheelView];
}
WhellView.m
#import "WheelView.h"
#import "SCButton.h"
@interface WheelView ()
{
UIImageView *_rotationImageView;
CADisplayLink *_link;
UIButton *_isButton;
}
@end
@implementation WheelView
-(id)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self) {
[self addRotationImageView];
[self addButton1];
}
return self;
}
-(void)addButton1
{
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(0, 0, 81, 81);
[button setBackgroundImage:[UIImage imageNamed:@"LuckyCenterButtonPressed@2x.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"LuckyCenterButton@2x.png"] forState:UIControlStateHighlighted];
button.center=CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
//添加转盘视图
-(void)addRotationImageView
{
_rotationImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
_rotationImageView.image=[UIImage imageNamed:@"LuckyRotateWheel.png"];
_rotationImageView.userInteractionEnabled=YES;
[self addSubview:_rotationImageView];
[self addButtons];
}
-(void)addButtons
{
UIImage *image1=[UIImage imageNamed:@"LuckyAstrology.png"];
UIImage *image2=[UIImage imageNamed:@"LuckyAstrologyPressed.png"];
for (int i=0; i<12; i++) {
SCButton *button=[SCButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"LuckyRototeSelected.png"] forState:UIControlStateSelected];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
button.frame=CGRectMake(0, 0, 68, 143);
button.layer.anchorPoint=CGPointMake(0.5, 1);
button.layer.position=CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
button.layer.transform=CATransform3DMakeRotation(M_PI/180*30*i, 0, 0, 1);
if (i==0) {
[button setSelected:YES];
[self buttonClick:button];
}
[_rotationImageView addSubview:button];
//裁减的宽度
CGFloat flipWidth=image1.size.width/12*[UIScreen mainScreen].scale;
//裁减的高度
CGFloat flipHeight=image1.size.height*[UIScreen mainScreen].scale;
//裁减图片
//设置正常状态下的按钮图片
CGImageRef cgImage=CGImageCreateWithImageInRect(image1.CGImage, CGRectMake(80*i, 0,flipWidth,flipHeight));
[button setImage:[UIImage imageWithCGImage:cgImage] forState:UIControlStateNormal];
//设置被选中状态下的按钮图片
CGImageRef cgImage1=CGImageCreateWithImageInRect(image2.CGImage, CGRectMake(80*i, 0, flipHeight, flipHeight));
[button setImage:[UIImage imageWithCGImage:cgImage1] forState:UIControlStateSelected];
}
}
//按钮被点击
-(void)buttonClick:(UIButton *)button
{
//取消上次被点击按钮的选择状态
[_isButton setSelected:NO];
_isButton=button;
//设置button被选中
[_isButton setSelected:YES];
}
-(void)click
{
if (_link==nil) {
//生成一个_link对象 然后添加到NSRunLoop中
_link=[CADisplayLink displayLinkWithTarget:self selector:@selector(move)];
[_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}else{
//如果_link暂停 则恢复_link执行,否则暂停
if (_link.isPaused) {
[_link setPaused:NO];
}else{
[_link setPaused:YES];
}
}
}
-(void)move
{
//位置变化
//_imageView.transform=CGAffineTransformTranslate(_imageView.transform, 1, 1);
// srand((unsigned) time(0));
// float a=(rand()%20)/10;
_rotationImageView.transform=CGAffineTransformRotate(_rotationImageView.transform, M_PI/180*3);
}
@end
SCButton.m文件
#import "SCButton.h"
@implementation SCButton
-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
return CGRectMake((contentRect.size.width-40)/2, 20, 40, 47);
}
//取消高亮
-(void)setHighlighted:(BOOL)highlighted
{
}
@end