锚点: anchorPoint     以锚点为中心 执行动画 (与 渔夫固定船的点时一致的)

anchorPoint 默认是 0.5,0.5  (注意: 锚点 是一个比例)

anchorPoint 在左上角的时候 为 0,0

anchorPoint 在右上角的时候 为 1,0

anchorPoint 在左下角的时候 为 0,1

anchorPoint 在右下角的时候 为 1,1

 #import "ViewController.h"

 @interface ViewController ()
{
CALayer *APLayer;
CALayer *ship;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
imageView.image = [UIImage imageNamed:@"网格.jpg"];
[self.view addSubview:imageView]; [self addShipLayer]; } - (void)addShipLayer { ship = [[CALayer alloc] init];
ship.backgroundColor = [UIColor brownColor].CGColor;
ship.bounds = CGRectMake(, , , );
ship.position = self.view.center;
// 透明度 设置
ship.opacity = 0.5;
[self.view.layer addSublayer:ship]; NSLog(@"锚点y:%f\n锚点x:%f", ship.anchorPoint.y, ship.anchorPoint.x); APLayer = [[CALayer alloc] init];
APLayer.bounds = CGRectMake(, , , );
// 通过ship的尺寸 设置 APLayer 的中心点
// position.x = ship的宽*锚点的X position.y = ship的高*锚点的Y
CGFloat x = CGRectGetWidth(ship.bounds)*(ship.anchorPoint.x);
CGFloat y = CGRectGetHeight(ship.bounds)*(ship.anchorPoint.y);
APLayer.position = CGPointMake(x, y);
APLayer.backgroundColor = [UIColor cyanColor].CGColor;
[ship addSublayer:APLayer];
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view]; // 通过点击屏幕的x点/屏幕的宽度 得到点击的点 与屏幕一个比例
CGFloat x = touchPoint.x/CGRectGetWidth([UIScreen mainScreen].bounds);
// 通过点击屏幕的y点/屏幕的高度 得到点击的点 与屏幕一个比例
CGFloat y = touchPoint.y/CGRectGetHeight([UIScreen mainScreen].bounds);
// 改变ship 的锚点
ship.anchorPoint = CGPointMake(x, y); CGFloat cx = CGRectGetWidth(ship.bounds)*ship.anchorPoint.x;
CGFloat cy = CGRectGetHeight(ship.bounds)*ship.anchorPoint.y;
APLayer.position = CGPointMake(cx, cy);
NSLog(@"锚点y:%f\n锚点x:%f", ship.anchorPoint.y, ship.anchorPoint.x);
// 角度值经计算转化为幅度值。要把角度值转化为弧度制,可以使用一个简单的公式Mπ/180
ship.transform = CATransform3DMakeRotation (*M_PI/, , , ); } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
ship.transform = CATransform3DIdentity;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

CoreAnimation 核心动画二 锚点的更多相关文章

  1. iOS开发CoreAnimation解读之一——初识CoreAnimation核心动画编程

    iOS开发CoreAnimation解读之一——初识CoreAnimation核心动画编程 一.引言 二.初识CoreAnimation 三.锚点对几何属性的影响 四.Layer与View之间的关系 ...

  2. CoreAnimation 核心动画 / CABasicAnimation/ CAKeyframeAnimation

    - (void)createBaseAnimation{ //基础动画 CABasicAnimation *animation = [CABasicAnimation animation]; anim ...

  3. iOS CoreAnimation 核心动画

    一 介绍 一组非常强大的动画处理API 直接作用在CALAyer上,并非UIView(UIView动画) CoreAnimation是所有动画的父类,但是不能直接使用,应该使用其子类 属性: dura ...

  4. ios开发图层layer与核心动画二:CATransform3D,CAlayear和UIView区别,layer的position和anchorpoint

    一:CATransform3D #import "ViewController.h" @interface ViewController () @property (weak, n ...

  5. CoreAnimation 核心动画一 (一些常用属性 和 方法)

    1.常用属性: frame   bounds   center   alpha    Transition 过渡    transform 动画效果 2.常用方法: +(void)setAnimati ...

  6. CoreAnimation 核心动画

    - (void)createBaseAnimation{ //基础动画 CABasicAnimation *animation = [CABasicAnimation animation]; anim ...

  7. iOS_核心动画(二)

    目 录: 一.Core Animation开发步骤 二.Core Animation的继承结构 三.CAAnimation常用的属性 四.CAPropertyAnimation(属性动画) 五.CAB ...

  8. iOS核心动画高级技巧之图层变换和专用图层(二)

    iOS核心动画高级技巧之CALayer(一) iOS核心动画高级技巧之图层变换和专用图层(二)iOS核心动画高级技巧之核心动画(三)iOS核心动画高级技巧之性能(四)iOS核心动画高级技巧之动画总结( ...

  9. 核心动画——Core Animation

    一. CALayer (一). CALayer简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比方一个button.一个文本标签.一个文本输入框.一个图标等等.这些都是UIView ...

随机推荐

  1. BZOJ 1024: [SCOI2009]生日快乐 dfs

    1024: [SCOI2009]生日快乐 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...

  2. 不用submit 同样实现button 点击enter键进行提交

    $(function(){ document.onkeydown = function (e) { var theEvent = window.event || e; var code = theEv ...

  3. 思科(Cisco)路由器策略路由配置详解

    策略路由是路由优化的常用方法.在做路由牵引时很多情况都要用到策略路由.我刚刚接触思科这东西,对策略路由的配置还不太熟悉,今天终于配好了,记录一下. 网络拓扑 R2的E1\E2口分别与R3的E1\E2口 ...

  4. PHP如何抓取https内容?记录一下。

    PHP里做一般的获取内容时,用自带的file_get_contents()函数基本就足够了.当然,这个函数只能抓一些简单的数据,如果是遇到需要登录的页面,就不行了,而且效率及稳定性也不是很强.所以要是 ...

  5. ASP.NET 之 检测到在集成的托管管道模式下不适用的ASP.NET设置

    将ASP.NET程序从IIS6移植到IIS7后,调试运行可能提示以下错误: HTTP 错误 500.23 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP. ...

  6. 一个JS版本的MD5

    var hexcase = 0; function hex_md5(a) { if (a == "") return a; return rstr2hex(rstr_md5(str ...

  7. The required Server component failed to start so Tomcat is unable to start解决之一

    http://www.cnblogs.com/quxuedan/archive/2012/12/11/2813445.html 看看这个博客园园主说的吧

  8. Windows 平台下Git 服务器搭建

    由于项目中一直在使用git作为版本管理,自己对git的理解.使用都还不是怎么的熟悉,所以准备深入了解一下git及一些常用命令的使用,于是干脆把服务端架上,通过自己的PC作为服务端同时作为客户端的角色进 ...

  9. 给jdk写注释系列之jdk1.6容器(5)-LinkedHashMap源码解析

    前面分析了HashMap的实现,我们知道其底层数据存储是一个hash表(数组+单向链表).接下来我们看一下另一个LinkedHashMap,它是HashMap的一个子类,他在HashMap的基础上维持 ...

  10. javaweb学习总结十一(JAXP对XML文档进行DOM解析)

    一:将内存中写好的xml文件读取到硬盘上 二:DOM方式对xml文件进行增删改查 1:添加节点(默认是在最后的子节点后面添加) @Test // 向元素中添加节点<version>1.0& ...