IOS uses transform and cabasicanimation to implement a view to rotate around a fixed point and to animate a rotated circle as an example of a carousel

Source: Internet
Author: User

As follows

Code of Implementation

1 #import "TurntableView.h"2 //#import "TurntableLabel.h"3 4 @implementationTurntableview5 {6Uiimageview *_BGIMGV;7 cgfloat _angle;8 }9-(Instancetype) initWithFrame: (CGRect) FrameTen { OneSelf =[Super Initwithframe:frame]; A     if(self) { - [self createsubviews]; -     } the     returnSelf ; - } -  -- (void) createsubviews{ +     //Nsarray *arr = @[@ "10", @ "20", @ "40", @ "50", @ "60", @ "80", @ "100", @ "140", @ "200", @ "300", @ "400", @ "500", @ "1000", @ "2000 ", @" 10000 "]; -Nsarray *arr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"Ten"]; +     //Background Image AUiimageview *BGIMGV = [[Uiimageview alloc]initwithframe:cgrectmake (0,0, -, -)]; atBgImgV.layer.cornerRadius = bgimgv.frame.size.width/2; -BgImgV.layer.masksToBounds =YES; -Bgimgv.center =Self.center; -Bgimgv.backgroundcolor = [Uicolor colorwithwhite:0Alpha0.5]; - [self ADDSUBVIEW:BGIMGV]; -_BGIMGV =BGIMGV; in      -     // toNsinteger i =0; +     //radius of the circle -CGFloat r = bgimgv.frame.size.width/2; the     //the angle of each fan of the turntable *CGFloat angle = m_pi/ the* the/Arr.count; $_angle =angle;Panax Notoginseng     //loop to create a number on a pie slice -      for(NSString *strincharr) { the          +UILabel *rewardmoneylabel = [UILabelNew]; ARewardmoneylabel.frame = CGRectMake (R,0, cos (angle/2) * R,2* Sin (angle/2) *R); theRewardmoneylabel.center = Cgpointmake (rewardmoneylabel.center.x,bgimgv.frame.size.height/2); +Rewardmoneylabel.textalignment =Nstextalignmentright; -         //Rewardmoneylabel.backgroundcolor = [Uicolor greencolor]; $Rewardmoneylabel.text =str; $ [BGIMGV Addsubview:rewardmoneylabel]; -          -         //Set the anchor point (in the view, which point is the center of rotation, (0,0) is the lower left corner, () is the upper-right corner, (0.5,0.5) is the center) theRewardMoneyLabel.layer.anchorPoint = Cgpointmake (0,0.5); -         //set the position of the rotationWuyiRewardMoneyLabel.layer.position =Cgpointmake (R, R); the         //Rotate -Rewardmoneylabel.transform = cgaffinetransformmakerotation (Angle *i); Wui + + ; -     } About      $     //Red Start button -UIButton *startbtn =[UIButton Buttonwithtype:uibuttontypecustom]; -Startbtn.frame = CGRectMake (0,0, -, -); -Startbtn.center =Cgpointmake (R, R); AStartbtn.backgroundcolor =[Uicolor Redcolor]; +     //Start button click event the [startbtn addtarget:self Action: @selector (startbtnaction:) forcontrolevents:uicontroleventtouchupinside]; - [self addsubview:startbtn]; $ } the //Click the Red Start button event the- (void) Startbtnaction: (uibutton*) sender{ the     //Create a base animation theCabasicanimation *animation = [cabasicanimationNew]; -     //set the properties of the animation to be changed inAnimation.keypath =@"transform.rotation.z"; the     //Animation.fromvalue = @ (_BGIMGV.LAYER.TRANSFORM.M11); the     //The value of the final property of the animation (Turn 7.5 turns) AboutAnimation.tovalue = @ (m_pi* the); the     //playback time of the animation theAnimation.duration =3; the     //animation effect slow-forward slow-out +Animation.timingfunction =[Camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout]; -     //solve the problem of returning to the original state after the animation ends theAnimation.removedoncompletion =NO;BayiAnimation.fillmode =kcafillmodeforwards; the     //add an animation to a layer on a view BGIMGV the[_bgimgv.layer addanimation:animation Forkey:@"Rotation"]; -      -      the     //two other methods of rotation the //[UIView animatewithduration:1 animations:^{ the ////_bgimgv.transform = Cgaffinetransformrotate (_bgimgv.transform, m_pi/180 + _angle); the //    }]; - //[UIView animatewithduration:1 animations:^{ the //_bgimgv.layer.transform = catransform3drotate (_bgimgv.layer.transform, _angle, 0, 0, 1); the //    }]; the }94  the @end

Finally, a summary of the keypath values of some commonly used animation

IOS uses transform and cabasicanimation to implement a view to rotate around a fixed point and to animate a rotated circle as an example of a carousel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.