Detailed Implementation of the turntable Function
Detailed Implementation of the turntable Function
I. Custom turntable XIB
+ (Instancetype) wheelView
{
Return [[NSBundlemainBundle] loadNibNamed: @ "HMWheelView" owner: niloptions: nil] [0];
}
2. Add button # Add warning button
-(Void) awakeFromNib
{// After loading XIB, start the turntable self. link. paused = NO;
_ RotationView. userInteractionEnabled = YES;
// Cropped large image
UIImage * bigImage = [UIImageimageNamed: @ "LuckyAstrology"];
UIImage * selectedImage = [UIImageimageNamed: @ "LuckyAstrologyPressed"];
// Image size
CGFloat imageW = 40 * [UIScreenmainScreen]. scale;
CGFloat imageH = 47 * [UIScreenmainScreen]. scale;
For (inti = 0; I <12; I ++ ){
// Create button
HMWheelButton * button = [HMWheelButtonbuttonWithType: UIButtonTypeCustom];
// Anchor
Button. layer. anchorPoint = CGPointMake (0.5, 1 );
// Location
Button. layer. position = CGPointMake (self. bounds. size. width * 0.5, self. bounds. size. height * 0.5 );
// Rotate the button
Button. layer. transform = CATransform3DMakeRotation (angle 2radian (I * 30), 0, 0, 1 );
// Size
Button. bounds = CGRectMake (0, 0, 68,143 );
// Set the selected background image
[Button setBackgroundImage: [UIImageimageNamed: @ "LuckyRototeSelected"] forState: UIControlStateSelected];
// Set the button Image
// Image: The cropped image.
// Rect: Crop size
CGRect clipRect = CGRectMake (I * imageW, 0, imageW, imageH );
CGImageRef smallImage = CGImageCreateWithImageInRect (bigImage. CGImage, clipRect );
[Button setImage: [UIImageimageWithCGImage: smallImage] forState: UIControlStateNormal];
// Set the selected Image
CGImageRef selectedSmallImage = CGImageCreateWithImageInRect (selectedImage. CGImage, clipRect );
[Button setImage: [UIImageimageWithCGImage: selectedSmallImage] forState: UIControlStateSelected];
// Listen for click events
[Button addTarget: selfaction: @ selector (btnClick :) forControlEvents: UIControlEventTouchDown];
// The first one is selected by default.
If (I = 0 ){
[SelfbtnClick: button];
}
[_ RotationViewaddSubview: button];
}
}
Iii. Listener method # click the warning listener button
-(Void) btnClick :( UIButton *) button
{
_ SelectedButton. selected = NO;
Button. selected = YES;
_ SelectedButton = button;
}
Iv. Define the timer-(CADisplayLink *) link
{
If (_ link = nil ){
CADisplayLink * link = [CADisplayLinkdisplayLinkWithTarget: selfselector: @ selector (update)];
[Link addToRunLoop: [nsunloopmainrunloop] forMode: NSDefaultRunLoopMode];
_ Link = link;
}
Return _ link;
}
-(Void) update
{
_ RotationView. transform = CGAffineTransformRotate (_ rotationView. transform, (45/60 .0)/180.0 * M_PI ));}
V. Start code Selection
-(IBAction) start :( id) sender {
// 1. Do not interact with users
_ RotationView. userInteractionEnabled = NO;
// 2. Cancel the slowly rotating
_ Link. paused = YES;
CABasicAnimation * anim = [CABasicAnimationanimation];
Anim. keyPath = @ "transform. rotation ";
Anim. toValue = @ (M_PI * 2*3 );
Anim. duration= 0.5;
Anim. delegate = self;
[_ RotationView. layeraddAnimation: animforKey: nil];
}
6. Proxy method for implementing CAAnimationDelegate
-(Void) animationDidStop :( CAAnimation *) anim finished :( BOOL) flag
{
_ RotationView. userInteractionEnabled = YES;
// Return the selected button to the center above:
CGFloat angle = atan2 (_ selectedButton. transform. B, _ selectedButton. transform. );
// Rotate our turntable so many degrees in reverse direction
_ RotationView. transform = CGAffineTransformMakeRotation (-angle );
Dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (2 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {
[SelfstartRotating];
});
}