IOS big turntable lottery, ios big Turntable
Function
Click the big turntable and rotate it to a position you can determine.
Structure
Turntable, start button, pointer
Technology
CADisplayLink does not stop re-painting, CGAffineTransform rotation, simple mathematical formula
Core code
1. Use CADisplayLink to repeatedly redraw the rotating Chassis
// Start rotating (always rotating)-(void) startRotate {CADisplayLink * link = [CADisplayLink displayLinkWithTarget: self selector: @ selector (Rotate)]; [link addToRunLoop: [nsunloop mainRunLoop] forMode: NSDefaultRunLoopMode]; self. link = link;} //-(void) Rotate {// Rotate 6 ° self each time. rotateWheel. transform = CGAffineTransformRotate (self. rotateWheel. transform, M_PI * 2/12/60 );}
2. Click start to execute the rotation animation,
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: @ "tip" message: @ "Congratulations! Cheated !!! "Delegate: self cancelButtonTitle: @" OK "otherButtonTitles: nil, nil]; [alert show]; if (_ delegate & [_ delegate respondsToSelector: @ selector (operator :)]) {[_ delegate LuckyDrawViewDidFinishWidthIndex: self. numberIndex];} self. numberIndex = 1 ;});}
3. Use a variable to control the last stop position of the turntable.
@property (nonatomic,assign)NSInteger numberIndex;
Demo link: http://pan.baidu.com/s/1dDPimiP