- #define Kdegreestoradian (x) (M_PI * (x)/180.0)
- #define Kradiantodegrees (Radian) (radian*180.0)/(M_PI)
- -(void) viewdidload
- {
- [Superviewdidload];
- Self.title = @ "Test animation";
- Self.view.backgroundColor = [Uicolorlightgraycolor];
- MyTest1 = [[Uilabelalloc]initwithframe:cgrectmake (10, 100, 60, 40)];
- Mytest1.backgroundcolor = [Uicolorbluecolor];
- Mytest1.textalignment = Nstextalignmentcenter;
- Mytest1.text = @ "Zhang Mingwei";
- Mytest1.textcolor = [Uicolorwhitecolor];
- [Self.viewaddSubview:myTest1];
- Flashing effect.
- [Mytest1.layer addanimation:[self opacityforever_animation:0.5] forkey:nil];
- Move the animation.
- [Mytest1.layer addanimation:[self movex:1.0f x:[nsnumber numberwithfloat:200.0f]] forkey:nil];
- The zoom effect.
- [Mytest1.layer addanimation:[self scale:[nsnumber numberwithfloat:1.0f] Orgin:[nsnumber numberWithFloat:3.0f] durtimes:2.0f Rep:maxfloat] Forkey:nil];
- Combining animations.
- Nsarray *myarray = [Nsarray arraywithobjects:[self opacityforever_animation:0.5],[self moveX:1.0f X:[NSNumber number Withfloat:200.0f]],[self scale:[nsnumber numberwithfloat:1.0f] Orgin:[nsnumber numberwithfloat:3.0f] durTimes:2.0f Rep:maxfloat], nil];
- [Mytest1.layer addanimation:[self groupanimation:myarray durtimes:3.0f rep:maxfloat] forKey:nil];
- Path animation.
- Cgmutablepathref Mypah = cgpathcreatemutable ();
- Cgpathmovetopoint (Mypah, nil,30, 77);
- Cgpathaddcurvetopoint (Mypah, Nil, 50, 50, 60, 200, 200, 200);//Here is the control point.
- [Mytest1.layer addanimation:[self keyframeanimation:mypah durtimes:5 rep:maxfloat] forKey:nil];
- Rotate the animation.
- [Mytest1.layeraddanimation:[selfrotation:2degree:kradiantodegrees (Direction:1repeatcount:maxfloat] ForKey: NIL];
- }
- #pragma mark = = Perpetual flashing animated ======
- -(Cabasicanimation *) Opacityforever_animation: (float) time
- {
- Cabasicanimation *animation = [cabasicanimation animationwithkeypath:@ "opacity"];//must write opacity.
- Animation.fromvalue = [NSNumber numberwithfloat:1.0f];
- Animation.tovalue = [NSNumber numberwithfloat:0.0f];//this is transparency.
- animation.autoreverses = YES;
- Animation.duration = time;
- Animation.repeatcount = maxfloat;
- Animation.removedoncompletion = NO;
- Animation.fillmode = Kcafillmodeforwards;
- Animation.timingfunction=[camediatimingfunction functionwithname:kcamediatimingfunctioneasein];///No words are evenly animated.
- return animation;
- }
- #pragma mark ===== Horizontal, vertical movement ===========
- -(Cabasicanimation *) MoveX: (float) time x: (NSNumber *) x
- {
- Cabasicanimation *animation = [cabasicanimation animationwithkeypath:@ "transform.translation.x"];///.y then move down.
- Animation.tovalue = x;
- Animation.duration = time;
- Animation.removedoncompletion = No;//yes, and return to the original position.
- Animation.repeatcount = maxfloat;
- Animation.fillmode = Kcafillmodeforwards;
- return animation;
- }
- #pragma mark ===== Zoom-=============
- -(Cabasicanimation *) Scale: (NSNumber *) Multiple orgin: (NSNumber *) orginmultiple durtimes: (float) time Rep: (float) Reperttimes
- {
- Cabasicanimation *animation = [cabasicanimation animationwithkeypath:@ "Transform.scale"];
- Animation.fromvalue = multiple;
- Animation.tovalue = Orginmultiple;
- animation.autoreverses = YES;
- Animation.repeatcount = Reperttimes;
- Animation.duration = time;//When not set, there is a default zoom time.
- Animation.removedoncompletion = NO;
- Animation.fillmode = Kcafillmodeforwards;
- return animation;
- }
- #pragma mark ===== Combo animation-=============
- -(Caanimationgroup *) Groupanimation: (Nsarray *) animationary durtimes: (float) time Rep: (float) repeattimes
- {
- Caanimationgroup *animation = [Caanimationgroupanimation];
- Animation.animations = animationary;
- Animation.duration = time;
- Animation.removedoncompletion = NO;
- Animation.repeatcount = RepeatTimes;
- Animation.fillmode = Kcafillmodeforwards;
- return animation;
- }
- #pragma mark ===== Path animation-=============
- -(Cakeyframeanimation *) Keyframeanimation: (cgmutablepathref) path durtimes: (float) time Rep: (float) repeattimes
- {
- Cakeyframeanimation *animation = [cakeyframeanimation animationwithkeypath:@ "position"];
- Animation.path = path;
- Animation.removedoncompletion = NO;
- Animation.fillmode = Kcafillmodeforwards;
- Animation.timingfunction = [Camediatimingfunction Functionwithname:kcamediatimingfunctioneasein];
- animation.autoreverses = NO;
- Animation.duration = time;
- Animation.repeatcount = RepeatTimes;
- return animation;
- }
- #pragma mark = = Rotation animation ======
- -(Cabasicanimation *) Rotation: (float) dur degree: (float) degree direction: (int) Direction repeatcount: (int) RepeatCount
- {
- Catransform3d rotationtransform = catransform3dmakerotation (degree, 0, 0, direction);
- Cabasicanimation *animation = [cabasicanimation animationwithkeypath:@ "transform"];
- Animation.tovalue = [Nsvalue valuewithcatransform3d:rotationtransform];
- Animation.duration = dur;
- animation.autoreverses = NO;
- Animation.cumulative = NO;
- Animation.fillmode = Kcafillmodeforwards;
- Animation.repeatcount = RepeatCount;
- Animation.delegate = self;
- return animation;
- }
iOS simple animation effects: flashing, moving, rotating, path, combo