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

iOS 动画分类浅析

2014-08-12 20:55 323 查看
别人的世界如何旋转,移动我不知道,也不想猜测。先把自己的想做的做了就好。just do it,just do
 I T。O(∩_∩)O~。

很多代码直接摘自下载的例子。手比较懒 。

view负责交互,layer负责展示,一个uiview默认有一个layer,可以添加多个layer。

1. 使用UIView的类方法,去实现动画。

+ (void)animateWithDuration:(NSTimeInterval)duration
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

+ (void)transitionFromView:(UIView *)fromView
toView:(UIView *)toView duration:+(NSTimeInterval)duration
options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion ;//根据设定options
可以指定界面是隐藏还是删除

2.也可以直接改变layer。

我们可以使用很多方法添加动画。这些动画都是继承自CAAnimation。常用的keypath看最下边的介绍。

CABasicAnimation 

    CABasicAnimation *bas=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];

    bas.duration=1;

    bas.delegate=self;

    bas.fromValue=[NSNumber numberWithInteger:0];

    bas.toValue=[NSNumber numberWithInteger:1];

    [layer addAnimation:bas forKey:@"key”];

CAKeyframeAnimation

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

animation.duration = 5.0;

animation.path = path;

animation.repeatCount = NSUIntegerMax;

animation.autoreverses = YES;//动画执行完之后是不是原路返回。

animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[spotlayer addAnimation:animation forKey:@"move"];

CATransition

CATransition *t = [CATransition animation];

t.type = typeController.selectedType;//飞入

t.subtype = subtypeController.selectedSubtype;//从左边

t.duration = 1.5;

t.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];//移动时候的速度由它控制。
self.view1.layer.contents =
(id)[UIImage imageNamed:[images objectAtIndex:imageIndex]].CGImage;

[self.view1.layer addAnimation:t forKey:@"Transition"];

CAAnimationGroup

[CATransaction begin];//设置层的时候,这样保证此刻是在一个线程中实现的。

[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
transitionLayer.opacity = 1.0;
transitionLayer.contents = (id)aCell.imageView.image.CGImage;
transitionLayer.frame = [[UIApplication sharedApplication].keyWindow convertRect:aCell.imageView.bounds fromView:aCell.imageView];

[[UIApplication sharedApplication].keyWindow.layer addSublayer:transitionLayer];

[CATransaction commit];

CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];

positionAnimation.fromValue = [NSValue valueWithCGPoint:transitionLayer.position];

positionAnimation.toValue = [NSValue valueWithCGPoint:CGPointZero];

CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];

boundsAnimation.fromValue = [NSValue valueWithCGRect:transitionLayer.bounds];

boundsAnimation.toValue = [NSValue valueWithCGRect:CGRectZero];

CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];

opacityAnimation.fromValue = [NSNumber numberWithFloat:1.0];

opacityAnimation.toValue = [NSNumber numberWithFloat:0.5];

CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

rotateAnimation.fromValue = [NSNumber numberWithFloat:0 * M_PI];

rotateAnimation.toValue = [NSNumber numberWithFloat:2 * M_PI];

CAAnimationGroup *group = [CAAnimationGroup animation];//多个动画一起执行。决定执行的顺序

group.beginTime = CACurrentMediaTime()
+ 0.25;

group.duration = 0.5;

group.animations = [NSArray arrayWithObjects:positionAnimation,
boundsAnimation, opacityAnimation, rotateAnimation, nil];

group.delegate = self;

group.fillMode = kCAFillModeForwards;

group.removedOnCompletion = NO;

[transitionLayer addAnimation:group forKey:@"move”];

UIDynamicAnimator

自己看例子吧。

3. keypath 介绍:

rotation.x

The rotation, in radians, in the x axis.
rotation.y

The rotation, in radians, in the y axis.
rotation.z

The rotation, in radians, in the z axis.
rotation

The rotation, in radians, in the z axis. This is identical to setting the 
rotation.z
 field.
scale.x

Scale factor for the x axis.
scale.y

Scale factor for the y axis.
scale.z

Scale factor for the z axis.
scale

Average of all three scale factors.
translation.x

Translate in the x axis.
translation.y

Translate in the y axis.
translation.z

Translate in the z axis.
translation

Translate in the x and y axis. Value is an NSSize or CGSize.
CGPoint
 exposes the following fields:

Structure Field 
Description
x

The x component of the point. 
y

The y component of the point. 
CGSize
 exposes the following fields:

Structure Field 
Description
width

The width component of the size.
height

The height component of the size. 
CGRect
 exposes the following fields:

Structure Field 
Description
origin

The origin of the rectangle as a 
CGPoint
.
origin.x

The x component of the rectangle origin.
origin.y

The y component of the rectangle origin.
size

The size of the rectangle as a 
CGSize
.
size.width

The width component of the rectangle size.
size.height

The height component of the rectangle size. 
You can not specify a structure field key path using Objective-C 2.0 properties. This will not work:

myLayer.transform.rotation.x=0;

Instead you must use 
setValue:forKeyPath:
 or 
valueForKeyPath:
 as
shown below:

[myLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"transform.rotation.x"];


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息