美文网首页
iOS转盘抽奖

iOS转盘抽奖

作者: 会写bug的程序媛 | 来源:发表于2018-12-13 14:52 被阅读0次

#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

相关文章

  • iOS转盘抽奖

    #import @interfaceWheelButton :UIButton @end #import "Whe...

  • iOS 抽奖转盘动画

    用法: 先添加指针视图,转盘背景和开始按钮。按钮的点击事件里设置开始动画,在动画开始的代理方法中让开始按钮不响应点...

  • ios 转盘抽奖demo

    规则要求: 每个奖品有中奖概率和奖品的个数,当奖品个数达到一定的数值时,中奖概率会发生变化。 转盘转动到哪个奖品的...

  • Scratch—转盘抽奖

    【知识延伸】 传统抽奖分为抽奖盒抽取、转盘抽奖等 【要求】 今天我们按照平时转盘抽奖、利用Scratch做一个转盘...

  • 抽奖转盘

    最近在写书上,老是看到别人抽一万钻的,我非常羡慕,就是我就试了试。 第一次我抽到的是解书钻二,我心想怎么这么倒霉,...

  • 抽奖转盘

    1.组件实现 2. 组件使用

  • 转盘抽奖

    越来越不济,100都没见一个。 全是10和20。

  • css3 转盘抽奖实践(sass)

    思路解释 转盘抽奖,就是像这样子的转盘。(如下图)点击中间的“点击抽奖”按钮,然后后面的圆形转盘开始转动,最后停在...

  • axure原型设计之转盘抽奖

    转盘抽奖一般会出现在一些金融类网站中,通过转盘抽奖来激活用户浏览行为,后来又多用于微信的公众号运营中,通过转盘抽奖...

  • 微信小程序 抽奖 转盘

    微信小程序 抽奖 转盘

网友评论

      本文标题:iOS转盘抽奖

      本文链接:https://www.haomeiwen.com/subject/gnxbhqtx.html