CoreAnimation,在iOS 5 Xcode 4中使用动画阴影移动UIImageView

Nab*_*abi 4 core-animation objective-c quartz-2d ios xcode4

我正在尝试为图像添加一个(假的)3d效果(UIImageView从A点移动到B,在此运动期间,我希望在C =(A + B)/ 2点,因为它具有最大的阴影大小(或更大的阴影偏移),所以看起来它再次上下移动.当我尝试甚至更改阴影大小时,它不是动画.你能帮我如何编辑这段代码:

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
Run Code Online (Sandbox Code Playgroud)

Mob*_*ord 10

您需要使用CAAnimation为图层的shadowOffset设置动画.这是一个关于如何在移动对象时放大shadowOffset的示例.此示例使用UIButton.

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

在M文件中,我通过按钮IBAction调用按钮上的动画.

-(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"];
}
Run Code Online (Sandbox Code Playgroud)

使用CoreAnimation时要记住的一件事是,在动画这样的属性时,除非在动画结束后在CAAnimation的委托方法中设置这些值,否则它们将从一开始就恢复为它们的值.

- (void) animationDidStop:(NSString *)theAnimation finished:(NSNumber *)finished context:(void *)context
Run Code Online (Sandbox Code Playgroud)

以下是有关CALayer动画属性的一些其他信息.

CALayer和CIFilter可动画的属性