ios 动画函数基础

来源:互联网 发布:java语言的可移植性 编辑:程序博客网 时间:2024/05/10 18:33

一、CGAffineTransformMakeTranslation和CGAffineTransformTranslate

(来源:http://blog.csdn.net/lamp_zy/article/details/8474818)

1.CGAffineTransformMakeTranslation每次都是以最初位置的中心点为起始参照

   CGAffineTransformTranslate每次都是以传入的transform为起始参照

   CGAffineTransformIdentity为最初状态,即最初位置的中心点

2.3个按钮,bt1,bt2,bt3,bt1和bt2控制bt3的移动

- (IBAction)bt1clicked:(id)sender {

    self.bt3.transform = CGAffineTransformMakeTranslation(100);

}

- (IBAction)bt2clicked:(id)sender {

    //self.bt3.transform = CGAffineTransformTranslate(CGAffineTransformIdentity100);

    self.bt3.transform = CGAffineTransformTranslate(self.bt3.transform100);

}

点击bt1,第一次移动10个像素,以后都是以最初位置的中心点为起始参照,所以后续bt1无论点击多少次,按钮都在初始位置偏移10个像素的位置不动

点击bt1一次,再点击bt2一次,偏移20像素,点击bt2时,上一次按钮点击的偏移作为这次的参照

只点击bt2一次,偏移10个像素

不断点击bt2,bt3不断偏移10个像素


bt2clicked的第一句不注释:

第一次点击bt2,bt3偏移20,后续再点击,永远再第一次点击后的位置,再点击bt1,回到初始偏移10的位置(往回走了10)

点击bt1,偏移10,再点击bt2,在bt1点击基础上再偏移10,后续再点击不动( CGAffineTransformTranslate(CGAffineTransformIdentity100);每次都是从最初位置开始偏移

二、 CGAffineTransformMakeRotation 实现旋转

  1. (来源:http://blog.csdn.net/like7xiaoben/article/details/9032827)
  2. UIImageView *image = [[UIImageView alloc]init];  
  3.     image.frame = CGRectMake(50, 50, 200, 200);  
  4.     image.image = [UIImage imageNamed:@"460.jpg"];  
  5.     [self.view addSubview:image];  
  6.     CGAffineTransform transform= CGAffineTransformMakeRotation(M_PI*0.38);  
  7.     /*关于M_PI 
  8.         #define M_PI     3.14159265358979323846264338327950288 
  9.         其实它就是圆周率的值,在这里代表弧度,相当于角度制 0-360 度,M_PI=180度 
  10.         旋转方向为:顺时针旋转 
  11.       
  12.      */  
  13.     image.transform = transform;//旋转 

三、同时使用两个动画CGAffineTransformConcat

四、CGAffineTransform相关函数

CoreGraphics.h

CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_2);
[xxx setTransform:rotation];
呵呵就这么简单的两行代码就可以实现了!
顺便记录一些常量,以后用的着!
#define M_E         2.71828182845904523536028747135266250   e
#define M_LOG2E     1.44269504088896340735992468100189214   log 2e
#define M_LOG10E    0.434294481903251827651128918916605082  log 10e
#define M_LN2       0.693147180559945309417232121458176568  log e2
#define M_LN10      2.30258509299404568401799145468436421   log e10
#define M_PI        3.14159265358979323846264338327950288   pi
#define M_PI_2      1.57079632679489661923132169163975144   pi/2
#define M_PI_4      0.785398163397448309615660845819875721  pi/4
#define M_1_PI      0.318309886183790671537767526745028724  1/pi
#define M_2_PI      0.636619772367581343075535053490057448  2/pi
#define M_2_SQRTPI  1.12837916709551257389615890312154517   2/sqrt(pi)
#define M_SQRT2     1.41421356237309504880168872420969808   sqrt(2)
#define M_SQRT1_2   0.707106781186547524400844362104849039  1/sqrt(2)
 
 
from:http://donbe.blog.163.com/blog/static/138048021201061054243442/

CGAffineTransformMakeTranslation(width, 0.0);是改变位置的,

CGAffineTransformRotate(transform, M_PI);是旋转的。

CGAffineTransformMakeRotation(-M_PI);也是旋转的

transform = CGAffineTransformScale(transform, -1.0, 1.0);是缩放的。


view.transform = CGAffineTransformIdentity;线性代数里面讲的矩阵变换,这个是恒等变换


当 你改变过一个view.transform属性或者view.layer.transform的时候需要恢复默认状态的话,记得先把他们重置可以使用

view.transform = CGAffineTransformIdentity,

或者view.layer.transform = CATransform3DIdentity,

假设你一直不断的改变一个view.transform的属性,而每次改变之前没有重置的话,你会发现后来 的改变和你想要的发生变化了,不是你真正想要的结果


Quartz转换实现的原理:Quartz把绘图分成两个部分,
    用户空间,即和设备无关,
    设备空间,
用户空间和设备空间中间存在一个转换矩阵 : CTM
本章实质是讲解CTM
 
Quartz提供的3大功能
移动,旋转,缩放
 
演示如下,首先加载一张图片
void CGContextDrawImage (
   CGContextRef c,
   CGRect rect,
   CGImageRef image
);
 
 
 
 
 
移动函数
CGContextTranslateCTM (myContext, 100, 50);
 
 
 
旋转函数
include <math.h>
static inline double radians (double degrees) {return degrees * M_PI/180;}
CGContextRotateCTM (myContext, radians(–45.));
 
 
 
缩放
CGContextScaleCTM (myContext, .5, .75);
 
 
 
翻转, 两种转换合成后的效果,先把图片移动到右上角,然后旋转180度
CGContextTranslateCTM (myContext, w,h);
CGContextRotateCTM (myContext, radians(-180.));
 
 
 
组合几个动作
CGContextTranslateCTM (myContext, w/4, 0);
CGContextScaleCTM (myContext, .25,  .5);
CGContextRotateCTM (myContext, radians ( 22.));
 
 
 
 
 
CGContextRotateCTM (myContext, radians ( 22.));
CGContextScaleCTM (myContext, .25,  .5);
CGContextTranslateCTM (myContext, w/4, 0);
 
 
 
 
上面是通过直接修改当前的ctm实现3大效果,下面是通过创建Affine Transforms,然后连接ctm实现同样的3种效果
这样做的好处是可以重用这个Affine Transforms
应用Affine Transforms 到ctm的函数
void CGContextConcatCTM (
   CGContextRef c,
   CGAffineTransform transform
);
 
 
Creating Affine Transforms
移动效果
CGAffineTransform CGAffineTransformMakeTranslation (
   CGFloat tx,
   CGFloat ty
);
 
CGAffineTransform CGAffineTransformTranslate (
   CGAffineTransform t,
   CGFloat tx,
   CGFloat ty
);
 
旋转效果
CGAffineTransform CGAffineTransformMakeRotation (
   CGFloat angle
);
 
CGAffineTransform CGAffineTransformRotate (
   CGAffineTransform t,
   CGFloat angle
);
 
缩放效果
CGAffineTransform CGAffineTransformMakeScale (
   CGFloat sx,
   CGFloat sy
);
 
CGAffineTransform CGAffineTransformScale (
   CGAffineTransform t,
   CGFloat sx,
   CGFloat sy
);
 
反转效果
CGAffineTransform CGAffineTransformInvert (
   CGAffineTransform t
);
 
只对局部产生效果
CGRect CGRectApplyAffineTransform (
   CGRect rect,
   CGAffineTransform t
);
 
判断两个AffineTrans是否相等
bool CGAffineTransformEqualToTransform (
   CGAffineTransform t1,
   CGAffineTransform t2
);
 
 
 
获得Affine Transform
CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform (
   CGContextRef c
);
 
下面的函数只起到查看的效果,比如看一下这个用户空间的点,转换到设备空间去坐标是多少
CGPoint CGContextConvertPointToDeviceSpace (
   CGContextRef c,
   CGPoint point
);
 
CGPoint CGContextConvertPointToUserSpace (
   CGContextRef c,
   CGPoint point
);
 
CGSize CGContextConvertSizeToDeviceSpace (
   CGContextRef c,
   CGSize size
);
 
CGSize CGContextConvertSizeToUserSpace (
   CGContextRef c,
   CGSize size
);
 
CGRect CGContextConvertRectToDeviceSpace (
   CGContextRef c,
   CGRect rect
);
 
CGRect CGContextConvertRectToUserSpace (
   CGContextRef c,
   CGRect rect
);
 
 
CTM真正的数学行为
这个转换矩阵其实是一个 3x3的 举证
如下图
 
 
下面举例说明几个转换运算的数学实现
x y 是原先点的坐标
下面是从用户坐标转换到设备坐标的计算公式
 
 
 
 
下面是一个identity matrix,就是输入什么坐标,出来什么坐标,没有转换
 
最终的计算结果是 x=x,y=y,  
 
 
 可以用函数判断这个矩阵是不是一个 identity matrix
bool CGAffineTransformIsIdentity (
   CGAffineTransform t
);
 
 
 
 
参考:http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_affine/dq_affine.html








- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation   duration:(NSTimeInterval)duration
{
        
    
        if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
        {
                b=YES;
                
                self.view=mainvv;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
                self.view.bounds = CGRectMake(0.0, 0.0, 768.0, 1004.0);
                
        }
        else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        {
                b=NO;
                
                self.view = self.vv;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
                self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0);
                
                
                
        }
        else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        {
                
                b=YES;
                self.view=mainvv;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
                self.view.bounds = CGRectMake(0.0, 0.0, 768.0, 1004.0);
                
        }
        else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
        {
                
                b=NO;
                self.view = self.vv;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
                self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0);
                
        }
        
        
}


3

Quartz转换实现的原理:Quartz把绘图分成两个部分,
    用户空间,即和设备无关,
    设备空间,
用户空间和设备空间中间存在一个转换矩阵 : CTM
本章实质是讲解CTM

Quartz提供的3大功能
移动,旋转,缩放

演示如下,首先加载一张图片
void CGContextDrawImage (
   CGContextRef c,
   CGRect rect,
   CGImageRef image
);


 

 
移动函数
CGContextTranslateCTM (myContext, 100, 50);




旋转函数
include <math.h>
static inline double radians (double degrees) {return degrees * M_PI/180;}
CGContextRotateCTM (myContext, radians(–45.));



缩放
CGContextScaleCTM (myContext, .5, .75);



翻转, 两种转换合成后的效果,先把图片移动到右上角,然后旋转180度
CGContextTranslateCTM (myContext, w,h);
CGContextRotateCTM (myContext, radians(-180.));



组合几个动作
CGContextTranslateCTM (myContext, w/4, 0);
CGContextScaleCTM (myContext, .25,  .5);
CGContextRotateCTM (myContext, radians ( 22.));


 


CGContextRotateCTM (myContext, radians ( 22.));
CGContextScaleCTM (myContext, .25,  .5);
CGContextTranslateCTM (myContext, w/4, 0);




上面是通过直接修改当前的ctm实现3大效果,下面是通过创建Affine Transforms,然后连接ctm实现同样的3种效果
这样做的好处是可以重用这个Affine Transforms
应用Affine Transforms 到ctm的函数
void CGContextConcatCTM (
   CGContextRef c,
   CGAffineTransform transform
);


Creating Affine Transforms
移动效果
CGAffineTransform CGAffineTransformMakeTranslation (
   CGFloat tx,
   CGFloat ty
);

CGAffineTransform CGAffineTransformTranslate (
   CGAffineTransform t,
   CGFloat tx,
   CGFloat ty
);

旋转效果
CGAffineTransform CGAffineTransformMakeRotation (
   CGFloat angle
);

CGAffineTransform CGAffineTransformRotate (
   CGAffineTransform t,
   CGFloat angle
);

缩放效果
CGAffineTransform CGAffineTransformMakeScale (
   CGFloat sx,
   CGFloat sy
);

CGAffineTransform CGAffineTransformScale (
   CGAffineTransform t,
   CGFloat sx,
   CGFloat sy
);

反转效果
CGAffineTransform CGAffineTransformInvert (
   CGAffineTransform t
);

只对局部产生效果
CGRect CGRectApplyAffineTransform (
   CGRect rect,
   CGAffineTransform t
);

判断两个AffineTrans是否相等
bool CGAffineTransformEqualToTransform (
   CGAffineTransform t1,
   CGAffineTransform t2
);



获得Affine Transform
CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform (
   CGContextRef c
);

下面的函数只起到查看的效果,比如看一下这个用户空间的点,转换到设备空间去坐标是多少
CGPoint CGContextConvertPointToDeviceSpace (
   CGContextRef c,
   CGPoint point
);

CGPoint CGContextConvertPointToUserSpace (
   CGContextRef c,
   CGPoint point
);

CGSize CGContextConvertSizeToDeviceSpace (
   CGContextRef c,
   CGSize size
);

CGSize CGContextConvertSizeToUserSpace (
   CGContextRef c,
   CGSize size
);

CGRect CGContextConvertRectToDeviceSpace (
   CGContextRef c,
   CGRect rect
);

CGRect CGContextConvertRectToUserSpace (
   CGContextRef c,
   CGRect rect
);


CTM真正的数学行为
这个转换矩阵其实是一个 3x3的 举证
如下图


下面举例说明几个转换运算的数学实现
x y 是原先点的坐标
下面是从用户坐标转换到设备坐标的计算公式




下面是一个identity matrix,就是输入什么坐标,出来什么坐标,没有转换

最终的计算结果是 x=x,y=y,  


 可以用函数判断这个矩阵是不是一个 identity matrix
bool CGAffineTransformIsIdentity (
   CGAffineTransform t
);


移动矩阵


 

缩放矩阵

 

旋转矩阵

 

旋转加移动矩阵



五、图例http://www.cnblogs.com/85538649/archive/2012/05/16.html

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 两个月宝宝干咳怎么办 幼儿洗澡冷到了怎么办 洗澡着凉感冒了怎么办 小孩抓妈妈头发怎么办 新生儿吃奶呛奶怎么办 2个月猫不吃东西怎么办 新生小牛不吃奶怎么办 一岁宝宝喝夜奶怎么办 小狗不会吸奶怎么办 婴儿喝水总呛怎么办 小羊刚出生吃奶怎么办 新生儿被呛到了怎么办 羊产后无奶怎么办 新生小猫不吃奶怎么办 婴儿吃母乳咳嗽怎么办 小孩呛到了咳嗽怎么办 婴儿喝水呛着怎么办 婴儿吃药呛到了怎么办 新生儿用奶瓶喝奶呛着怎么办 新生儿喝水呛到怎么办 宝宝吃奶呛到了怎么办 羔羊高烧不退怎么办 小孩晚上咳嗽厉害怎么办 婴儿经常呛咳怎么办 新生儿3小时喂奶怎么办 宝宝不会吸奶怎么办 4岁宝宝打呼噜怎么办 宝宝感冒拒奶怎么办 新生儿吸奶无力怎么办 吃奶小牛肚子胀怎么办 新生儿吃奶后打嗝怎么办 新生儿吃多漾奶怎么办 婴儿总是睡不熟怎么办 新生儿不不吃奶怎么办 学生沉迷网络游戏班主怎么办 手机用不了卡怎么办 手机变竖屏怎么办 游戏不支持分屏怎么办 服装设计做到没思路怎么办 眼睛变单眼皮了怎么办 换手机号银行卡绑定怎么办