zoukankan      html  css  js  c++  java
  • 常用动画

     

    //

    //  ViewController.m

    //  AnimationSample

    //

    //  Created by 你好,夏天 on 13-12-13.

    //  Copyright (c) 2013 你好,夏天. All rights reserved.

    //

    #import "ViewController.h"

    #import <QuartzCore/QuartzCore.h>

    @interfaceViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad

    {

        [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

        UIView *redview = [[UIView alloc] init];

        redview.backgroundColor = [UIColorgreenColor];

        redview.tag = 1001;

        [redview setTranslatesAutoresizingMaskIntoConstraints:NO];

        [self.view addSubview:redview];

        

        NSArray *h_constrains = [NSLayoutConstraintconstraintsWithVisualFormat:@"H:|-40-[redview]-40-|"options:0metrics:nilviews:NSDictionaryOfVariableBindings(redview)];

        NSArray *V_constrains = [NSLayoutConstraintconstraintsWithVisualFormat:@"V:|-200-[redview(>=30)]-200-|"options:0metrics:nilviews:NSDictionaryOfVariableBindings(redview)];

        [self.view addConstraints:h_constrains];

        [self.view addConstraints:V_constrains];

        

        //按钮视图是父视图是根视图

        //title@"开始动画"

        //V:[button]-20-[view]

        //H:|-40-[button(>=100)]-40-|

        //didClickAnimationButtton

        UIButton *button = [UIButtonbuttonWithType:(UIButtonTypeRoundedRect)];

        button.backgroundColor = [UIColorredColor];

        [button setTranslatesAutoresizingMaskIntoConstraints:NO];

        [button setTitle:@"开始动画" forState:(UIControlStateNormal)];

        [button addTarget:selfaction:@selector(didClickAnimation:) forControlEvents:(UIControlEventTouchUpInside)];

        

        [self.view addSubview:button];

        

        NSArray *h_constrainsButton = [NSLayoutConstraintconstraintsWithVisualFormat:@"|-40-[button(>=100)]-40-|"options:0metrics:nilviews:NSDictionaryOfVariableBindings(button)];

        NSArray *v_constrainsButton = [NSLayoutConstraintconstraintsWithVisualFormat:@"V:[button(30)]-20-[redview]"options:0metrics:nilviews:NSDictionaryOfVariableBindings(redview,button)];

        

        [self.view addConstraints:h_constrainsButton];

        [self.view addConstraints:v_constrainsButton];

        

    }

    - (void)didReceiveMemoryWarning

    {

        [superdidReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    - (void)didClickAnimation:(id)sender

    {

        //获取要承载动画的视图

        UIView *redView = [self.view viewWithTag:1001];

        

        //使用UIView类方法1

        /*

        [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

            CGFloat r = arc4random()%1000/1000.0f;

            CGFloat g = arc4random()%1000/1000.0f;

            CGFloat b = arc4random()%1000/1000.0f;

            redView.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1];

            

            //过度效果

            redView.transform = CGAffineTransformMakeRotation(M_PI);

            

        } completion:^(BOOL finished) {

            NSLog(@"动画结束");

        }];

         */

        //使用UIView类方法2

        /*

        //开始设置动画

        [UIView beginAnimations:nil context:nil];

        //设置过度效果是否从前状态开始启动,否则为当前视图的最终状态开始启动

        [UIView setAnimationBeginsFromCurrentState:YES];

        //设置过度效果的进度,慢入慢出,慢入,慢出,线性4

        [UIView setAnimationCurve:(UIViewAnimationCurveEaseInOut)];

        //设置过渡效果是否延迟

        [UIView setAnimationDelay:0];

        //设置过度效果的过度时间

        [UIView setAnimationDuration:1.0f];

        //设置过度效果是否自动恢复

        [UIView setAnimationRepeatAutoreverses:YES];

        //设置过度效果是否重复出现

        [UIView setAnimationRepeatCount:1.5f];

        

        [UIView setAnimationDelegate:self];

        [UIView setAnimationWillStartSelector:@selector(animationStart)];

        [UIView setAnimationDidStopSelector:@selector(animationEnd)];

        

        

        CGFloat x = arc4random() % 100+100;

        CGFloat y = arc4random() % 100+200;

        redView.center = CGPointMake(x, y);

        

    //    [sender layoutIfNeeded];

        

        //提交过度动画效果

        [UIView commitAnimations];

         */

        //使用QuartzCoree框架内的对象3

        //实例化过度对象

        CATransition *animation = [CATransitionanimation];

        //设置过度对象的时间

        animation.duration = 0.5f;

        //设置过度对象的类型

        animation.type = kCATransitionFade;

        //设置过度对象的子类型

    //    animation.subtype =

        [redView.layer addAnimation:animation forKey:nil];

        //设置视图层次对象的透明

        redView.layer.opacity = 0.2f;

        

         /*

        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"anchorPoint"];

        //设置过度对象的时间间隔

        animation.duration = 0.5f;

        //设置过度对象的开始值

        animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0.5f,0.5f)];

        animation.toValue = [NSValue valueWithCGPoint:CGPointMake(1.0f, 1.0f)];

        //将过度对象添加到视图层上

        [redView.layer addAnimation:animation forKey:nil];

        //设置视图层的最终状态

        redView.layer.anchorPoint = CGPointMake(1.0f, 1.0f);

        */

        

        

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

    //    //设置过度对象的时间间隔

    //    animation.duration = 1.0f;

    //    //设置过度对象的中间过度状态

    //    CGMutablePathRef path = CGPathCreateMutable();

    //    CGPathMoveToPoint(path, NULL, 110, 200);

    //    CGPathAddQuadCurveToPoint(path, NULL, 150, 250, 200, 200);

    //    animation.path = path;

    //    [redView.layer addAnimation:animation forKey:nil];

        

        

    }

    - (void)animationStart

    {

        NSLog(@"Start");

    }

    - (void)animationEnd

    {

        NSLog(@"end");

    }

  • 相关阅读:
    如何高效的学习技术
    面试连环炮系列(二十三): StringBuffer与StringBuild的区别
    面试连环炮系列(二十二):常用的设计模式有哪些
    算法天天练709:字符串转小写
    面试连环炮系列(二十一):你们的项目怎么使用kafka
    算法天天练771:查找字符串出现的次数
    初次进入职场如何工作与学习
    面试连环炮系列(二十):TCP的滑动窗口协议是什么
    算法天天练334:字符串翻转
    面试连环炮系列(十九):分布式锁的实现方案
  • 原文地址:https://www.cnblogs.com/zhucunliang/p/3473762.html
Copyright © 2011-2022 走看看