ios动画

来源:互联网 发布:百度广告联盟 知乎 编辑:程序博客网 时间:2024/05/16 19:02
基本路径动画


CABasicAnimation *animation  = [CABasicAnimationanimationWithKeyPath:@"position"];
animation.fromValue = [NSValuevalueWithCGPoint:bananaLayer.position];
CGPoint toPoint = bananaLayer.position;
toPoint.x += 180;
animation.toValue = [NSValuevalueWithCGPoint:toPoint];

CABasicAnimation *rotateAnimation  = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.y"];
rotateAnimation.fromValue = [NSNumbernumberWithFloat:0.0];
rotateAnimation.toValue = [NSNumbernumberWithFloat:1.0 * M_PI];


CABasicAnimation *scaoleAnimation  = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];
scaoleAnimation.duration = 0.5;
scaoleAnimation.autoreverses = YES;
scaoleAnimation.fromValue = [NSNumbernumberWithFloat:1.0];
scaoleAnimation.toValue = [NSNumbernumberWithFloat:1.5];


//简单的移动
    CABasicAnimation *theAnimation1;    //定义动画
    theAnimation1=[CABasicAnimationanimationWithKeyPath:@"transform.translation.x"];
    theAnimation1.fromValue=[NSNumbernumberWithFloat:0];
    theAnimation1.toValue=[NSNumbernumberWithFloat:-100];    
    
CAAnimationGroup *group = [CAAnimationGroupanimation];
group.autoreverses = YES;
group.duration = 1.0;
group.animations = [NSArrayarrayWithObjects:animation, scaoleAnimation,theAnimation1, nil];
group.repeatCount = NSNotFound;
    

CABasicAnimation *boundsAnimation = [CABasicAnimationanimationWithKeyPath:@"bounds"];
boundsAnimation.fromValue = [NSValuevalueWithCGRect:transitionLayer.bounds];
boundsAnimation.toValue = [NSValuevalueWithCGRect:CGRectZero];

//类似于透明度的东东
CABasicAnimation *opacityAnimation = [CABasicAnimationanimationWithKeyPath:@"opacity"];
opacityAnimation.fromValue = [NSNumbernumberWithFloat:1.0];
opacityAnimation.toValue = [NSNumbernumberWithFloat:0.5];



[bananaLayeraddAnimation:group forKey:@"move"];


关键帧动画
self.backgroundColor = [UIColorwhiteColor];
path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 20.0, 40.0);
for (NSUInteger i = 0; i < 10; i++) {
CGFloat x = (i % 2) ? 20.0 : self.bounds.size.width - 20.0;
CGFloat y = 40.0 + 30.0 * (i + 1);
//CGPathAddLineToPoint(path, NULL, x, y);
CGPathAddArcToPoint(path, NULL, x, y, x, y + 20.0, 10.0);
}
spot = [[ZBLayoutLayeralloc] init];
spot.bounds = CGRectMake(0.0, 0.0, 30.0, 30.0);
spot.color = [UIColorredColor];
[self.layeraddSublayer:spot];

CAKeyframeAnimation *animation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
animation.duration = 5.0;
animation.path = path;
animation.repeatCount = NSUIntegerMax;
animation.autoreverses = YES;
animation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[spotaddAnimation:animation forKey:@"move"];



动画停止代理
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
if ([theAnimation isEqual:[transitionLayeranimationForKey:@"move"]]) {
[transitionLayerremoveFromSuperlayer];
[transitionLayerremoveAllAnimations];
}
}





在layer设置一些变量时,似乎就直接默认有动画,如果想去掉这个动画的话,可以采用以下代码
[CATransactionbegin];
[CATransactionsetValue:(id)kCFBooleanTrueforKey:kCATransactionDisableActions];

//以下是自己的代码
transitionLayer.opacity = 1.0;
transitionLayer.contents = (id)aCell.imageView.image.CGImage;
transitionLayer.frame = [[UIApplicationsharedApplication].keyWindowconvertRect:aCell.imageView.boundsfromView:aCell.imageView];
[[UIApplicationsharedApplication].keyWindow.layeraddSublayer:transitionLayer];


[CATransactioncommit];



这个应该也可以
[CATransactionsetDisableActions:actionsSwitch.on];



设置相对复杂点的起始值
CABasicAnimation *transformAnimation = [CABasicAnimationanimationWithKeyPath:@"transform"];
transformAnimation.fromValue = [NSValuevalueWithCATransform3D:CATransform3DIdentity];
transformAnimation.toValue = [NSValuevalueWithCATransform3D:CATransform3DMakeRotation(DEGREES_TO_RADIANS(85), 0, 1, 1)];
transformAnimation.duration = 1.5;
transformAnimation.autoreverses = YES;
transformAnimation.repeatCount = HUGE_VALF;
transformAnimation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
原创粉丝点击