iOS学习笔记-073.CALayer03——position和anchorPosition
来源:互联网 发布:微信怎样在淘宝上付款 编辑:程序博客网 时间:2024/05/16 17:50
- CALayer03position和anchorPosition
- 一基本说明
- 二anchorPosition基本图示
- 图示 图示 图示 anchorPostion00 anchorPostion005 anchorPostion01 anchorPostion050 anchorPostion0505 anchorPostion051 anchorPostion10 anchorPostion105 anchorPostion11
- 三anchorPosition代码示例
- 四anchorPosition图示
- 五主要总结
CALayer03——position和anchorPosition
一、基本说明
CALayer
有2个非常重要的属性:position
和 anchorPoint
@property CGPoint position;用来设置CALayer在父层中的位置以父层的左上角为原点(0, 0)
@property CGPoint anchorPoint;称为“定位点”、“锚点”决定着CALayer身上的哪个点会在position属性所指的位置以自己的左上角为原点(0, 0)它的x、y取值范围都是0~1,默认值为(0.5, 0.5)
二、anchorPosition基本图示
如图:
红色图层显示到什么位置,由position属性决定
假设红色图层的position是(100,100)
下图是anchorPostion的位置
图示 图示 图示 anchorPostion(0,0)
anchorPostion(0,0.5)
anchorPostion(0,1)
anchorPostion(0.5,0)
anchorPostion(0.5,0.5)
anchorPostion(0.5,1)
anchorPostion(1,0)
anchorPostion(1,0.5)
anchorPostion(1,1)
三、anchorPosition代码示例
主要代码
//改变anchorPoint-(void)changeAnchorPoint:(NSInteger)pos{ CGPoint point = _pointArr[pos].CGPointValue; _redView.layer.anchorPoint = point;}
全部代码
//// ViewController.m// 03_UIView66_anchorPostion//// Created by 杞文明 on 17/6/11.// Copyright © 2017年 杞文明. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIView *redView;@property (weak, nonatomic) IBOutlet UIView *myCon;@property (weak, nonatomic) UIButton * lastBtn;@property (strong, nonatomic) NSArray<NSValue *> *pointArr;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; _redView.layer.position = CGPointMake(100, 100); [self initPoint]; [self createButtons];}//初始化点-(void) initPoint{ NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(0, 0) ]; NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(0, 0.5)]; NSValue *value3 = [NSValue valueWithCGPoint: CGPointMake(0, 1)]; NSValue *value4 = [NSValue valueWithCGPoint: CGPointMake(0.5, 0)]; NSValue *value5 = [NSValue valueWithCGPoint: CGPointMake(0.5, 0.5)]; NSValue *value6 = [NSValue valueWithCGPoint: CGPointMake(0.5, 1)]; NSValue *value7 = [NSValue valueWithCGPoint: CGPointMake(1, 0)]; NSValue *value8 = [NSValue valueWithCGPoint: CGPointMake(1, 0.5)]; NSValue *value9 = [NSValue valueWithCGPoint: CGPointMake(1, 1) ]; _pointArr = [[NSArray alloc]initWithObjects:value1,value2,value3,value4,value5,value6,value7,value8,value9, nil];}//创建按钮-(void)createButtons{ //控件之间的空格 int MARGIN_WIDTH = 30; int width = (_myCon.bounds.size.width-MARGIN_WIDTH*4) / 3; int height = 40; int j = 0; for (int i=0; i<9; i++) { j = i/3; NSInteger startX = MARGIN_WIDTH + (MARGIN_WIDTH+width)*(i%3); NSInteger startY = MARGIN_WIDTH + (MARGIN_WIDTH+height)*j; UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(startX, startY, width, width)]; //获取对应的point CGPoint point = _pointArr[i].CGPointValue; NSString * title = [NSString stringWithFormat:@"(%0.1f,%0.1f)",point.x,point.y]; //给控件设置值和一些属性] //文字 [button setTitle:title forState:UIControlStateNormal]; //字体颜色 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button setTitleColor:[UIColor greenColor] forState:UIControlStateSelected]; //给控件点击事件添加代理 [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside]; //设置tag [button setTag:i]; //.把控件添加到myView中 [_myCon addSubview:button]; }}/**控件的点击事件*/-(void)clickButton:(UIButton*)button{ [self changeAnchorPoint:button.tag]; //选中设置 if(_lastBtn!=nil) [_lastBtn setSelected:NO]; [button setSelected:YES]; _lastBtn = button;}//改变anchorPoint-(void)changeAnchorPoint:(NSInteger)pos{ CGPoint point = _pointArr[pos].CGPointValue; _redView.layer.anchorPoint = point;}@end
四、anchorPosition图示
五、主要总结
position和anchorPosition总是重合
UIView的center就是CALayer的position
阅读全文
0 0
- iOS学习笔记-073.CALayer03——position和anchorPosition
- 综述iOS视图的frame、bounds、center、position、anchorPosition等与位置、大小有关的属性
- C#学习笔记--Position
- 【Cocos2d-x 3.0学习笔记】 AnchorPoint 和Position 关系
- Cocos2d-x 3.0学习笔记】 AnchorPoint 和Position 关系
- 【Cocos2d-x 3.0学习笔记】 AnchorPoint 和Position 关系
- 【Cocos2d-x 3.0学习笔记】 AnchorPoint 和Position 关系
- cocos2d-x中节点的position与anchorPosition属性的关系
- css学习笔记之position
- iOS学习笔记—— UIActivityIndicatorView 和 UIProgressView 的使用
- iOS学习笔记—— UIAlertView 和 UIActionSheet 的使用
- IOS学习笔记——UISearchBar和UISearchDisplayController
- 黑马程序员——IOS学习笔记(类和对象)
- 黑马程序员——IOS学习笔记(协议和分类)
- iOS学习笔记12—听筒和扬声器的转换
- iOS学习笔记06—Category和Extension
- iOS学习笔记15—Cocoa:NSOperation和NSOperationQueue
- iOS学习笔记(1)— UIView 渲染和内容管理
- [BZOJ2281][Sdoi2011]黑白棋(博弈+组合数学)
- thinkPHP5 session和cache的区别问题
- 【动态规划13】hdu2167 Pebbles(简单状压dp)
- 系统的硬件组成
- jdk1.8 metaspace介绍
- iOS学习笔记-073.CALayer03——position和anchorPosition
- epoll浅析以及nio中的Selector
- C++/MFC-钩子函数
- GIT 入门使用
- Python操作MySQL-(3)转账实例
- CASENet: Deep Category-Aware Semantic Edge Detection读书笔记
- Spring MVC 向页面传值-Map、Model和ModelMap
- 抽象类注意点
- OpenDDS开发手册---第一章4