我正在尝试为图像添加类似(假)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
您需要使用 CAAnimation 对图层的 ShadowOffset 进行动画处理。以下是如何在移动对象时放大shadowOffset 的示例。此示例使用 UIButton。
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface ViewController : UIViewController
@property (nonatomic, retain) IBOutlet UIButton *button;
@end
在 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"];
}
使用 CoreAnimation 需要记住的一件事是,当像这样对属性进行动画处理时,它们将从一开始就恢复到它们的值,除非您在动画结束后在 CAAnimation 的委托方法中设置这些值。
- (void) animationDidStop:(NSString *)theAnimation finished:(NSNumber *)finished context:(void *)context
以下是有关 CALayer 可动画属性的一些附加信息。
CALayer 和 CIFilter 可动画属性 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/AnimProps.html#//apple_ref/doc/uid/TP40005942-SW1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)