IOS 动画的各种实现方法

更多技术性文章请关注 合伙呀

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //创建一个
    UIView *view=[[UIView alloc]init];
    view.backgroundColor=[UIColor redColor];
    [self.view addSubview:view];
    view.tag=1001;
    
    //top 200 dowm 200 left 40 right 40
    [view setTranslatesAutoresizingMaskIntoConstraints:NO];
    NSArray *constraintH= [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-40-[view]-40-|" options: 0 metrics:Nil views:NSDictionaryOfVariableBindings(view)];
    NSArray *constraintV=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-200-[view(>=30)]-200-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)];
    [self.view addConstraints:constraintH];
    [self.view addConstraints:constraintV];
    
    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.view addSubview:button];
    [button setTitle:@"开始动画" forState:UIControlStateNormal];
    [button setTranslatesAutoresizingMaskIntoConstraints:NO];
    NSArray *contraintButtonH=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-40-[button(>=100)]-40-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)];
    NSArray *contraintButtonV=[NSLayoutConstraint constraintsWithVisualFormat:@"V:[button(30)]-20-[view]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button,view)];
    [self.view addConstraints:contraintButtonH];
    [self.view addConstraints:contraintButtonV];
    [button addTarget:self action:@selector(didClickAnimationButton:) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)didClickAnimationButton:(UIButton *)button
{
    //获取要承载动画的视图..
    UIView *redView=[self.view viewWithTag:1001];
    /*
    //使用uiview类方法--1
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        //视图的最终状态
        CGFloat r=arc4random()%1000/1000.f;
        CGFloat g=arc4random()%1000/1000.f;
        CGFloat b=arc4random()%1000/1000.f;
        
        redView.backgroundColor=[UIColor colorWithRed:r green:g blue:b alpha:1.0f];
        
        
        //加旋转
//        redView.transform=CGAffineTransformMakeRotation(M_PI_2*r);
        
    } completion:^(BOOL finished) {
        NSLog(@"finished....");
    }];
     */
    
    /*
    //使用UIView类方法2
    //开始设置动画
    [UIView beginAnimations:nil context:nil];
    //设置过渡效果是否从当前状态开始启动,否则为当前视图的最终状态开始启动
    [UIView setAnimationBeginsFromCurrentState:YES];
    //设置过渡效果的进度,慢入慢出,慢入,慢出,线性4中
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    //设置过渡效果对否延迟
    [UIView setAnimationDelay:0];
    //设置过渡效果的过渡时间
    [UIView setAnimationDuration:0.25f];
    //设置过渡效果是否自动恢复
    [UIView setAnimationRepeatAutoreverses:YES];
    //设置过渡效果是否重复出现
    [UIView setAnimationRepeatCount:1.5];
    
    //设置
    [UIView setAnimationDelegate:self];
    [UIView setAnimationWillStartSelector:@selector(animationStart)];
    [UIView setAnimationDidStopSelector:@selector(animationStop)];
    
    
    CGFloat x=arc4random()%100+100;
    CGFloat y=arc4random()%100+100;
    redView.center=CGPointMake(x, y );
    
   // button.center=CGPointMake(x, y );
   // [button layoutIfNeeded];
    //提交过度动画效果
    [UIView commitAnimations];
     
     */
    
    /*
    //使用quartzCore框架内的对象--3
    //实例化过度对象
    CATransition *animation=[CATransition animation];
    //设置过度对象的时间
    animation.duration=0.5f;
    //设置过度对象的类型
    animation.type=kCATransitionFade;
    //设置过度对象的子类型
    //animation.subtype=
    [redView.layer addAnimation:animation forKey:nil];
    redView.layer.opacity=0.0f;//设置透明度
    */
    
    /*
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"anchorPoint"];
//    /设置过度对象的时间间隔
    animation.duration=0.5f;
    //设置过度对象的开始值
    animation.fromValue=[NSValue valueWithCGPoint:CGPointMake(0.5, 0.5)];
    animation.toValue=[NSValue valueWithCGPoint:CGPointMake(1.0, 1.0)];
    //将过度对象添加到视图层上
    [redView.layer addAnimation:animation forKey:nil];
    //设置视图层的最终状态
    redView.layer.anchorPoint=CGPointMake(1.0f, 1.0f);
     */
    
    
    
    /*
//    keyframe
    CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    //设置过度对象时间间隔
    animation.duration=1;
    //设置过度对象的中间过度状态
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, 110, 200);
    CGPathAddQuadCurveToPoint(path, nil, 150, 250, 200, 200);
    animation.path=path;
    //将过度对象添加到视图
    [redView.layer addAnimation:animation forKey:nil];
     */
    
}

-(void)animationStart
{
    NSLog(@"begin-------------------");
}
-(void)animationStop
{
    NSLog(@"end--++++++++++++++++++++++++++++");
}

原文地址:https://www.cnblogs.com/huntaiji/p/3473726.html