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

相关文章

基于Python开发PDF转Doc格式小程序

《基于Python开发PDF转Doc格式小程序》这篇文章主要为大家详细介绍了如何基于Python开发PDF转Doc格式小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用python实现PDF转Doc格式小程序以下是一个使用Python实现PDF转DOC格式的GUI程序,采用T

使用Python开发一个图像标注与OCR识别工具

《使用Python开发一个图像标注与OCR识别工具》:本文主要介绍一个使用Python开发的工具,允许用户在图像上进行矩形标注,使用OCR对标注区域进行文本识别,并将结果保存为Excel文件,感兴... 目录项目简介1. 图像加载与显示2. 矩形标注3. OCR识别4. 标注的保存与加载5. 裁剪与重置图像

Java8需要知道的4个函数式接口简单教程

《Java8需要知道的4个函数式接口简单教程》:本文主要介绍Java8中引入的函数式接口,包括Consumer、Supplier、Predicate和Function,以及它们的用法和特点,文中... 目录什么是函数是接口?Consumer接口定义核心特点注意事项常见用法1.基本用法2.结合andThen链

MySQL 日期时间格式化函数 DATE_FORMAT() 的使用示例详解

《MySQL日期时间格式化函数DATE_FORMAT()的使用示例详解》`DATE_FORMAT()`是MySQL中用于格式化日期时间的函数,本文详细介绍了其语法、格式化字符串的含义以及常见日期... 目录一、DATE_FORMAT()语法二、格式化字符串详解三、常见日期时间格式组合四、业务场景五、总结一、

golang panic 函数用法示例详解

《golangpanic函数用法示例详解》在Go语言中,panic用于触发不可恢复的错误,终止函数执行并逐层向上触发defer,最终若未被recover捕获,程序会崩溃,recover用于在def... 目录1. panic 的作用2. 基本用法3. recover 的使用规则4. 错误处理建议5. 常见错

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE