/*
Caanimationgroup Animation Group
1. An array of animations animations
2, the BeginTime start time
3, the animation group set the "duration (duration)", may cause animation group inside the "animation duration" Invalid
*/
#import "ViewController.h"
@interface Viewcontroller ()
{
Calayer *petal;
}
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
[Self addbgview];
[Self addanimationgroup];
[Self addpetallayer];
}
#pragma mark-Add a background map
-(void) Addbgview {
Uiimageview *bgview = [[Uiimageview alloc] initWithFrame:self.view.frame];
Bgview.image = [UIImage imagenamed:@ "deciduous. jpg"];
[Self.view Addsubview:bgview];
[Self addpetallayer];
}
#pragma mark-Add a picture to animate
-(void) Addpetallayer {
UIImage *image = [UIImage imagenamed:@ "petal.jpg"];
Petal = [[Calayer alloc] init];
Petal.position = Cgpointmake (100, 200);
Petal.bounds = CGRectMake (0, 0, image.size.width, image.size.height);
Petal.contents = (ID) image. Cgimage;
[Self.view.layer Addsublayer:petal];
}
#pragma mark-Rotate animation ("Base animation")
-(Cabasicanimation *) rotationanimation {
Cabasicanimation *rotation = [cabasicanimation animationwithkeypath:@ "Transform.rotation.z"]; Rotate Z Axis
Rotation.tovalue = @ (m_pi_2*3); Rotation angle
Rotation.removedoncompletion = NO;
return rotation;
}
#pragma mark-The Falling animation
-(Cakeyframeanimation *) dropanimation {
Cakeyframeanimation *drop = [cakeyframeanimation animationwithkeypath:@ "position"];
Cgpathref pathref = cgpathcreatemutable ();
Cgpathmovetopoint (Pathref, NULL, petal.position.x, PETAL.POSITION.Y);
Cgpathaddcurvetopoint cp1x y cpx y set two points draw a curve between these two points
X y termination Point
Cgpoint EndPoint = Cgpointmake (40, 500);
Cgpathaddcurvetopoint (Pathref, NULL, 280, -30, Endpoint.x, ENDPOINT.Y);
Drop.path = Pathref;
Cgpathrelease (PATHREF);
return drop;
}
#pragma mark-Add animation group
-(void) Addanimationgroup {
Caanimationgroup *animationgroup = [Caanimationgroup animation];
Animationgroup.animations = @[[self Rotationanimation], [self dropanimation]];
Animationgroup.duration = 10;
BeginTime Animation start time
Cacurrentmediatime () Gets the current time from the start of calling the method starting 10 seconds after execution
Animationgroup.begintime = Cacurrentmediatime () + 1;
Animationgroup.removedoncompletion = NO;
Animationgroup.fillmode = Kcafillmodeboth;
Animationgroup.repeatcount = 5;
[Petal addanimation:animationgroup forkey:@ "group"];
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
@end
Animation Group (petals) falling with curves