iOS开发之锚点anchorPoint
2015-09-16 15:17
381 查看
效果图如下:我们可以看到,不管棕色的方块怎么运动,总是以绿色的店作为中心来移动的,这就和船只的锚点是一样的道理一、简介:anchorPoint 锚点以锚点为中心,执行动画(与渔夫固定船的点一致)anchorPoint 默认是 0.5 0.5锚点是一个比例anchorPoint 锚点在左上角的时候,为(0,0)右上角(1,0)左下角(0,1)右下角(1,1)二、代码1、定义两个全局变量
<span style="color:#009900;">{ CALayer *ship; CALayer *APLayer; }</span>2、代码实现两个layer
ship = [[CALayer alloc]init]; ship.backgroundColor = [UIColor brownColor].CGColor; ship.bounds = CGRectMake(0, 0, 100, 100); ship.position = self.view.center; // CALayer的 中心点 ship.opacity = 0.5; // CALayer的透明度 shadowOpacity NSLog(@"锚点x:%f 锚点y:%f",ship.anchorPoint.x,ship.anchorPoint.y); [self.view.layer addSublayer:ship];
APLayer = [[CALayer alloc]init];APLayer.backgroundColor = [UIColor greenColor].CGColor;APLayer.bounds = CGRectMake(0, 0, 10, 10);CGFloat x = CGRectGetWidth(ship.bounds)*ship.anchorPoint.x;CGFloat y = CGRectGetHeight(ship.bounds)*ship.anchorPoint.y;APLayer.position = CGPointMake(x, y);[ship addSublayer:APLayer];3、触摸屏幕和触屏结束调用
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{UITouch *touch = [touches anyObject];CGPoint touchPoint = [touch locationInView:self.view];// APLayer.position = touchPoint;// 得到锚点CGFloat x1 = touchPoint.x/CGRectGetWidth(self.view.frame);CGFloat y1 = touchPoint.y/CGRectGetHeight(self.view.frame);ship.anchorPoint = CGPointMake(x1, y1);NSLog(@"%f %f",x1,y1);// 得到APlayer的位置CGFloat x = CGRectGetWidth(ship.bounds)*ship.anchorPoint.x;CGFloat y = CGRectGetHeight(ship.bounds)*ship.anchorPoint.y;APLayer.position = CGPointMake(x, y);// 角度值经计算转化为弧度值。要把角度值转化为弧度值,可以使用一个简单的公式Mπ/180// xyz 是三个轴 0 1ship.transform = CATransform3DMakeRotation(45*M_PI/180, 0, 0, 1);}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ship.transform = CATransform3DIdentity;}
相关文章推荐
- iOS 即时视频和聊天(基于环信)
- IOS高德地图使用说明
- iOS 心得一 单元格的动画加载(非autolayout适配模式下)
- iOS 键盘自适应(IQKeyboardManager)使用小结
- iOS 开发值CALayer
- iOS--警告收录及科学快速的消除方法
- IOS 的恢复方法总结
- iOS申请真机调试证书 图文详解
- IOS高德地图的使用
- iOS--基础知识--cookie的使用
- iOS开发之简易时钟的制作
- iOS GCD集汇(一)GCD(Grand Central Dispatch)和Block 使用-浅析
- iOS GCD集汇(二)GCD详解---->很详细噢
- ios技术支持
- iOS开发-语法篇-block详解
- iOS 核心动画-基础动画
- iOS软件开发 设置启动页面
- iOS程序执行
- 【转】外媒解析:iOS 9中值得关注的十大新特性
- iOS核心动画----基础动画