CoreAnimation,在iOS 5 Xcode 4中移动带有animation阴影的UIImageView

我试图为图像添加一个(假的)3D效果(UIImageView从A点移动到B,在这个移动过程中,我想在点C =(A + B)/ 2,因为它具有最大的影子尺寸更大的阴影偏移),所以它看起来像是再次上升和下降,当我试图甚至改变阴影大小,它不是animation,你能帮助我如何编辑此代码:

NSValue *pointB = [NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(imageView.frame)+50, CGRectGetMinY(imageView.frame)+50)]; [self.view bringSubviewToFront:ImageView]; [UIView beginAnimations:@"UIImage Move" context:NULL]; CGPoint point = [pointB CGPointValue]; CGSize size =imageView.frame.size; [UIView setAnimationDuration:1.0]; imageView.frame = CGRectMake(point.x, point.y, size.width, size.height); imageView.layer.shadowOffset = CGSizeMake(0, 4); //actually I want this to happen in mid point and revert to offset 1 [UIView commitAnimations]; //sorry for possible problems with syntax, the code works fine, I had to rewrite and simplify it for understanding 

您需要使用CAAnimation为图层的shadowOffset设置animation。 下面是如何在移动对象时放大shadowOffset的示例。 这个例子使用一个UIButton。

 #import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> @interface ViewController : UIViewController @property (nonatomic, retain) IBOutlet UIButton *button; @end 

在M文件中,我从buttonIBAction调用button上的animation。

 -(IBAction)shadowGrow:(id)sender { CABasicAnimation *shadowGrow = [CABasicAnimation animationWithKeyPath:@"shadowRadius" ]; shadowGrow.delegate = self; [shadowGrow setFromValue:[NSNumber numberWithFloat:3.0]]; [shadowGrow setToValue:[NSNumber numberWithFloat:20.0]]; [shadowGrow setDuration:1.0f]; shadowGrow.autoreverses = YES; CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"transform.translation.x" ]; move.delegate = self; [move setFromValue:[NSNumber numberWithFloat:0]]; [move setToValue:[NSNumber numberWithFloat:50]]; [move setDuration:1.0f]; move.autoreverses = YES; //Add animation to a specific element's layer. Must be called after the element is displayed. [[button layer] addAnimation:shadowGrow forKey:@"shadowRadius"]; [[button layer] addAnimation:move forKey:@"transform.translation.x"]; } 

在CoreAnimation中要记住的一件事是,当animation像这样的属性时,它们将从一开始就恢复到它们的值,除非在animation在CAAnimation的委托方法中结束之后设置这些值。

 - (void) animationDidStop:(NSString *)theAnimation finished:(NSNumber *)finished context:(void *)context 

这里是关于CALayer的animation属性的一些附加信息。

CALayer和CIFilter的animation性能