抽奖转盘


#import <UIKit/UIKit.h>


@interface WheelView : UIView


+ (instancetype)wheelView;


- (void)startAnimation;

- (void)stopAnimation;

@end




#import "WheelView.h"

#import "WButton.h"

@interface WheelView ()

@property (weak, nonatomic) IBOutlet UIImageView *centerView;

@property (nonatomic, strong) UIButton *selectedBtn;


@property (nonatomic, strong) CADisplayLink *link;

- (IBAction)chooseNumber:(UIButton *)sender;


@end


@implementation WheelView


+ (instancetype)wheelView{

    return [[[NSBundle mainBundle] loadNibNamed:@"WheelView" owner:nil options:nil] lastObject];

}

//添加按钮

- (void)awakeFromNib{

    self.centerView.userInteractionEnabled = YES;

    //大图片

    UIImage *img = [UIImage imageNamed:@"LuckyAstrology"];

    UIImage *imgSelected = [UIImage imageNamed:@"LuckyAstrologyPressed"];

    

    //从大图片中裁剪对应星座的图片

    CGFloat smallW = img.size.width / 12 * [UIScreen mainScreen].scale;

    CGFloat smallH = img.size.height * [UIScreen mainScreen].scale;

    

    

    // 添加12个按钮

    for (int index = 0; index < 12; index++) {

        WButton *btn = [WButton buttonWithType:UIButtonTypeCustom];

        

        CGRect smallRect = CGRectMake(index * smallW, 0, smallW, smallH);

        

        // CGImageCreateWithImageInRect只认像素

        CGImageRef smallImage = CGImageCreateWithImageInRect(img.CGImage, smallRect);

        [btn setImage:[UIImage imageWithCGImage:smallImage] forState:UIControlStateNormal];

        

        CGImageRef smallImageSelected = CGImageCreateWithImageInRect(imgSelected.CGImage, smallRect);

        [btn setImage:[UIImage imageWithCGImage:smallImageSelected] forState:UIControlStateSelected];

        

        [btn setBackgroundImage:[UIImage imageNamed:@"LuckyRototeSelected"] forState:UIControlStateSelected];

        btn.bounds = CGRectMake(0, 0, 68, 143);

        

        // 设置锚点和位置

        btn.layer.anchorPoint = CGPointMake(0.5, 1);

        btn.layer.position = CGPointMake(self.centerView.frame.size.width * 0.5, self.centerView.frame.size.height * 0.5);

        

        // 设置旋转角度(绕着锚点进行旋转)

        CGFloat angle = (30 * index) / 180.0 * M_PI;

        btn.transform = CGAffineTransformMakeRotation(angle);

        btn.layer.masksToBounds = YES;

        // 监听按钮点击

        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];

            [self.centerView addSubview:btn];

        btn.tag = index;

        //

        if (index == 0) {

            [self btnClick:btn];

        }

        }


}


#pragma mark - btnAction

- (void)btnClick:(WButton *)btn{

    self.selectedBtn.selected = NO;

    btn.selected = YES;

    self.selectedBtn = btn;

}

//核心动画不会改图层的属性,旋转式假象

//开始不停旋转

- (void)startAnimation{

    if (self.link) {

        return;

    }

    //1秒刷新60

    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];

    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

    self.link = link;

}

- (void)update{

    self.centerView.transform = CGAffineTransformRotate(self.centerView.transform, M_PI / 100);

}

- (void)stopAnimation{

    [self.link invalidate];

    self.link = nil;

}

- (IBAction)chooseNumber:(UIButton *)sender {

    [self stopAnimation];

    CABasicAnimation *anim = [CABasicAnimation animation];

    anim.keyPath = @"transform.rotation";

    anim.toValue = @(2 * M_PI * 3);

    anim.duration = 2;

    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    anim.delegate = self;

    [self.centerView.layer addAnimation:anim forKey:nil];

    self.userInteractionEnabled = NO;

}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{

    self.userInteractionEnabled = YES;

    //选中的视图置顶居中

    self.centerView.transform = CGAffineTransformMakeRotation(-(self.selectedBtn.tag * M_PI / 6));

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [self startAnimation];//1秒之后开始旋转

    });

}

@end


#import <UIKit/UIKit.h>


@interface WButton : UIButton


@end


#import "WButton.h"


@implementation WButton


- (CGRect)imageRectForContentRect:(CGRect)contentRect

{

    CGFloat imageW = 40;

    CGFloat imageH = 47;

    CGFloat imageX = (contentRect.size.width - imageW) * 0.5;

    CGFloat imageY = 20;

    return CGRectMake(imageX, imageY, imageW, imageH);

}


- (void)setHighlighted:(BOOL)highlighted

{

    

}

@end


使用

#import "ViewController.h"

#import "WheelView.h"

@interface ViewController ()

- (IBAction)stop:(id)sender;

@property (nonatomic, strong) WheelView *wheel;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    WheelView *wheel = [WheelView wheelView];

    wheel.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);

    [wheel startAnimation];

    [self.view addSubview:wheel];

    self.wheel = wheel;

    

}


demo: http://download.csdn.net/detail/baitxaps/8953773
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值