iOS学习_动画

UIView动画

改变UIView的frame:

- (IBAction)changeFrame:(id)sender {
    //UIView动画有开始beginAnimation, 有结束commitAnimation
    //第一步:开始UIview动画
    [UIView beginAnimations:@"move" context:nil];
    //第二步:设置动画的时长
    [UIView setAnimationDuration:3];
    //第三步:设置代理
    [UIView setAnimationDelegate:self];
    //第四步:设置相关的对象的frame
    self.testView.frame = CGRectMake(100, 100, 200, 100);
    //第五步:结束动画
    [UIView commitAnimations];
}
#pragma mark - UIViewAnimationDelegate的代理
//开始动画方法
- (void)animationWillStart:(NSString *)animationID context:(void *)context{
    NSLog(@"ID = %@,context = %@",animationID,context);
}
//结束动画的方法
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
    NSLog(@"ID = %@,context = %@",animationID,context);
}

改变UIView的颜色

- (IBAction)changeColor:(id)sender {
    [UIView beginAnimations:@"color" context:nil];
    [UIView setAnimationDuration:2];
    [UIView setAnimationDelegate:self];
    self.testView.backgroundColor = [UIColor redColor];
    [UIView commitAnimations];
}

改变UIView的透明度

- (IBAction)changeAlpha:(id)sender {
    [UIView beginAnimations:@"alpha" context:nil];
    [UIView setAnimationDuration:5];
    [UIView setAnimationDelegate:self];
    self.testView.alpha = 0.2;
    [UIView commitAnimations];
}

UIView的翻转效果

- (IBAction)rotaionAction:(id)sender {
    //开始动画
    [UIView beginAnimations:@"rotation" context:nil];
    //设置时长
    [UIView setAnimationDuration:1.5f];
    //设置淡入的效果
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    //设置代理
    [UIView setAnimationDelegate:self];
    //设置翻转方向
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.testView cache:YES];
    //结束
    [UIView commitAnimations];

}

UIView的旋转效果

//旋转的效果
- (IBAction)transfromAction:(id)sender {
    [UIView beginAnimations:@"transfrom" context:nil];
    [UIView setAnimationDuration:3.0f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatCount:-1];
    //进行旋转,设置旋转角度
    CGAffineTransform transfrom = CGAffineTransformMakeRotation(3*M_PI);
    //设置旋转的角度的对象
    [self.testView setTransform:transfrom];
    [UIView commitAnimations];

}

CoreAnimation动画

Layer的一些常用属性

  //设置图片为圆角
   self.imageview.layer.cornerRadius = self.imageview.frame.size.width/2;
//    //注意:还要设置(masksToBounds这个属性影响layer层的阴影效果)
//    self.imageview.layer.masksToBounds = YES;
    //设置layer的阴影颜色
    self.imageview.layer.shadowColor = [UIColor redColor].CGColor;
    //设置透明度
    self.imageview.layer.shadowOpacity = 0.1;
    //设置阴影的偏移量
    self.imageview.layer.shadowOffset = CGSizeMake(20, 10);
    //设置阴影的模糊度
    self.imageview.layer.shadowRadius = 1.0f;

    //需求:拖进来uiview,设置阴影,

    self.uiView.layer.shadowColor = [UIColor orangeColor].CGColor;
    self.uiView.layer.shadowOpacity = 0.5;
    self.uiView.layer.shadowOffset = CGSizeMake(30, 30);
    self.uiView.layer.shadowRadius = 2.0f;

自定义Layer

- (void)customLayout{
    //创建layer对象
    CALayer *layer = [CALayer layer];
    //设置对象的位置
    layer.frame = CGRectMake(0, 280, 100, 100);
    //设置背景颜色
    layer.backgroundColor = [UIColor redColor].CGColor;
    //设置锚点
    layer.anchorPoint = CGPointMake(0, 0);
    //设置锚点的大小(位置)
    layer.position = CGPointMake(50, 50);
    //layer需要添加到layer层
    [self.view.layer addSublayer:layer];

}

CABasicAnimation动画

- (IBAction)basicAnimation:(id)sender {

    //第一步:创建动画的对象
    CABasicAnimation *basicAnimation = [CABasicAnimation animation];
    //第二步:告诉layer层,需要执行什么动画,后边设置的内容为CAlay的先关属性
    basicAnimation.keyPath = @"position";
    //第三步:告诉layer哪来,到哪去
    basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
    basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
    //注意:如果要实现移动到的位置不回到原来的位置需要实现以下两句代码;
    basicAnimation.removedOnCompletion = NO;
    //设置保存动画状态的内容
    basicAnimation.fillMode = kCAFillModeForwards;
    //第四步;设置动画持续时长
    basicAnimation.duration = 3.0f;
    //第五步:将要执行的动画添加到calayer上
    [self.imageview.layer addAnimation:basicAnimation forKey:@"basic"];

}

CAKeyframeAnimation动画

- (IBAction)keyAction:(id)sender {
    //第一步:创建对象
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation];
    //第二步:设置动画轨迹
    keyFrameAnimation.keyPath = @"transform.rotation";
    //第三步:设置翻转角度(弧度的计算公式:度数 / 180 * M_PI)
    keyFrameAnimation.values = @[@(90/180.0*M_PI),@(180/180.0*M_PI),@(360/180.0*M_PI)];
    //第四步;设置动画时长
    keyFrameAnimation.duration = 3.0f;
   //第五步:将要执行的动画添加到layer上
    [self.imageview.layer addAnimation:keyFrameAnimation forKey:@"keyFrameAnimation"];
}

CAAnimationGroupAction组动画

- (IBAction)CAAnimationGroupAction:(id)sender {
    //平移动画
    CABasicAnimation *basicAnimation1 = [CABasicAnimation animation];
    basicAnimation1.keyPath = @"transform.translation.y";
    basicAnimation1.toValue = @(400);

    //翻转动画
    CABasicAnimation *basicAnimation2 = [CABasicAnimation animation];
    basicAnimation2.keyPath = @"transform.scale";
    basicAnimation2.toValue = @(3);//弧度

    //旋转动画
    CABasicAnimation *basicAnimation3 = [CABasicAnimation animation];
    basicAnimation3.keyPath = @"transform.rotation";
    basicAnimation3.toValue = @(M_PI);

    //创建管理动画的动画组
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[basicAnimation1,basicAnimation2,basicAnimation3];
    group.duration = 3.0f;
    [self.imageview.layer addAnimation:group forKey:@"group"];

}

CASpring动画

- (IBAction)CASpringAction:(id)sender {
    //创建对象
    CASpringAnimation *spring = [CASpringAnimation animation];
    spring.keyPath = @"transform.scale";
    spring.fromValue = @1;
    spring.toValue = @0.25;
    spring.duration = 2.0f;
    [self.imageview.layer addAnimation:spring forKey:@"spring"];
}
时间: 2024-08-26 15:01:10

iOS学习_动画的相关文章

IOS学习--核心动画

1.CoreAnimation的介绍 Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就可以实现非常强大的功能. Core Animation可以用在Mac OS X和iOS平台. Core Animation的动画执行过程都是在后台操作的,不会阻塞主线程. 要注意的是,Core Animation是直接作用在CALayer上的,并非UIView 详细地址:http://www.cnb

iOS进阶_动画的多种实现方式

一.UIView动画 //UIView动画有开始beginAnimation,有结束commitAnimation    //第一步:开始UIView动画    [UIView beginAnimations:@"标识ID(可随意命名)" context:nil];    //第二步:设置动画时长    [UIView setAnimationDuration:3];    //第三步:设置UIView动画的回调代理    [UIView setAnimationDelegate:se

iOS学习_界面通讯

属性传值(从前往后传) 第一步:在你想传过去的那个界面的.h文件中声明属性,用来接收上一个界面传过来的内容 1 #import <UIKit/UIKit.h> 2 3 @interface SecondViewController : UIViewController 4 5 // 第一步: 声明属性,用来存放上一页传过来的数据 6 @property (nonatomic, copy) NSString *textString; 7 8 9 @end 第二步:把你想传的内容赋给第一步声明的属

iOS学习_地图_添加大头针

首先要引入#import <MapKit/MapKit.h>框架 创建一个继承与NSObject的类;用于声明属性 接下来在ViewController中实现相关的操作 引入头文件 #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> #import "MyAnnotation.h" 定义相应的属性 //定位管理器 @property(nonatomic,strong)CLLo

iOS学习之动画效果的实现

// //  ViewController.m //  UI-动画练习 // //  Created by jzq_mac on 15/7/22. //  Copyright (c) 2015年 jzq_mac. All rights reserved. // #import "ViewController.h" @interface ViewController () { UIImageView *animation; UIView *view; UIView *view1; } @

iOS学习_地图_定位和编码与反编码

定位: 引入头文件  #import <CoreLocation/CoreLocation.h>声明管理器属性:@property(nonatomic,strong)CLLocationManager *manager;第一步:初始化管理器self.manager = [[CLLocationManager alloc] init];第二步:进行隐私的判断并授权 //进行隐私的判断 if (![CLLocationManager locationServicesEnabled]) { NSLo

转 iOS Core Animation 动画 入门学习(一)基础

iOS Core Animation 动画 入门学习(一)基础 reference:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40004514 在iOS中,每个view中都自动配置了一个layer,我们不能人为新建,而在Mac OS中,view默认是没有

ios客户端发现_动画屋后期页面重构与悬浮评论分享模块开发项目总结

从"看世界杯送流量"项目,遇到响应式布局问题之后,专门钻研了这方面专业的书籍,同时阅读了相关文章.响应式布局简单的说就是使开发的页面在不同设备上都有友好的效果.而最开始"暑期动画屋"的项目,当时并没有采用响应式布局,虽然ipad上可用,其他设备则会有显示问题.这也暴露了,目前所在移动业务事业部前端的问题:    1.考虑到响应式布局在不同设备上,UI设计师只给了一套UI原型图,而在不同设备上的显示只是根据前端工程师的理解或个人偏好来完成布局.从产品经理的角度以及测试

ios客户端发现_动画屋活动获奖展示和获奖模块开发总结

   最近在看<web2.0策略指南>,开篇即对flicker的DVD租赁业务模型进行分析,目前看来这样的商业模型仍然很强大,而自去年纸牌屋后,flicker又引领了一股自有内容的热潮,湖南卫视未来如爸爸去哪儿.快乐大本营等节目.央视世界杯独播.爱奇艺的晓说,刘春加盟后未可知的big thing的节目.热潮后,是人为对影音入口的切割,这个入口渐次增多,对用户和对各方入口都不太算是好事,影音入口又进入了战国时代,不要多久这种模式的风险就会显现,只靠自有内容留住用户还远远不是终点,或许国内的入口可