您的位置:首页 > 移动开发 > IOS开发

animation - 6

2015-12-28 18:04 465 查看
收集几个学习中用到的animation例子,都是结合移动,缩放,旋转,透明度等的常用炫酷实现。

转载于:http://www.cnblogs.com/pengyingh/articles/2396032.html

#import "LBCoreAnimationViewController.h"

@interface LBCoreAnimationViewController ()

@property (nonatomic, strong) UIImageView* imageView;

@end

@implementation LBCoreAnimationViewController

- (void)viewDidLoad
{
[super viewDidLoad];

self.imageView = [[UIImageView alloc] init];
[self.imageView setFrame:CGRectMake(30, 30, 80, 80)];
[self.imageView setImage:[UIImage imageNamed:@"loading_monkey"]];
[self.view addSubview:self.imageView];

UIButton* button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(0, 0, 50, 50)];
[button1 setCenter:CGPointMake(self.view.frame.size.width/3, self.view.center.y)];
[button1 setBackgroundColor:[UIColor redColor]];
[button1 setTag:111];
[button1 addTarget:self action:@selector(buttonClicled:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}

- (void)buttonClicled:(id)sender
{
UIButton* button = (UIButton*)sender;

//向右下角缩小移动.
if (button.tag == 111)
{
CGPoint fromPoint = self.imageView.center;

//路径曲线,由贝塞曲线来绘制.
UIBezierPath* movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:fromPoint];

CGPoint toPoint = CGPointMake(300, 460);
[movePath addQuadCurveToPoint:toPoint controlPoint:CGPointMake(toPoint.x, fromPoint.y)]; //保持原始的y.

//关键帧.
//用 + animationWithKeyPath 的方法是根据参数的keyPath来返回的.一对一绑定与关键路径的.
CAKeyframeAnimation* moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnim.path = movePath.CGPath;
moveAnim.removedOnCompletion = YES;

//旋转变化.
CABasicAnimation* scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
//x.y轴缩小到0.1,z轴不变
scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
scaleAnim.removedOnCompletion = YES;

//透明度变化.
CABasicAnimation* opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"];
opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
opacityAnim.toValue = [NSNumber numberWithFloat:0.1];
opacityAnim.removedOnCompletion = YES;

//关键帧,basic旋转,basic透明度组合执行.
CAAnimationGroup* animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim, opacityAnim, nil];
animGroup.duration = 1;
//这一句就说明了层是构建动画的一个重要原因.
[self.imageView.layer addAnimation:animGroup forKey:nil];

}
}

@end

效果是图像从原始态做右抛物线缩小并且随着缩小而变得透明,形成一个抛物状的动画。但是alpha这部分是没有明显的变化的哦,这个将会去找到为什么呢!!

试着改写一下,使得点击一下从小向右上角做抛物线的相反效果吧!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS Animation