IOS UIView动画(封装动画)

UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView 将为这些改变提供动画支持
执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视 图,为此需要将改变属性的代码放在[UIView beginAnimations:nil context:nil][UIView commitAnimations]之间
 
常见方法解析:
+ (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代表使用视 图缓存,性能较好
#import "NJViewController.h"

@interface NJViewController ()
@property (weak, nonatomic) IBOutlet UIView *cutomView;

@end

@implementation NJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
    [UIView transitionWithView:self.view duration:1.0 options:0 animations:^{
        NSLog(@"animations");
        // 要执行的动画
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

    } completion:^(BOOL finished) {
        NSLog(@"completion");
        // 执行完毕之后执行的动画
    }];

}

- (void)test2
{
    [UIView animateWithDuration:2.0 animations:^{
        NSLog(@"动画执行之前: %@",NSStringFromCGPoint(self.cutomView.center));
        // 需要执行动画的代码
        self.cutomView.center = CGPointMake(300, 300);
        
    } completion:^(BOOL finished) {
        // 动画执行完毕之后执行的代码
        NSLog(@"动画执行之后: %@",NSStringFromCGPoint(self.cutomView.center));
        
    }];
}

- (void)test1
{
    // 1.创建核心动画
    // 注意点:如果通过核心动画改变layer的位置状态, 表面上看上去已经改变了, 但是实质上是没有改变的
    CABasicAnimation *anima = [CABasicAnimation animation];
    anima.keyPath = @"position";
    anima.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
    
    anima.removedOnCompletion = NO;
    anima.fillMode = kCAFillModeForwards;
    
    anima.delegate = self;
    
    // 2.添加核心动画
    [self.cutomView.layer addAnimation:anima forKey:nil];
}

- (void)animationDidStart:(CAAnimation *)anim
{
    NSLog(@"核心动画执行之前 %@", NSStringFromCGPoint(self.cutomView.layer.position));

}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSLog(@"核心动画执行完毕 %@", NSStringFromCGPoint(self.cutomView.layer.position));
}

- (void)test
{
    // 1.UIVIEW封装的动画, 动画执行完毕之后不会反弹
    NSLog(@"动画执行之前: %@",NSStringFromCGPoint(self.cutomView.center));
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(didStopAnimatino)];
    self.cutomView.center = CGPointMake(300, 300);
    [UIView commitAnimations];
    
}

- (void)didStopAnimatino
{
    NSLog(@"动画执行完毕 %@", NSStringFromCGPoint(self.cutomView.center));
}




@end
View Code
原文地址:https://www.cnblogs.com/liuwj/p/6599594.html