1.UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变
时,UIView将为这些改变提供动画支持
2.执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视图,为此需要将改变属性的代码放在[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之间:
[UIView beginAnimations:nil context:nil];
// ...(需要执行动画的代码)
[UIView commitAnimations];
3.UIView动画常见方法解析:
+ (void)setAnimationDelegate:(id)delegate
设置动画代理对象,当动画开始或者结束时会发消息给代理对象
+ (void)setAnimationWillStartSelector:(SEL)selector
当动画即将开始时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector
+ (void)setAnimationDidStopSelector:(SEL)selector
当动画结束时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector
+ (void)setAnimationDuration:(NSTimeInterval)duration动画的持续时间,秒为单位
+ (void)setAnimationDelay:(NSTimeInterval)delay动画延迟delay秒后再开始
+ (void)setAnimationStartDate:(NSDate *)startDate
动画的开始时间,默认为now
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve
动画的节奏控制
+ (void)setAnimationRepeatCount:(float)repeatCount
动画的重复次数
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses
如果设置为YES,代表动画每次重复执行的效果会跟上一次相反
+(void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView*)view cache:(BOOL)cache
设置视图view的过渡效果, transition指定过渡类型, cache设置YES代表使用视图缓存,性能较好
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOLfinished))completion
参数解析:
duration:动画的持续时间
delay:动画延迟delay秒后开始
options:动画的节奏控制
animations:将改变视图属性的代码放在这个block中
completion:动画结束后,会自动调⽤用这个block
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOLfinished))completion
参数解析:
duration:动画的持续时间
view:需要进⾏行转场动画的视图
options:转场动画的类型
animations:将改变视图属性的代码放在这个block中
completion:动画结束后,会自动调用这个block
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)durationoptions:(UIViewAnimationOptions)options completion:(void(^)(BOOL finished))completion
方法调用完毕后,相当于执行了下面两句代码:
// 添加toView到父视图
[fromView.superview addSubview:toView];
// 把fromView从父视图中移除[fromView.superview removeFromSuperview];
参数解析:
duration:动画的持续时间
options:转场动画的类型
animations:将改变视图属性的代码放在这个block中
completion:动画结束后,会自动调用这个block
//
// JLViewController.m
// UIView封装的动画
//
// Created by Mac on 15-5-16.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "JLViewController.h"
@interface JLViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)testLayerAnim;
- (IBAction)testUIViewAnim;
- (IBAction)next;
@property (nonatomic, assign)int index;
@end
@implementation JLViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
/**
* 使用UIView做转场动画
*/
- (IBAction)next {
self.index++;
if (self.index == 3) {
self.index = 0;
}
NSString *filename = [NSString stringWithFormat:@"%d.jpg", self.index + 1];
self.imageView.image = [UIImage imageNamed:filename];
[UIView transitionWithView:self.imageView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromTop animations:nil completion:nil];
}
/***********************************测试UIView动画begin********************************************/
///////////////////////////////////////////////////////////////////////////////////////
/// 由该测试可知:
/// 我们修改了imageView的center属性的值,从打印结果可知,UIView动画会真正改变center属性的值。
///////////////////////////////////////////////////////////////////////////////////////
- (IBAction)testUIViewAnim {
/*
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelegate:self];
self.imageView.center = CGPointMake(100, 100);
[UIView commitAnimations];
*/
// 以上代码可以简写为下面这种block的形式
[UIView animateWithDuration:2.0 animations:^{
self.imageView.center = CGPointMake(100, 100);
} completion:^(BOOL finished) {
NSLog(@"%@", NSStringFromCGPoint(self.imageView.center));
}];
}
/***********************************测试UIView动画begin********************************************/
/***********************************测试图层动画begin********************************************/
///////////////////////////////////////////////////////////////////////////////////////
/// 由该测试可知,图层动画都是假象。
/// 我们修改了layer的position属性的值,但从代理方法的打印结果可知
/// 即使加上了
/// anim.removedOnCompletion = NO;
/// anim.fillMode = kCAFillModeForwards;
/// layer的position属性依然没有改变。
///////////////////////////////////////////////////////////////////////////////////////
- (IBAction)testLayerAnim {
CABasicAnimation *anim = [[CABasicAnimation alloc] init];
anim.keyPath = @"position";
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
anim.duration = 2.0;
// 下面两句代码的作用:保持动画执行完毕后的状态(如果不这样设置,动画执行完毕后会回到原状态)
// anim.removedOnCompletion = NO;
// anim.fillMode = kCAFillModeForwards;
// 设置代理
anim.delegate = self;
[self.imageView.layer addAnimation:anim forKey:nil];
}
/***********************************测试图层动画end********************************************/
/**
* 代理方法。动画结束时会调用
*/
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
NSLog(@"%@", NSStringFromCGPoint(self.imageView.layer.position));
}
@end