iOS转盘抽奖

#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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,444评论 4 365
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,867评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,157评论 0 248
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,312评论 0 214
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,673评论 3 289
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,802评论 1 223
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,010评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,743评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,470评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,696评论 2 250
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,187评论 1 262
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,538评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,188评论 3 240
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,127评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,902评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,889评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,741评论 2 274

推荐阅读更多精彩内容