iOS學習筆記-073.CALayer03——position和anchorPosition – iPhone手機開發 iPhone軟體開發教學課程

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)
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 *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

發佈留言