CoreAnimation 核心动画二 锚点
2015-10-06 16:11
423 查看
锚点: anchorPoint 以锚点为中心 执行动画 (与 渔夫固定船的点时一致的)
anchorPoint 默认是 0.5,0.5 (注意: 锚点 是一个比例)
anchorPoint 在左上角的时候 为 0,0
anchorPoint 在右上角的时候 为 1,0
anchorPoint 在左下角的时候 为 0,1
anchorPoint 在右下角的时候 为 1,1
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(0, 0, 100, 100); 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(0, 0, 5, 5); // 通过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 (90*M_PI/180, 0, 0, 1); } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { ship.transform = CATransform3DIdentity; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- C语言文件输出,把内容输入到新建文件中
- 增删改查数据库用的组件
- (转)数据库范式那些事(清晰)
- UEFI工程编译命令Build用法
- [LeetCode-172] Factorial Trailing Zeroes(n 阶乘后面有几个0)
- 20151006模拟赛总结
- codeforces 372B B. Counting Rectangles is Fun(暴力)
- 原码、反码、补码的由来和计算
- 导航效果
- leetcode 290: Word Pattern
- java 中static final关键字
- 如何在控件没有绘制完成之前读取控件的长宽
- Learn more study less 读后感
- 随机数
- 多表联查
- ACM学习历程—POJ3565 Ants(最佳匹配KM算法)
- hdu5117 Fluorescent DP,期望
- 【Scala学习笔记】2. 函数定义、流程控制、异常处理
- eclipse中倒入项目时,报关于.classpath的错误
- JavaBean入门 - 概念和简单例子