iOS技術文檔No.2 AppKit_NSLayoutAnchor

簡介

NSLayoutAnchor API是iOS9版本引入,不僅讓約束聲明更加清晰明了,而且還通過靜態(tài)類型檢查以確保約束能夠正常工作,

其實是一個工廠類,類似NSNumber這樣的設計思想.

NSLayoutAnchor用來創(chuàng)建NSLayoutConstraint對象,使用這些對象從而實現(xiàn)自動布局.

但是一般不會直接創(chuàng)建NSLayoutConstraint對象,而是用UIView(NSView)或者其子類,或者UILayoutGuide的某個anchor屬性(比如centerXAnchor),

這些屬性對應Auto Layout中主要的NSLayoutAttribute值(InterfaceBuilder下屬性欄可以看到),所以也可以用NSLayoutAnchor子類創(chuàng)建這些NSLayoutAttribute值.
不過它的寫法依舊有些復雜,真正在項目中推薦使用SDAutoLayout或者Masonry。

使用

UILayoutGuide *view_Guide = self.view.layoutMarginsGuide;
    /**
     左邊黃色view
     */
    UIView *yellow_View = [[UIView alloc]init];
    yellow_View.backgroundColor = [UIColor yellowColor];
    yellow_View.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:yellow_View];
    //左邊距約束
    NSLayoutConstraint *yellowView_Leading = [yellow_View.leadingAnchor constraintEqualToAnchor:view_Guide.leadingAnchor constant:10];
    //頂部約束
    NSLayoutConstraint *yellowView_Top = [yellow_View.topAnchor constraintEqualToAnchor:view_Guide.topAnchor constant:84];
    //寬度約束
    NSLayoutConstraint *yellowView_Width = [yellow_View.widthAnchor constraintEqualToConstant:100];
    //高度約束
    NSLayoutConstraint *yellow_Height = [yellow_View.heightAnchor constraintEqualToConstant:100];
    [NSLayoutConstraint activateConstraints:@[yellowView_Leading,yellowView_Top,yellow_Height,yellowView_Width]];
    
    yellowViewTopConstraint = yellowView_Top;
    
    
    /**
     居中的紅色view
     */
    UIView *middleView = [[UIView alloc]init];
    middleView.backgroundColor = [UIColor redColor];
    middleView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:middleView];
    //水平居中
    NSLayoutConstraint *middleView_CenterX = [middleView.centerXAnchor constraintEqualToAnchor:view_Guide.centerXAnchor];
    //垂直居中
    NSLayoutConstraint *middleView_CenterY = [middleView.centerYAnchor constraintEqualToAnchor:view_Guide.centerYAnchor];
    //寬度約束
    NSLayoutConstraint *middleView_Width = [middleView.widthAnchor constraintEqualToConstant:100];
    //高度約束
    NSLayoutConstraint *middleView_Height = [middleView.heightAnchor constraintEqualToConstant:100];
    [NSLayoutConstraint activateConstraints:@[middleView_CenterX,middleView_CenterY,middleView_Height,middleView_Width]];
    
/**
     * 創(chuàng)建一個與黃色view相聚10的藍色view
     */
    UIView *blueView = [[UIView alloc]init];
    blueView.backgroundColor = [UIColor blueColor];
    blueView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:blueView];
    //左邊約束
    NSLayoutConstraint *blueView_Leading = [blueView.leadingAnchor constraintEqualToAnchor:yellow_View.trailingAnchor constant:10];
    //頂部約束于黃色view頂部對齊
    NSLayoutConstraint *blueView_Top = [blueView.topAnchor constraintEqualToAnchor:yellow_View.topAnchor];
    //寬度約束
    NSLayoutConstraint *blueView_Width = [blueView.widthAnchor constraintEqualToConstant:100];
    //高度約束
    NSLayoutConstraint *blueView_Height = [blueView.heightAnchor constraintEqualToConstant:100];
    [NSLayoutConstraint activateConstraints:@[blueView_Leading,blueView_Top,blueView_Width,blueView_Height]];
    
    blueViewLeadConstraint = blueView_Leading;

    //創(chuàng)建一個執(zhí)行動畫按鈕
    
    UIButton *btnAnimate = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnAnimate setBackgroundColor:[UIColor redColor]];
    [btnAnimate setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnAnimate setTitle:@"執(zhí)行動畫" forState:UIControlStateNormal];
    [btnAnimate addTarget:self action:@selector(btnAnimateClick:) forControlEvents:UIControlEventTouchUpInside];
    btnAnimate.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:btnAnimate];
    //水平居中
    NSLayoutConstraint *btnAnimate_CenterX = [btnAnimate.centerXAnchor constraintEqualToAnchor:view_Guide.centerXAnchor];
    //底部約束
    NSLayoutConstraint *btnAnimate_Bottom = [btnAnimate.bottomAnchor constraintEqualToAnchor:view_Guide.bottomAnchor constant:-20];
    
    //寬度約束
    NSLayoutConstraint *btnAnimate_Width = [btnAnimate.widthAnchor constraintEqualToConstant:80];
    //高度約束
    NSLayoutConstraint *btnAnimate_Height = [btnAnimate.heightAnchor constraintEqualToConstant:32];
    
    
    [NSLayoutConstraint activateConstraints:@[btnAnimate_Height,btnAnimate_Width,btnAnimate_CenterX,btnAnimate_Bottom]];
- (void)btnAnimateClick:(UIButton *)sender
{
    
    [UIView animateKeyframesWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionRepeat animations:^{
        
        yellowViewTopConstraint.constant += 10;
        blueViewLeadConstraint.constant += 10;
        [self.view layoutIfNeeded];
        
        
    } completion:^(BOOL finished) {
        
        
    }];  
}

運行效果

運行效果.gif
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容