@interfaceACAnimatablePropertiesViewController(){CALayer*layer;}@endstaticCGFloatwidth=150.0;@implementationACAnimatablePropertiesViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.layer=[[CALayeralloc]init];layer.bounds=CGRectMake(0,0,width,width);layer.position=self.view.center;layer.backgroundColor=[UIColorblueColor].CGColor;layer.cornerRadius=width/2;layer.shadowColor=[UIColorgrayColor].CGColor;layer.shadowOffset=CGSizeMake(2,3);layer.shadowOpacity=0.9;layer.borderWidth=2.0;layer.borderColor=[UIColorgreenColor].CGColor;[self.view.layeraddSublayer:layer];}-(void)touchesEnded:(NSSet<UITouch*>*)toucheswithEvent:(UIEvent*)event{UITouch*touch=[touchesanyObject];CGFloatrandWidth=arc4random()%200+100;layer.bounds=CGRectMake(0,0,randWidth,randWidth);layer.cornerRadius=randWidth/2;layer.position=[touchlocationInView:self.view];layer.backgroundColor=[UIColorcolorWithRed:arc4random()%225/225.0green:arc4random()%225/225.0blue:arc4random()%225/225.0alpha:1.0].CGColor;layer.borderColor=[UIColorcolorWithRed:arc4random()%225/225.0green:arc4random()%225/225.0blue:arc4random()%225/225.0alpha:1.0].CGColor;}@end
@interfaceACAnchorPointViewController()<UITextFieldDelegate>{CALayer*layer;}@property(weak,nonatomic)IBOutletUITextField*xField;@property(weak,nonatomic)IBOutletUITextField*yField;@end@implementationACAnchorPointViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.layer=[[CALayeralloc]init];layer.backgroundColor=[UIColorlightGrayColor].CGColor;layer.position=self.view.center;layer.bounds=CGRectMake(0,0,150.0,150.0);CABasicAnimation*rotationAnimation=[CABasicAnimationanimationWithKeyPath:@"transform.rotation.z"];rotationAnimation.duration=2;rotationAnimation.repeatCount=HUGE_VALF;rotationAnimation.removedOnCompletion=NO;rotationAnimation.fromValue=[NSNumbernumberWithFloat:0];rotationAnimation.toValue=[NSNumbernumberWithFloat:3.1415926*2];[layeraddAnimation:rotationAnimationforKey:@"rotationTransform"];[self.view.layeraddSublayer:layer];}#pragma mark - UITextFieldDelegate-(BOOL)textFieldShouldReturn:(UITextField*)textField{doublevalue=textField.text.length?textField.text.doubleValue:0.5;if(value<0||value>1){UIAlertController*alert=[UIAlertControlleralertControllerWithTitle:@"输入值必须介于0到1"message:nilpreferredStyle:UIAlertControllerStyleAlert];[alertaddAction:[UIAlertActionactionWithTitle:@"我知道了"style:UIAlertActionStyleCancelhandler:nil]];[selfpresentViewController:alertanimated:YEScompletion:nil];returnNO;}if(textField==self.xField){CGPointpoint=layer.anchorPoint;point.x=value;layer.anchorPoint=point;}else{CGPointpoint=layer.anchorPoint;point.y=value;layer.anchorPoint=point;}[textFieldresignFirstResponder];returnYES;}@end
@interfaceACDrawLayerByDelegateViewController(){CALayer*layer;}@endstaticCGFloatwidth=150.0;@implementationACDrawLayerByDelegateViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.layer=[[CALayeralloc]init];layer.bounds=CGRectMake(0,0,width,width);layer.position=self.view.center;layer.backgroundColor=[UIColorblueColor].CGColor;layer.cornerRadius=width/2;layer.masksToBounds=YES;layer.borderWidth=2.0;layer.borderColor=[UIColorgrayColor].CGColor;layer.delegate=self;[self.view.layeraddSublayer:layer];[layersetNeedsDisplay];}-(void)dealloc{layer.delegate=nil;}#pragma mark - layer delegate-(void)drawLayer:(CALayer*)layerinContext:(CGContextRef)ctx{CGContextSaveGState(ctx);CGContextScaleCTM(ctx,1,-1);CGContextTranslateCTM(ctx,0,-width);UIImage*image=[UIImageimageNamed:@"avatar.png"];CGContextDrawImage(ctx,CGRectMake(0,0,width,width),image.CGImage);CGContextRestoreGState(ctx);}@end
@interfaceACDrawLayerCustomViewController()@end@implementationACDrawLayerCustomViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.ACCustomLayer*layer=[[ACCustomLayeralloc]init];layer.bounds=CGRectMake(0,0,width,width);layer.position=self.view.center;layer.backgroundColor=[UIColorblueColor].CGColor;[self.view.layeraddSublayer:layer];[layersetNeedsDisplay];}@end
@interfaceACCircleAvatarWithShadowViewController(){CALayer*layer;}@endstaticCGFloatwidth=150.0;@implementationACCircleAvatarWithShadowViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.CALayer*shadowLayer=[[CALayeralloc]init];shadowLayer.bounds=CGRectMake(0,0,width,width);shadowLayer.position=self.view.center;shadowLayer.backgroundColor=[UIColorwhiteColor].CGColor;shadowLayer.cornerRadius=width/2;shadowLayer.shadowOffset=CGSizeMake(2,2);shadowLayer.shadowColor=[UIColorgrayColor].CGColor;shadowLayer.shadowOpacity=1.0;[self.view.layeraddSublayer:shadowLayer];layer=[[CALayeralloc]init];layer.bounds=CGRectMake(0,0,width,width);layer.position=self.view.center;layer.backgroundColor=[UIColorblueColor].CGColor;layer.cornerRadius=width/2;layer.masksToBounds=YES;layer.borderWidth=2.0;layer.borderColor=[UIColorgrayColor].CGColor;layer.delegate=self;[self.view.layeraddSublayer:layer];[layersetNeedsDisplay];}-(void)dealloc{layer.delegate=nil;}#pragma mark - layer delegate-(void)drawLayer:(CALayer*)layerinContext:(CGContextRef)ctx{CGContextSaveGState(ctx);CGContextScaleCTM(ctx,1,-1);CGContextTranslateCTM(ctx,0,-width);UIImage*image=[UIImageimageNamed:@"avatar.png"];CGContextDrawImage(ctx,CGRectMake(0,0,width,width),image.CGImage);CGContextRestoreGState(ctx);}@end
@interfaceACMoveAnimationViewController(){CALayer*petalLayer;}@end@implementationACMoveAnimationViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.UIImage*backgroundImage=[UIImageimageNamed:@"background.jpg"];self.view.layer.contents=(id)backgroundImage.CGImage;petalLayer=[[CALayeralloc]init];petalLayer.bounds=CGRectMake(0,0,10,20);petalLayer.position=CGPointMake(50,150);petalLayer.contents=(id)[UIImageimageNamed:@"petal.png"].CGImage;[self.view.layeraddSublayer:petalLayer];}-(void)touchesEnded:(NSSet<UITouch*>*)toucheswithEvent:(UIEvent*)event{UITouch*touch=touches.anyObject;[selfanimationMoveTo:[touchlocationInView:self.view]];}#pragma mark - Animation-(void)animationMoveTo:(CGPoint)location{CABasicAnimation*moveAnimation=[CABasicAnimationanimationWithKeyPath:@"position"];moveAnimation.toValue=[NSValuevalueWithCGPoint:location];moveAnimation.duration=5.0;moveAnimation.repeatCount=0;moveAnimation.removedOnCompletion=YES;[petalLayeraddAnimation:moveAnimationforKey:@"petalLayer_moveAnimation"];}@end
@interfaceACMoveAnimationFullEditionViewController(){CALayer*petalLayer;BOOLisMoving;}@end@implementationACMoveAnimationFullEditionViewController-(void)viewDidLoad{[superviewDidLoad];isMoving=NO;UIImage*backgroundImage=[UIImageimageNamed:@"background.jpg"];self.view.layer.contents=(id)backgroundImage.CGImage;petalLayer=[[CALayeralloc]init];petalLayer.bounds=CGRectMake(0,0,10,20);petalLayer.position=CGPointMake(50,150);petalLayer.contents=(id)[UIImageimageNamed:@"petal.png"].CGImage;[self.view.layeraddSublayer:petalLayer];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];// Dispose of any resources that can be recreated.}-(void)touchesEnded:(NSSet<UITouch*>*)toucheswithEvent:(UIEvent*)event{if(isMoving){return;}UITouch*touch=touches.anyObject;[selfanimationMoveTo:[touchlocationInView:self.view]];}#pragma mark - Animation-(void)animationMoveTo:(CGPoint)location{CABasicAnimation*moveAnimation=[CABasicAnimationanimationWithKeyPath:@"position"];moveAnimation.toValue=[NSValuevalueWithCGPoint:location];moveAnimation.duration=[selfpetalMoveDuring:location];moveAnimation.repeatCount=0;moveAnimation.removedOnCompletion=YES;moveAnimation.delegate=self;[moveAnimationsetValue:[NSValuevalueWithCGPoint:location]forKey:@"petalLayer_moveAnimation_destination"];[petalLayeraddAnimation:moveAnimationforKey:@"petalLayer_moveAnimation"];}-(NSTimeInterval)petalMoveDuring:(CGPoint)destination{CGFloatdeltaX=destination.x-petalLayer.position.x;CGFloatdeltaY=destination.y-petalLayer.position.y;CGFloatdistance=sqrt(deltaX*deltaX+deltaY*deltaY);return(NSTimeInterval)distance/50.0;}#pragma mark - Animation Delegate-(void)animationDidStart:(CAAnimation*)anim{isMoving=YES;}-(void)animationDidStop:(CAAnimation*)animfinished:(BOOL)flag{[CATransactionbegin];//禁用隐式动画[CATransactionsetDisableActions:YES];petalLayer.position=[[animvalueForKey:@"petalLayer_moveAnimation_destination"]CGPointValue];[CATransactioncommit];isMoving=NO;}@end
@interfaceACKeyFrameAnimationViewController(){CALayer*petalLayer;}@end@implementationACKeyFrameAnimationViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.UIImage*backgroundImage=[UIImageimageNamed:@"background.jpg"];self.view.layer.contents=(id)backgroundImage.CGImage;petalLayer=[[CALayeralloc]init];petalLayer.bounds=CGRectMake(0,0,10,20);petalLayer.position=CGPointMake(50,150);petalLayer.contents=(id)[UIImageimageNamed:@"petal.png"].CGImage;[self.view.layeraddSublayer:petalLayer];[selfaddKeyframeMoveAnimation];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];// Dispose of any resources that can be recreated.}#pragma mark - animation-(void)addKeyframeMoveAnimation{CAKeyframeAnimation*keyframeAnimation=[CAKeyframeAnimationanimationWithKeyPath:@"position"];//关键帧动画的初始值不能省略NSValue*key1=[NSValuevalueWithCGPoint:petalLayer.position];NSValue*key2=[NSValuevalueWithCGPoint:CGPointMake(80,320)];NSValue*key3=[NSValuevalueWithCGPoint:CGPointMake(45,400)];NSValue*key4=[NSValuevalueWithCGPoint:CGPointMake(55,500)];NSArray*values=@[key1,key2,key3,key4];keyframeAnimation.values=values;keyframeAnimation.duration=8.0;keyframeAnimation.beginTime=CACurrentMediaTime()+2;keyframeAnimation.delegate=self;[keyframeAnimationsetValue:key4forKey:@"petalLayer_keyframeAnimation_destination"];[petalLayeraddAnimation:keyframeAnimationforKey:@"petalLayer_keyframeAnimation_position"];}#pragma mark - animation delegate-(void)animationDidStart:(CAAnimation*)anim{}-(void)animationDidStop:(CAAnimation*)animfinished:(BOOL)flag{[CATransactionbegin];[CATransactionsetDisableActions:YES];petalLayer.position=[[animvalueForKey:@"petalLayer_keyframeAnimation_destination"]CGPointValue];[CATransactioncommit];}@end
@interfaceACGroupAnimationViewController(){CALayer*petalLayer;}@end@implementationACGroupAnimationViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.UIImage*backgroundImage=[UIImageimageNamed:@"background.jpg"];self.view.layer.contents=(id)backgroundImage.CGImage;petalLayer=[[CALayeralloc]init];petalLayer.bounds=CGRectMake(0,0,10,20);petalLayer.position=CGPointMake(100,150);petalLayer.contents=(id)[UIImageimageNamed:@"petal.png"].CGImage;[self.view.layeraddSublayer:petalLayer];[selfaddAnimationGroup];}-(void)addAnimationGroup{CAAnimationGroup*animationGroup=[CAAnimationGroupanimation];CABasicAnimation*rotateAnimation=[selfrotateAnimation];CAKeyframeAnimation*keyframeMoveAnimation=[selfkeyframeMoveAnimation];animationGroup.animations=@[rotateAnimation,keyframeMoveAnimation];animationGroup.delegate=self;animationGroup.duration=8.0;animationGroup.beginTime=CACurrentMediaTime()+2;[petalLayeraddAnimation:animationGroupforKey:nil];}-(CAKeyframeAnimation*)keyframeMoveAnimation{CAKeyframeAnimation*keyframeAnimation=[CAKeyframeAnimationanimationWithKeyPath:@"position"];CGMutablePathRefpath=CGPathCreateMutable();CGPathMoveToPoint(path,NULL,petalLayer.position.x,petalLayer.position.y);CGPathAddCurveToPoint(path,NULL,300,250,-100,450,100,550);keyframeAnimation.path=path;CGPathRelease(path);[keyframeAnimationsetValue:[NSValuevalueWithCGPoint:CGPointMake(100,550)]forKey:@"petalLayer_keyframeAnimation_destination"];returnkeyframeAnimation;}-(CABasicAnimation*)rotateAnimation{CABasicAnimation*rotateAnimation=[CABasicAnimationanimationWithKeyPath:@"transform.rotation.z"];rotateAnimation.repeatCount=HUGE_VALF;rotateAnimation.removedOnCompletion=NO;rotateAnimation.autoreverses=YES;rotateAnimation.toValue=[NSNumbernumberWithFloat:M_PI_2*3];[rotateAnimationsetValue:[NSNumbernumberWithFloat:M_PI_2*3]forKey:@"rotateAnimation_toValue"];returnrotateAnimation;}#pragma mark - animation delegate-(void)animationDidStop:(CAAnimation*)animfinished:(BOOL)flag{CAAnimationGroup*animationGroup=(CAAnimationGroup*)anim;CABasicAnimation*rotateAnimation=(CABasicAnimation*)animationGroup.animations[0];CAKeyframeAnimation*keyframeMoveAnimation=(CAKeyframeAnimation*)animationGroup.animations[1];CGFloattoValue=[[rotateAnimationvalueForKey:@"rotateAnimation_toValue"]floatValue];CGPointtoPoint=[[keyframeMoveAnimationvalueForKey:@"petalLayer_keyframeAnimation_destination"]CGPointValue];[CATransactionbegin];[CATransactionsetDisableActions:YES];petalLayer.position=toPoint;petalLayer.transform=CATransform3DMakeRotation(toValue,0,0,1);[CATransactioncommit];}@end
@interfaceACTransitionAnimationViewController(){UIImageView*imageView;NSArray*transitionTypes;}@endstaticinttransitionTypeIndex;@implementationACTransitionAnimationViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.transitionTypes=@[@"fade",@"moveIn",@"push",@"reveal",@"cube",@"oglFlip",@"suckEffect",@"rippleEffect",@"pageCurl",@"pageUnCurl",@"cameraIrisHollowOpen",@"cameraIrisHollowClose"];transitionTypeIndex=0;imageView=[[UIImageViewalloc]initWithFrame:[UIScreenmainScreen].bounds];[imageViewsetImage:[UIImageimageNamed:@"background.jpg"]];[self.viewaddSubview:imageView];UISwipeGestureRecognizer*swipeGesture=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;[self.viewaddGestureRecognizer:swipeGesture];}-(void)swipe:(UISwipeGestureRecognizer*)gestureRecognizer{if(transitionTypeIndex>11){transitionTypeIndex=0;}CATransition*transition=[[CATransitionalloc]init];transition.type=transitionTypes[transitionTypeIndex];transition.subtype=kCATransitionFromRight;transition.duration=1.0;[imageView.layeraddAnimation:transitionforKey:@"imageView_transition"];transitionTypeIndex++;}@end
@interfaceACViewSpringAnimationViewController(){UIImageView*ballView;}@end@implementationACViewSpringAnimationViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.ballView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"ball"]];ballView.center=self.view.center;[self.viewaddSubview:ballView];}-(void)touchesEnded:(NSSet<UITouch*>*)toucheswithEvent:(UIEvent*)event{UITouch*touch=touches.anyObject;CGPointlocation=[touchlocationInView:self.view];/*创建弹性动画 damping:阻尼,范围0-1,阻尼越接近于0,弹性效果越明显 velocity:弹性复位的速度 */[UIViewanimateWithDuration:[selfpetalMoveDuring:location]delay:0usingSpringWithDamping:0.5initialSpringVelocity:0options:UIViewAnimationOptionCurveLinearanimations:^{ballView.center=location;}completion:nil];}-(NSTimeInterval)petalMoveDuring:(CGPoint)destination{CGFloatdeltaX=destination.x-ballView.center.x;CGFloatdeltaY=destination.y-ballView.center.y;CGFloatdistance=sqrt(deltaX*deltaX+deltaY*deltaY);NSLog(@"%f",distance);return(NSTimeInterval)distance/150.0;}@end
@interfaceACViewKeyframeAnimationViewController(){UIImageView*petalView;}@end@implementationACViewKeyframeAnimationViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.self.view.layer.contents=(id)[UIImageimageNamed:@"background.jpg"].CGImage;petalView=[[UIImageViewalloc]init];petalView.frame=CGRectMake(50,150,0,0);petalView.image=[UIImageimageNamed:@"petal"];[petalViewsizeToFit];[self.viewaddSubview:petalView];[selfaddKeyframeAnimation];}-(void)addKeyframeAnimation{[UIViewanimateKeyframesWithDuration:5.0delay:0.0options:UIViewKeyframeAnimationOptionCalculationModeLinearanimations:^{[UIViewaddKeyframeWithRelativeStartTime:0.0relativeDuration:0.5animations:^{petalView.center=CGPointMake(80.0,220.0);}];[UIViewaddKeyframeWithRelativeStartTime:0.5relativeDuration:0.25animations:^{petalView.center=CGPointMake(45.0,300.0);}];[UIViewaddKeyframeWithRelativeStartTime:0.75relativeDuration:0.25animations:^{petalView.center=CGPointMake(55.0,400.0);}];}completion:nil];}@end
@interfaceACViewTransitionAnimationViewController(){UIImageView*imageView;NSArray*transitionTypes;}@endstaticinttransitionTypeIndex;@implementationACViewTransitionAnimationViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.transitionTypes=@[[NSNumbernumberWithUnsignedInteger:UIViewAnimationOptionTransitionFlipFromRight],[NSNumbernumberWithUnsignedInteger:UIViewAnimationOptionTransitionCurlUp],[NSNumbernumberWithUnsignedInteger:UIViewAnimationOptionTransitionCrossDissolve],[NSNumbernumberWithUnsignedInteger:UIViewAnimationOptionTransitionFlipFromBottom]];transitionTypeIndex=0;imageView=[[UIImageViewalloc]initWithFrame:[UIScreenmainScreen].bounds];[imageViewsetImage:[UIImageimageNamed:@"background.jpg"]];[self.viewaddSubview:imageView];UISwipeGestureRecognizer*swipeGesture=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;[self.viewaddGestureRecognizer:swipeGesture];}-(void)swipe:(UISwipeGestureRecognizer*)gestureRecognizer{if(transitionTypeIndex>3){transitionTypeIndex=0;}UIViewAnimationOptionsoption;option=[transitionTypes[transitionTypeIndex]unsignedIntegerValue];option=option|UIViewAnimationOptionCurveLinear;[UIViewtransitionWithView:imageViewduration:1.0options:optionanimations:^{[imageViewsetImage:[UIImageimageNamed:@"background.jpg"]];}completion:nil];transitionTypeIndex++;}@end