objective c - UIView Animating a 2D Bouncing Ball (Squash & Stretch) in iOS -
looking animating view bouncing ball using squash & stretch, in cocoa using core animation...
1 - bouncing ball animation
;;;;
cabasicanimation *animmoveup = [cabasicanimation animationwithkeypath:@"position"]; animmoveup.timingfunction = [camediatimingfunction functionwithname:kcamediatimingfunctioneaseout]; animmoveup.fromvalue = [nsvalue valuewithcgpoint:_bottom]; animmoveup.tovalue = [nsvalue valuewithcgpoint:_top]; cabasicanimation *scaleanim1 = [cabasicanimation animationwithkeypath:@"transform"]; scaleanim1.tovalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(2.5, 2.0, 1.0)]; scaleanim1.tovalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(2.0, 2.0, 1.0)]; cabasicanimation *bounceanim1 = [cabasicanimation animationwithkeypath:@"transform"]; bounceanim1.fromvalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(2.0, 2.0, 1.0)]; bounceanim1.tovalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(3.0, 1.0, 1.0)]; cabasicanimation *animmovedown = [cabasicanimation animationwithkeypath:@"position"]; animmovedown.timingfunction = [camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout]; animmovedown.fromvalue = [nsvalue valuewithcgpoint:_top]; animmovedown.tovalue = [nsvalue valuewithcgpoint:_bottom]; cabasicanimation *scaleanim2 = [cabasicanimation animationwithkeypath:@"transform"]; scaleanim2.fromvalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(2.5, 2.0, 1.0)]; scaleanim2.tovalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(2.0, 2.0, 1.0)]; cabasicanimation *bounceanim2 = [cabasicanimation animationwithkeypath:@"transform"]; bounceanim2.fromvalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(2.0, 2.0, 1.0)]; bounceanim2.tovalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(3.0, 1.0, 1.0)]; cabasicanimation *animmoveup1 = [cabasicanimation animationwithkeypath:@"position"]; animmoveup1.timingfunction = [camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout]; animmoveup1.fromvalue = [nsvalue valuewithcgpoint:_bottom]; animmoveup1.tovalue = [nsvalue valuewithcgpoint:_top]; cabasicanimation *scaleanim3 = [cabasicanimation animationwithkeypath:@"transform"]; scaleanim3.fromvalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(2.5, 2.0, 1.0)]; scaleanim3.tovalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(2.0, 2.0, 1.0)]; cabasicanimation *bounceanim3 = [cabasicanimation animationwithkeypath:@"transform"]; bounceanim3.fromvalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(2.0, 2.0, 1.0)]; bounceanim3.tovalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(3.0, 1.0, 1.0)]; cabasicanimation *animmovedown2 = [cabasicanimation animationwithkeypath:@"position"]; animmovedown2.timingfunction = [camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout]; animmovedown2.fromvalue = [nsvalue valuewithcgpoint:_top]; animmovedown2.tovalue = [nsvalue valuewithcgpoint:_bottom]; cabasicanimation *scaleanim4 = [cabasicanimation animationwithkeypath:@"transform"]; scaleanim4.fromvalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(2.5, 2.0, 1.0)]; scaleanim4.tovalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(2.0, 2.0, 1.0)]; cabasicanimation *bounceanim4 = [cabasicanimation animationwithkeypath:@"transform"]; bounceanim4.fromvalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(2.0, 2.0, 1.0)]; bounceanim4.tovalue = [nsvalue valuewithcatransform3d:catransform3dmakescale(3.0, 1.0, 1.0)]; caanimationgroup *animgroup = [caanimationgroup animation]; animgroup.animations = @[animmoveup, scaleanim1, bounceanim1, animmovedown, scaleanim2, bounceanim2, animmoveup1, scaleanim3, bounceanim3, animmovedown2, scaleanim4, bounceanim4]; animgroup.duration = 2.5; animgroup.delegate = self; animgroup.fillmode = kcafillmodeforwards; animgroup.removedoncompletion = no; [_imgview.layer addanimation:animgroup forkey:nil];
the following code generate animation above:
cakeyframeanimation *trans = [cakeyframeanimation animationwithkeypath:@"transform.translation.y"]; nsarray *values = @[@(-200),@(20),@(0)]; trans.values = values; nsarray *times = @[@(0.0),@(0.85),@(1)]; trans.keytimes = times; trans.timingfunction = [camediatimingfunction functionwithname:kcamediatimingfunctioneasein]; trans.autoreverses = yes; trans.duration = 1.0; trans.repeatcount = infinity; cakeyframeanimation *scalexanimation = [cakeyframeanimation animationwithkeypath:@"transform.scale.x"]; nsarray *scalexvalues = @[@(0.75),@(0.75),@(1)]; scalexanimation.values = scalexvalues; nsarray *scalextimes = @[@(0.0),@(0.85),@(1)]; scalexanimation.keytimes = scalextimes; scalexanimation.timingfunction = [camediatimingfunction functionwithname:kcamediatimingfunctionlinear]; scalexanimation.autoreverses = yes; scalexanimation.duration = 1.0; scalexanimation.repeatcount = infinity; cakeyframeanimation *scaleyanimation = [cakeyframeanimation animationwithkeypath:@"transform.scale.y"]; nsarray *scaleyvalues = @[@(0.75),@(1),@(0.85)]; scaleyanimation.values = scaleyvalues; nsarray *scaleytimes = @[@(0.1),@(0.5),@(1)]; scaleyanimation.keytimes = scaleytimes; scaleyanimation.timingfunction = [camediatimingfunction functionwithname:kcamediatimingfunctionlinear]; scaleyanimation.autoreverses = yes; scaleyanimation.duration = 1.0; scaleyanimation.repeatcount = infinity; [_ballview.layer addanimation:scalexanimation forkey:@"scalex"]; [_ballview.layer addanimation:scaleyanimation forkey:@"scaley"]; [_ballview.layer addanimation:trans forkey:@"trans"];
Comments
Post a Comment