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个非常重要的属性:positionanchorPoint

@property CGPoint position;用来设置CALayer在父层中的位置以父层的左上角为原点(0, 0)
@property CGPoint anchorPoint;称为“定位点”、“锚点”决定着CALayer身上的哪个点会在position属性所指的位置以自己的左上角为原点(0, 0)它的xy取值范围都是0~1,默认值为(0.5, 0.5

二、anchorPosition基本图示

如图:
红色图层显示到什么位置,由position属性决定
假设红色图层的position是(100,100)
下图是anchorPostion的位置

图示 图示 图示 anchorPostion(0,0)
ihih 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
原创粉丝点击