代碼片段之Masonry 按鈕放大縮小動(dòng)畫簡(jiǎn)單實(shí)踐

// UIView如果使用AutoLayout 必須寫此方法

+ (BOOL)requiresConstraintBasedLayout
{
    return YES;
}

//按鈕的動(dòng)態(tài)效果修改時(shí) 此方法為 系統(tǒng)自帶方法

// this is Apple's recommended place for adding/updating constraints
- (void)updateConstraints {

    [self.growingButton updateConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(@(self.buttonSize.width)).priorityLow();
        make.height.equalTo(@(self.buttonSize.height)).priorityLow();
        make.width.lessThanOrEqualTo(self); //按鈕的最大寬度 <= 當(dāng)前self的寬度
        make.height.lessThanOrEqualTo(self); //按鈕的最大高度 <= 當(dāng)前self的寬度
    }];
    
    //according to apple super should be called at end of method
    [super updateConstraints]; //蘋果規(guī)定最后調(diào)用這個(gè)
}

//按鈕觸發(fā)的時(shí)候

1.   [self setNeedsUpdateConstraints]; //必須調(diào)用
2.   [self updateConstraintsIfNeeded]; //更新約束
3.   [UIView animateWithDuration:0.4 animations:^{
        [self layoutIfNeeded];
      }];   //動(dòng)畫效果

//代碼演示

- (void)didTapGrowButton:(UIButton *)button {
    if (!self.zoom) {
        self.buttonSize = CGSizeMake(self.buttonSize.width * 5, self.buttonSize.height * 6);
        self.zoom = YES;
    } else {
        self.buttonSize = CGSizeMake(self.buttonSize.width / 5, self.buttonSize.height /  6);
        self.zoom = NO;
    }

    // tell constraints they need updating
    [self setNeedsUpdateConstraints];

    // update constraints now so we can animate the change
    [self updateConstraintsIfNeeded];

    [UIView animateWithDuration:0.4 animations:^{
        [self layoutIfNeeded];
    }];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 備胎 備胎? 在我的愛情字典里,沒有備胎這個(gè)詞。或者其實(shí)我也沒有一本愛情字典。我對(duì)男女間感情的定義是,遇見了感覺心...
    池俊秀閱讀 640評(píng)論 0 49
  • 站在2017這個(gè)時(shí)間點(diǎn)上,回望研究生生涯論文制作的時(shí)候,經(jīng)歷了問卷搜尋數(shù)據(jù),用spss分析處理數(shù)據(jù),做成論文輸出的...
    yutingle116閱讀 816評(píng)論 4 5
  • 在需要使用地理位置的時(shí)候就需要打開使用地理位置的權(quán)限.很多時(shí)候用戶在第一時(shí)間就直接關(guān)閉了使用地理位置的權(quán)限,那么用...
    iOS劉耀宗閱讀 1,498評(píng)論 1 4
  • 控制情緒應(yīng)該是人性里最重要也最難的事情。所謂控制住,并不是喜怒不形于色那么回事。那只能說城府深埋心底。但內(nèi)心深...
    心如蔚藍(lán)閱讀 322評(píng)論 0 0
  • 這一周主要是教師端的招生bug修復(fù)問題。 下一周任務(wù) 繼續(xù)修復(fù)招生bug;推古詩(shī)打卡模塊。
    Kevinxuelei閱讀 208評(píng)論 0 0