欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS学习笔记-073.CALayer03——position和anchorPosition

程序员文章站 2022-06-16 12:27:54
CALayer03——position和anchorPosition 一、基本说明 CALayer 有2个非常重要的属性:position 和 anch...
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)
iOS学习笔记-073.CALayer03——position和anchorPosition anchorPostion(0,0.5)
iOS学习笔记-073.CALayer03——position和anchorPosition anchorPostion(0,1)
iOS学习笔记-073.CALayer03——position和anchorPosition anchorPostion(0.5,0)
iOS学习笔记-073.CALayer03——position和anchorPosition anchorPostion(0.5,0.5)
iOS学习笔记-073.CALayer03——position和anchorPosition anchorPostion(0.5,1)
iOS学习笔记-073.CALayer03——position和anchorPosition anchorPostion(1,0)
iOS学习笔记-073.CALayer03——position和anchorPosition anchorPostion(1,0.5)
iOS学习笔记-073.CALayer03——position和anchorPosition anchorPostion(1,1)
iOS学习笔记-073.CALayer03——position和anchorPosition 三、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 *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图示

iOS学习笔记-073.CALayer03——position和anchorPosition

五、主要总结

position和anchorPosition总是重合

UIView的center就是CALayer的position