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

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

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

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

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

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

  1. #import "ViewController.h"
  2.  
  3. @interface ViewController ()
  4. {
  5. CALayer *APLayer;
  6. CALayer *ship;
  7. }
  8. @end
  9.  
  10. @implementation ViewController
  11.  
  12. - (void)viewDidLoad {
  13. [super viewDidLoad];
  14. self.view.backgroundColor = [UIColor whiteColor];
  15. UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
  16. imageView.image = [UIImage imageNamed:@"网格.jpg"];
  17. [self.view addSubview:imageView];
  18.  
  19. [self addShipLayer];
  20.  
  21. }
  22.  
  23. - (void)addShipLayer {
  24.  
  25. ship = [[CALayer alloc] init];
  26. ship.backgroundColor = [UIColor brownColor].CGColor;
  27. ship.bounds = CGRectMake(, , , );
  28. ship.position = self.view.center;
  29. // 透明度 设置
  30. ship.opacity = 0.5;
  31. [self.view.layer addSublayer:ship];
  32.  
  33. NSLog(@"锚点y:%f\n锚点x:%f", ship.anchorPoint.y, ship.anchorPoint.x);
  34.  
  35. APLayer = [[CALayer alloc] init];
  36. APLayer.bounds = CGRectMake(, , , );
  37. // 通过ship的尺寸 设置 APLayer 的中心点
  38. // position.x = ship的宽*锚点的X position.y = ship的高*锚点的Y
  39. CGFloat x = CGRectGetWidth(ship.bounds)*(ship.anchorPoint.x);
  40. CGFloat y = CGRectGetHeight(ship.bounds)*(ship.anchorPoint.y);
  41. APLayer.position = CGPointMake(x, y);
  42. APLayer.backgroundColor = [UIColor cyanColor].CGColor;
  43. [ship addSublayer:APLayer];
  44. }
  45.  
  46. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  47. UITouch *touch = [touches anyObject];
  48. CGPoint touchPoint = [touch locationInView:self.view];
  49.  
  50. // 通过点击屏幕的x点/屏幕的宽度 得到点击的点 与屏幕一个比例
  51. CGFloat x = touchPoint.x/CGRectGetWidth([UIScreen mainScreen].bounds);
  52. // 通过点击屏幕的y点/屏幕的高度 得到点击的点 与屏幕一个比例
  53. CGFloat y = touchPoint.y/CGRectGetHeight([UIScreen mainScreen].bounds);
  54. // 改变ship 的锚点
  55. ship.anchorPoint = CGPointMake(x, y);
  56.  
  57. CGFloat cx = CGRectGetWidth(ship.bounds)*ship.anchorPoint.x;
  58. CGFloat cy = CGRectGetHeight(ship.bounds)*ship.anchorPoint.y;
  59. APLayer.position = CGPointMake(cx, cy);
  60. NSLog(@"锚点y:%f\n锚点x:%f", ship.anchorPoint.y, ship.anchorPoint.x);
  61. // 角度值经计算转化为幅度值。要把角度值转化为弧度制,可以使用一个简单的公式Mπ/180
  62. ship.transform = CATransform3DMakeRotation (*M_PI/, , , );
  63.  
  64. }
  65.  
  66. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  67. ship.transform = CATransform3DIdentity;
  68. }
  69.  
  70. - (void)didReceiveMemoryWarning {
  71. [super didReceiveMemoryWarning];
  72. // Dispose of any resources that can be recreated.
  73. }
  74.  
  75. @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. github 坑爹的仓库初始化设置

    一段时间没有使用 github,奇妙地发现自己连仓库都不会建了,汗一个... 话说上次我在 github 上面建了一个仓库,在创建仓库的设置表单中勾上了自动生成 README.md 选项, ok,创建 ...

  2. iptables实战系列:通过NAT转发实现私网对外发布信息

    原文地址: http://os.51cto.com/art/201109/289486.htm [51CTO独家特稿]本文将介绍一个使用iptables实现NAT转发功能的案例. 本文假设读者已经对N ...

  3. 正则表达式从右往左进行匹配(Regex)

    #匹配最末两位为数字 $x=New-Object regex ('\d{2}','RightToLeft') #$x.RightToLeft $x.Match('abcd22') 结果:

  4. iOS开发笔记系列-基础7(C语言特性)

    Objective-C是C语言的扩展,因此,也具备很多C语言的基本特性,这里只罗列部分. 块(Blocks) 块是对C语言的一种扩展,它并未作为标准ANSI C所定义的部分,而是Apple添加到语言中 ...

  5. cdoj 1252 24点游戏 dfs

    24点游戏 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1252 Descr ...

  6. codeforces Gym 100500 J. Bye Bye Russia

    Problem J. Bye Bye RussiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...

  7. hdu1428漫步校园( 最短路+BFS(优先队列)+记忆化搜索(DFS))

    Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于长时间坐在电脑边,缺乏运动.他决定充分利用每次从寝室到机房的时间,在校园里散散步.整个HDU校园呈方形布 ...

  8. Nginx目录保护、防盗链、限速及多域名处理

    http://www.opsers.org/server/nginx-directory-protection-anti-hotlinking-processing-speed-and-multi-d ...

  9. ABAP 日期时间函数(转)

    转自:http://www.sapjx.com/abap-datetime-function.html 函数名称 (内页-点击名称可查看操作) 函数说明 备注 FIMA_DATE_CREATE RP_ ...

  10. Asp.net生成随机不重复的函数(方法)

    // 生成三位毫秒字串         public static string Get_mSec()         {             string mSec = System.DateT ...