CoreAnimation, moving UIImageView with animating shadow in iOS 5 Xcode 4

前端 未结 1 1743
误落风尘
误落风尘 2021-02-10 13:40

I am trying to add a (fake)3d like effect for an image (UIImageView moving from point A to B, during this movement I want at point C=(A+B)/2 for it to have the biggest shadow si

相关标签:
1条回答
  • 2021-02-10 14:39

    You need to animate the shadowOffset of the layer by using CAAnimation. Here is an example on how to enlarge the shadowOffset while moving the object. This example uses a UIButton.

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

    In the M file I am calling the animations on the button from the buttons 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"];
    }
    

    One thing to remember with CoreAnimation is when animating the properties like this they are going to revert to their value from the start unless you set those values after the animation ends in the CAAnimation's Delegate Method.

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

    Here is some additional information on CALayer's animatable properties.

    CALayer and CIFilter animatable properties

    0 讨论(0)
提交回复
热议问题