星座转转转

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




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值