锚点anchorPoint

//

//  ViewController.m

//  UI-NO-36-2 锚点

//

//  Created by 容伟 on 15/9/15.

//  Copyright (c) 2015年 容伟. All rights reserved.

//

/*

 锚点: 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(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

 

时间: 2025-01-06 07:03:27

锚点anchorPoint的相关文章

什么是锚点(AnchorPoint)

1.锚点通常是图形的几何中心, AnchorPoint(x,y)的两个参量x和y的取值通常都是0到1之间的实数,表示锚点相对于节点长宽的位置. 例如,把节点左下角作为锚点,值为(0,0): 把节点的中心作为锚点,值为(0.5,0.5): 把节点右下角作为锚点,值为(1,0). 精灵的AnchorPoint默认值为(0.5,0.5),其他节点的默认值为(0,0),如CCLayer. 相关的操作:setAnchorpoint(0,0); 影响: 1.挂载位置,   2.缩放,     3.旋转 my

iOS开发——使用OC篇&frame,bounds,center,position,anchorPoint总结

frame,bounds,center,position,anchorPoint总结 图层的 position 属性是一个 CGPoint 的值,它指定图层相当于它父图层的位置, 该值基于父图层的坐标系. 图层的 bounds 属性是一个 CGRect 的值,指定图层的大小(bounds.size)和图层的 原点(bounds.origin).当你重写图层的重画方法的时候,bounds 的原点可以作为图形 上下文的原点. 图层拥有一个隐式的 frame,它是 position,bounds,an

彻底理解position与anchorPoint

引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置? CALayer的position点是哪一点呢? anchorPoint与position有什么关系? 我也迷惑过,找过网上的教程,大部分都是复制粘贴的,有些是翻译的文章但很有问题,看得似懂非懂,还是自己写代码彻底弄懂了,做点笔记吧. 每一个UIView内部都默认关联着一个CALayer, UIView有frame.bounds和center三个属性,CALayer也有类似的属性,

Cocos2dx学习笔记9:cocos2dx锚点(Anchor Point)

锚点(AnchorPoint)是相对坐标,通常用来定义物体内部的点,在cocos2dx中,一般都是以加载精灵来实现游戏元素的表现,而精灵一般都是对应的一张图片资源. 我们在设置精灵位置的时候,要设置精灵中的锚点来和我们的坐标点相对应,就比如人站在地上,我们要设置人的脚为锚点,假如设置人的头为锚点,那么人的身子就都会在底下了. Anchor Point的两个参数都在0~1之间.一般Node的锚点默认为(0.5, 0.5),而Layer的锚点则在左下角(0,0). 在cocos2dx中物体的旋转等动

初识CALayer之position与anchorPoint

转载:http://www.cnblogs.com/benbenzhu/p/3615516.html 引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置?CALayer的position点是哪一点呢?anchorPoint与position有什么关系? 我也迷惑过,找过网上的教程,大部分都是复制粘贴的,有些是翻译的文章但很有问题,看得似懂非懂,还是自己写代码彻底弄懂了,做点笔记吧. 每一个UIView内部都默认关联着一个CALaye

position与anchorPoint

相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置?CALayer的position点是哪一点呢?anchorPoint与position有什么关系? 我也迷惑过,找过网上的教程,大部分都是复制粘贴的,有些是翻译的文章但很有问题,看得似懂非懂,还是自己写代码彻底弄懂了,做点笔记吧. 每一个UIView内部都默认关联着一个CALayer, UIView有frame.bounds和center三个属性,CALayer也有类似的属性,分别为fr

理解position与anchorPoint[转]

引言 相信初接触到CALayer的人都会遇到以下几个问题:  为什么修改anchorPoint会移动layer的位置? CALayer的position点是哪一点呢? anchorPoint与position有什么关系? 每一个UIView内部都默认关联着一个CALayer, UIView有frame.bounds和center三个属性,CALayer也有类似的属性,分别为frame.bounds.position.anchorPoint.frame和bounds比较好理解,bounds可以视为

彻底理解position与anchorPoint - Wonderffee's Blog(转)

引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置?CALayer的position点是哪一点呢?anchorPoint与position有什么关系? 我也迷惑过,找过网上的教程,大部分都是复制粘贴的,有些是翻译的文章但很有问题,看得似懂非懂,还是自己写代码彻底弄懂了,做点笔记吧. 每一个UIView内部都默认关联着一个CALayer, UIView有frame.bounds和center三个属性,CALayer也有类似的属性,分别

iOS中Animation 动画 UI_22

1.iOS中我们能看到的控件都是UIView的子类,比如UIButton UILabel UITextField UIImageView等等 2.UIView能够在屏幕的显示是因为在创建它的时候内部自动添加一个CALayer图层,通过这个图层在屏幕上显示的时候会调用一个drawRect: 的方法,完成绘图,才能在屏幕上显示 3.CALayer 本身就具有显示功能,但是它不能响应用户的交互事件,如果只是单纯的显示一个图形,此时你可以使用CALayer创建或者是使用UIView创建,但是如果这个图形