IOS开发 CGAffineTransform相关函数

2024-04-08 04:48

本文主要是介绍IOS开发 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
);


移动矩阵


 

缩放矩阵

 

旋转矩阵

 

旋转加移动矩阵




//********************************//

layer 动画-(transform.rotation篇)

x轴旋转:
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
theAnimation.duration=8;
theAnimation.removedOnCompletion = YES;
theAnimation.fromValue = [NSNumber numberWithFloat:0];
theAnimation.toValue = [NSNumber numberWithFloat:3.1415926];[yourView.layer addAnimation:theAnimation forKey:@"animateTransform"];y轴旋转:
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
theAnimation.duration=8;
theAnimation.removedOnCompletion = YES;
theAnimation.fromValue = [NSNumber numberWithFloat:0];
theAnimation.toValue = [NSNumber numberWithFloat:3.1415926];[yourView.layer addAnimation:theAnimation forKey:@"animateTransform"];z轴旋转:
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
theAnimation.duration=8;
theAnimation.removedOnCompletion = YES;
theAnimation.fromValue = [NSNumber numberWithFloat:0];
theAnimation.toValue = [NSNumber numberWithFloat:3.1415926];[yourView.layer addAnimation:theAnimation forKey:@"animateTransform"];以上缩放是以view的中心点为中心缩放的,如果需要自定义缩放点,可以设置卯点:
//中心点
[yourView.layer setAnchorPoint:CGPointMake(0.5, 0.5)];//左上角
[yourView.layer setAnchorPoint:CGPointMake(0, 0)];//右下角
[yourView.layer setAnchorPoint:CGPointMake(1, 1)];可设参数:theAnimation.repeatCount = 0;
theAnimation.autoreverses = NO;旋转的另一种实现:(以下代码绕z轴旋转180度)[self.topViewController.view.layer setAnchorPoint:CGPointMake(0.5, 0.5)];[self.topViewController.view.layer setTransform:CATransform3DMakeRotation(0, 0, 0, 1)];[UIView animateWithDuration:8 delay:0.0f options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveEaseIn animations:^{[self.topViewController.view.layer setTransform:CATransform3DMakeRotation(3.1415926, 0, 0, 1)];} completion:^(BOOL finished) {}];


//获取当前transform
CGAffineTransform transform = self.lblSeg.transform;//缩放
self.lblSeg.transform = CGAffineTransformMakeScale(.5, .5);//在一个transform的基础上再缩放
self.lblTest.transform = CGAffineTransformScale(transform, 0.5, 0.5);//旋转(弧度制)
self.lblSeg.transform = CGAffineTransformMakeRotation(M_2_PI);//在一个transform的基础上再旋转
self.lblTest.transform = CGAffineTransformRotate(transform,M_2_PI);//平移
self.lblSeg.transform = CGAffineTransformMakeTranslation(100, 100);//在一个transform的基础上再平移
self.lblTest.transform = CGAffineTransformTranslate(transform, 100, 100);



这篇关于IOS开发 CGAffineTransform相关函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/884607

相关文章

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

Redis的Zset类型及相关命令详细讲解

《Redis的Zset类型及相关命令详细讲解》:本文主要介绍Redis的Zset类型及相关命令的相关资料,有序集合Zset是一种Redis数据结构,它类似于集合Set,但每个元素都有一个关联的分数... 目录Zset简介ZADDZCARDZCOUNTZRANGEZREVRANGEZRANGEBYSCOREZ

Linux使用fdisk进行磁盘的相关操作

《Linux使用fdisk进行磁盘的相关操作》fdisk命令是Linux中用于管理磁盘分区的强大文本实用程序,这篇文章主要为大家详细介绍了如何使用fdisk进行磁盘的相关操作,需要的可以了解下... 目录简介基本语法示例用法列出所有分区查看指定磁盘的区分管理指定的磁盘进入交互式模式创建一个新的分区删除一个存

基于Python开发电脑定时关机工具

《基于Python开发电脑定时关机工具》这篇文章主要为大家详细介绍了如何基于Python开发一个电脑定时关机工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 简介2. 运行效果3. 相关源码1. 简介这个程序就像一个“忠实的管家”,帮你按时关掉电脑,而且全程不需要你多做

关于Maven生命周期相关命令演示

《关于Maven生命周期相关命令演示》Maven的生命周期分为Clean、Default和Site三个主要阶段,每个阶段包含多个关键步骤,如清理、编译、测试、打包等,通过执行相应的Maven命令,可以... 目录1. Maven 生命周期概述1.1 Clean Lifecycle1.2 Default Li

numpy求解线性代数相关问题

《numpy求解线性代数相关问题》本文主要介绍了numpy求解线性代数相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 在numpy中有numpy.array类型和numpy.mat类型,前者是数组类型,后者是矩阵类型。数组

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j

Oracle的to_date()函数详解

《Oracle的to_date()函数详解》Oracle的to_date()函数用于日期格式转换,需要注意Oracle中不区分大小写的MM和mm格式代码,应使用mi代替分钟,此外,Oracle还支持毫... 目录oracle的to_date()函数一.在使用Oracle的to_date函数来做日期转换二.日

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要