CoreAnimation,在iOS 5 Xcode 4中使用动画阴影移动UIImageView [英] CoreAnimation, moving UIImageView with animating shadow in iOS 5 Xcode 4

查看:88
本文介绍了CoreAnimation,在iOS 5 Xcode 4中使用动画阴影移动UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 size (or larger shadow offset), so it looks like it is going up and down again. when I try to even change the shadow size, it is not animating. could you help me how to edit this code:

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设置动画。这是一个关于如何在移动对象时放大shadowOffset的示例。此示例使用UIButton。

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

在M文件中我从按钮调用按钮上的动画IBAction。

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"];
}

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

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

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

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

CALayer和CIFilter可动画属性

这篇关于CoreAnimation,在iOS 5 Xcode 4中使用动画阴影移动UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆