demo:
ChangeDemo.zip (112.8 KB)
关键代码:
- (void)changeCurrentSubView {
animationIsStop =NO;
[CATransactionbegin];
[CATransactionsetCompletionBlock:^(void){animationIsStop =YES;}];
[CATransactionsetValue:[NSNumbernumberWithFloat:1.0]forKey:kCATransactionAnimationDuration];
CABasicAnimation *shrinkAnimation = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];
shrinkAnimation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn];
shrinkAnimation.toValue = [NSNumbernumberWithFloat:0];
shrinkAnimation.fillMode=kCAFillModeForwards;
shrinkAnimation.removedOnCompletion=NO;
[[[imagesViewArrayobjectAtIndex:CurrentImageNumber]layer]addAnimation:shrinkAnimationforKey:@""];
CABasicAnimation *fadeAnimation = [CABasicAnimationanimationWithKeyPath:@"opacity"];
fadeAnimation.toValue = [NSNumbernumberWithFloat:0];
fadeAnimation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn];
fadeAnimation.fillMode=kCAFillModeForwards;
fadeAnimation.removedOnCompletion=NO;
[[[imagesViewArrayobjectAtIndex:CurrentImageNumber]layer]addAnimation:fadeAnimationforKey:@""];
CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
CGMutablePathRef positionPath = CGPathCreateMutable();
CGPathMoveToPoint(positionPath,NULL, [[imagesViewArrayobjectAtIndex:CurrentImageNumber]layer].position.x,
[[imagesViewArrayobjectAtIndex:CurrentImageNumber]layer].position.y);
CGPathAddQuadCurveToPoint(positionPath,NULL,
-[[imagesViewArrayobjectAtIndex:CurrentImageNumber]layer].position.x,
[[imagesViewArrayobjectAtIndex:CurrentImageNumber]layer].position.y,
[[imagesViewArrayobjectAtIndex:CurrentImageNumber]layer].position.x,
[[imagesViewArrayobjectAtIndex:CurrentImageNumber]layer].position.y);
positionAnimation.path = positionPath;
positionAnimation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn];
[[[imagesViewArrayobjectAtIndex:CurrentImageNumber]layer]addAnimation:positionAnimationforKey:@"positionAnimation"];
if (CurrentImageNumber >=imagesCount -1) {
CurrentImageNumber = -1;
}
CABasicAnimation *shrinkAnimation2 = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];
shrinkAnimation2.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn];
shrinkAnimation2.fromValue = [NSNumbernumberWithFloat:0.001];
shrinkAnimation2.toValue = [NSNumbernumberWithFloat:1.0];
shrinkAnimation2.fillMode=kCAFillModeForwards;
shrinkAnimation2.removedOnCompletion=NO;
[[[imagesViewArrayobjectAtIndex:CurrentImageNumber +1]layer]addAnimation:shrinkAnimation2forKey:@""];
CABasicAnimation *fadeAnimation2 = [CABasicAnimationanimationWithKeyPath:@"opacity"];
fadeAnimation2.toValue = [NSNumbernumberWithFloat:1.0];
fadeAnimation2.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn];
fadeAnimation2.fillMode=kCAFillModeForwards;
fadeAnimation2.removedOnCompletion=NO;
[[[imagesViewArrayobjectAtIndex:CurrentImageNumber +1] layer]addAnimation:fadeAnimation2forKey:@""];
CAKeyframeAnimation *positionAnimation2 = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
positionAnimation2.delegate =self;
CGMutablePathRef positionPath2 = CGPathCreateMutable();
CGPathMoveToPoint(positionPath2, NULL, [[imagesViewArrayobjectAtIndex:CurrentImageNumber +1]layer].position.x,
[[imagesViewArrayobjectAtIndex:CurrentImageNumber +1]layer].position.y);
CGPathAddQuadCurveToPoint(positionPath2, NULL, [[imagesViewArrayobjectAtIndex:CurrentImageNumber +1]layer].position.x +self.frame.size.width ,
[[imagesViewArrayobjectAtIndex:CurrentImageNumber +1]layer].position.y,
[[imagesViewArrayobjectAtIndex:CurrentImageNumber +1]layer].position.x,
[[imagesViewArrayobjectAtIndex:CurrentImageNumber +1]layer].position.y);
positionAnimation2.path = positionPath2;
positionAnimation2.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn];
[[[imagesViewArrayobjectAtIndex:CurrentImageNumber +1]layer]addAnimation:positionAnimation2forKey:@"positionAnimation"];
[CATransactioncommit];
CurrentImageNumber++;
}