• iOS大转盘抽奖


    功能

    点击大转盘旋转后固定到某个自己可以确定的位置

    结构

    转盘,开始按钮,指针

    技术

    CADisplayLink不停重绘,CGAffineTransform旋转,简单数学公式

    核心代码

    1.使用CADisplayLink不停重绘旋转底盘

    // 开始转动(一直不停的转动)
    - (void)startRotate
    {
        
        CADisplayLink* link = [CADisplayLink displayLinkWithTarget:self selector:@selector(Rotate)];
        [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
        self.link = link;
    }
    
    //
    - (void)Rotate
    
    {
        //每次旋转6°
        self.rotateWheel.transform = CGAffineTransformRotate(self.rotateWheel.transform, M_PI * 2 / 12/ 60 );
    }

    2.点击开始执行旋转动画,

      if (![self.rotateWheel.layer animationForKey:@"zhuandong"]) {
            
            CABasicAnimation* animation = [[CABasicAnimation alloc] init];
            
            animation.keyPath = @"transform.rotation";
            animation.toValue = @(2 * M_PI * 5 - M_PI*2/12*(13-self.numberIndex));
            animation.duration = 5;
            
            animation.removedOnCompletion = NO;
            animation.fillMode = kCAFillModeForwards;
            
            [self.rotateWheel.layer addAnimation:animation forKey:@"zhuandong"];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animation.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                self.rotateWheel.transform = CGAffineTransformMakeRotation(M_PI*2/12*(13-self.numberIndex));
                self.link.paused = YES;
                
                [self.rotateWheel.layer removeAnimationForKey:@"zhuandong"];
                
                UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"恭喜你!被骗了!!!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                
                [alert show];
                if (_delegate && [_delegate respondsToSelector:@selector(LuckyDrawViewDidFinishWidthIndex:)]) {
                    [_delegate LuckyDrawViewDidFinishWidthIndex:self.numberIndex];
                }
                self.numberIndex = 1;
                
            });
        }

    3.其间控制转盘最后停留的位置用一个变量控制就行了

    @property (nonatomic,assign)NSInteger numberIndex;

    效果图

    demo链接:http://pan.baidu.com/s/1dDPimiP

  • 相关阅读:
    Log Explorer的使用
    Windows消息大全
    Devepress LayoutControl的使用
    IIS7虚拟目录出现HTTP错误500.19(由于权限不足而无法读取配置文件)
    检索参数信息并填充指定的 SqlCommand 对象的 Parameters 集合
    Failed to access IIS metabase.
    SQL Server FOR XML PATH 语句的应用
    C#调用API:mouse_event 模拟鼠标事件
    C#执行SQL脚本
    ADO.NET 中的表达式
  • 原文地址:https://www.cnblogs.com/hxwj/p/5234465.html
Copyright © 2020-2023  润新知