Ios中CATransform3D的一点使用心得。
来源:互联网 发布:数据流量英语 编辑:程序博客网 时间:2024/05/22 14:13
最近项目中有个绕y轴左右摆动UIView的需求。首先想到了使用CATransform3D来做旋转。但是使用过程中碰到了如下两个问题。
问题1:旋转之后,目标View只能看到一半的UI,比如绕y轴旋转只能看到左半边或者右半边。
问题2:摆动的动画,给人感觉总是向一边摆动(一直向左边或者右边)代码如下:
CATransform3D rotationTransform = CATransform3DIdentity;
rotationTransform = CATransform3DRotate(rotationTransform, 0.25 * M_PI_2, 0.0f, 1.0f, 0.0f);
weakSelf.viewHightScoreAvatar.layer.transform = rotationTransform;
[UIView animateWithDuration:2.0 delay:0 options: UIViewAnimationOptionRepeat |UIViewAnimationOptionAutoreverse animations:^{
CATransform3D rotationTransform2 = CATransform3DIdentity;
rotationTransform2 = CATransform3DRotate(rotationTransform2, -0.25* M_PI_2, 0.0f, 1.0f, 0.0f);
weakSelf.viewHightScoreAvatar.layer.transform = rotationTransform2;
} completion:^(BOOL finished) {
}];
经过多次查找资料跟不断尝试终于发现了问题所在,解决方法如下:
对于问题1:可以通过设置CALayer的zPosition来解决,zPosition表示UIView在UI渲染的时候,在屏幕上的层级,zPosition越大在越上面。
对于问题2:可以通过设置CATransform3D的m34来解决,m34表示透视效果,默认值是0(1/D),表示的是无穷远( D无穷大)。当D越小,也就是m34越接近1,透视效果越明显。
修改后的代码如下:
CATransform3D rotationTransform = CATransform3DIdentity;
rotationTransform.m34 = -1.0f/200.0f;//注意这里要在CATransform3DRotate前调用,否则看不效果。
rotationTransform = CATransform3DRotate(rotationTransform, 0.25 * M_PI_2, 0.0f, 1.0f, 0.0f);
weakSelf.viewHightScoreAvatar.layer.transform = rotationTransform;
weakSelf.viewHightScoreAvatar.layer.zPosition = 100;
[UIView animateWithDuration:2.0 delay:0 options: UIViewAnimationOptionRepeat |UIViewAnimationOptionAutoreverse animations:^{
CATransform3D rotationTransform2 = CATransform3DIdentity;
rotationTransform2.m34 = -1.0f/200.0f;
rotationTransform2 = CATransform3DRotate(rotationTransform2, -0.25* M_PI_2, 0.0f, 1.0f, 0.0f);
weakSelf.viewHightScoreAvatar.layer.transform = rotationTransform2;
weakSelf.viewHightScoreAvatar.layer.zPosition = 100;
} completion:^(BOOL finished) {
}];
- Ios中CATransform3D的一点使用心得。
- CATransform3D的一点记录
- Ios中,在UIImage中写文字的一点心得。
- 关于iOS UITextView的一点使用心得,设置placeHolder效果
- 在FLEX的DATAGIRD中使用itemRenderer的一点心得
- Transform 使用的一点心得。
- 使用Authentication的一点心得
- 使用SIP的一点心得
- 使用JFreeChar的一点心得
- 使用表格的一点心得
- 使用RichTextBox的一点心得
- DataGridView使用的一点心得
- LINQ使用的一点心得
- 使用xrc的一点心得
- 使用Authentication的一点心得
- spl_autoload的一点使用心得
- 使用CImageList的一点心得
- 使用dynamic_cast的一点心得
- java中输入输出流的简单应用
- 判断一个自然数是否是某个数的平方
- MFC大文件传输
- 新浪微博OAuth2.0认证理解
- SQLite 锁机制学习总结 锁状态转换及锁机制实现代码分析
- Ios中CATransform3D的一点使用心得。
- Android 自定义进度条
- 源配置
- Microsoft.Practices.EnterpriseLibrary.Data 使用
- 常用上网增强类Chrome扩展
- 只需改动少数代码就能将IOS游戏搬上安卓平台的好工具---Apportable
- 循环双向链表
- 干净卸载SQL Server 2005
- 定时器用法