知識點總結(jié)11-通過改變約束實現(xiàn)滑出注冊窗口


#import "ZGKLoginViewController.h"

@interface ZGKLoginViewController ()
// 立即登錄按鈕
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
// 登錄模塊左邊間距
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftMargin;

@end

@implementation ZGKLoginViewController

/**** 導航欄顏色 ****/
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (IBAction)closeBtn {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)registerOrLoginBtn:(UIButton *)sender {
    
    // 1.關(guān)閉輸入法
    [self.view endEditing:YES];

    
//    // 2.改變登錄框的約束,記得改變左邊的約束,不要固定死右邊的約束
    if (self.leftMargin.constant) { // 這個里沒有改變控件的frame,只是記錄了要改變的約束,所以不能將這些代碼放入動畫中,要將強制刷新動作放入動畫中
        // 目前顯示的是注冊界面,點擊按鈕后切換為登錄界面
        self.leftMargin.constant = 0;
        sender.selected = NO;
        [sender setTitle:@"注冊賬號" forState:UIControlStateNormal];
    }else{ // 目前顯示的是登錄界面,點擊按鈕后切換為注冊界面
        self.leftMargin.constant = self.view.zgk_width;
        sender.selected = YES;
        [sender setTitle:@"已有賬號" forState:UIControlStateSelected];
    }
    
    // 3.做動畫,強制刷新,或者等runloop也可以
    [UIView animateWithDuration:0.25 animations:^{
        // 這里會刷新self.view內(nèi)部所有子控件的布局
        // 強制刷新:讓最新設(shè)置的約束值馬上作用到UI控件上
        [self.view layoutIfNeeded];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    [self dismissViewControllerAnimated:YES completion:nil];
    // 點擊空白地方,關(guān)閉輸入法
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // 設(shè)置圓角登錄按鈕
    // 數(shù)值前面要加@,包裝成對象,直接屬性用setValue forKey, 間接屬性用setValue forKeyPath
    //    self.loginBtn.layer.cornerRadius = 5;
    [self.loginBtn setValue:@5 forKeyPath:@"layer.cornerRadius"];
    
    //    self.loginBtn.layer.masksToBounds = YES;
    [self.loginBtn setValue:@YES forKeyPath:@"layer.masksToBounds"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

  • 0.將登錄窗口的左間距拖入到登錄窗口(注意xib中,右間距不能設(shè)置死,最好不設(shè)置,不然滑動會出現(xiàn)問題)
// 登錄模塊左邊間距
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftMargin;
  • 1.點擊注冊或者返回已有賬號按鈕
- (IBAction)registerOrLoginBtn:(UIButton *)sender {

    // 1.關(guān)閉輸入法
    [self.view endEditing:YES];


   // 2.改變登錄框的約束,記得改變左邊的約束,不要固定死右邊的約束
    if (self.leftMargin.constant) { // 這個里沒有改變控件的frame,只是記錄了要改變的約束,所以不能將這些代碼放入動畫中,要將強制刷新動作放入動畫中
        // 目前顯示的是注冊界面,點擊按鈕后切換為登錄界面
        self.leftMargin.constant = 0;
        sender.selected = NO;
        [sender setTitle:@"注冊賬號" forState:UIControlStateNormal];
    }else{ // 目前顯示的是登錄界面,點擊按鈕后切換為注冊界面
        self.leftMargin.constant = self.view.zgk_width;
        sender.selected = YES;
        [sender setTitle:@"已有賬號" forState:UIControlStateSelected];
    }
    // 3.做動畫,強制刷新,或者不強制刷新等runloop也可以
    [UIView animateWithDuration:0.25 animations:^{
        // 這里會刷新self.view內(nèi)部所有子控件的布局
        // 強制刷新:讓最新設(shè)置的約束值馬上作用到UI控件上
        [self.view layoutIfNeeded];
    }];
}
  • 2.讓登錄按鈕的方框角變成圓角
- (void)viewDidLoad {
    [super viewDidLoad];

    // 設(shè)置圓角登錄按鈕
    // 數(shù)值前面要加@,包裝成對象,直接屬性用setValue forKey, 間接屬性用setValue forKeyPath
    //    self.loginBtn.layer.cornerRadius = 5; 
    
    // 方法二:kvc
    [self.loginBtn setValue:@5 forKeyPath:@"layer.cornerRadius"];

    //    self.loginBtn.layer.masksToBounds = YES;
    // 方法二:kvc
    [self.loginBtn setValue:@YES forKeyPath:@"layer.masksToBounds"];
}
  • 3.xib中設(shè)置登錄按鈕的圓角半徑和裁剪


    4104104C-877C-431A-927B-156D2915B705.png
  • 4.成為第一響應者,并設(shè)置占位文字顏色
259D3EB5-7365-41B4-8EC2-17A0CAA84245.png
  • 5.報錯: storyboard中設(shè)置了錯誤的類
18631315-AF9C-4545-A28F-B0AB2E36384F.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容