最近公司經(jīng)營了一個新的項目,項目的宗旨是做一個交友的app,在項目開發(fā)過程中本人負(fù)責(zé)個人主頁等相關(guān)頁面。
因為是一個娛樂性的交友a(bǔ)pp,免不了界面要鮮艷、有吸引力,尤其是在個人主頁這一塊,要多一些小動畫,可以讓個人主頁免于顯得過于單調(diào)。
由于本項目的個人主頁設(shè)計類似于微博的的個人主頁,所以就擅自作主加了類似其的動畫。即點擊頭像可以放大。
剛開始做動畫的時候代碼的邏輯是這樣的,請看一下代碼:
- (void)showBigImgAnimation{
? ?/*隱藏子控件*/
? ?[self animationHideView];
? ?[UIView animateWithDuration:.25 animations:^{
? ? ? ?_userHeadImgVIew.x = 0;
? ? ? ?_userHeadImgVIew.y = 0;
? ? ? ?_userHeadImgVIew.width = SCREEN_WIDTH;
? ? ? ?_userHeadImgVIew.height = HeadViewDefaultH + tabH;
? ?} completion:^(BOOL finished) {
? ? ? ?isHaveScaled = YES;
? ?}];
}
- (void)hideBigImgAnimation{
? ?/*顯示子控件*/
? ?[self animationShowView];
? ?[UIView animateWithDuration:.25 animations:^{
? ? ? ?_userHeadImgVIew.x = initX;
? ? ? ?_userHeadImgVIew.y = initY;
? ? ? ?_userHeadImgVIew.width = initImgW;
? ? ? ?_userHeadImgVIew.height = initImgH;
? ?} completion:^(BOOL finished) {
? ? ? ?isHaveScaled = NO;;
? ?}];
}
弄好了之后發(fā)現(xiàn)動畫不是預(yù)料中的樣子,各種調(diào)試之后還是沒有達(dá)到理想的效果。最后突然想到我的界面是用xib拖拽的,而且添加了約束,就把相關(guān)的約束條件鏈接到 .m文件里。然后在動畫中修改約束。結(jié)果真的成功了。成功之后的代碼如下所示:
- (void)showBigImgAnimation{
? ?_userHeadImgVIew.layer.cornerRadius =0;
? ?_userHeadImgVIew.layer.borderWidth = 0;
? ?_userHeadImgVIew.layer.borderColor = [[UIColor clearColor] CGColor];
? ?_userHeadImgVIew.layer.masksToBounds = YES;
? ?//隱藏控件
? ?[self animationHideView];
? ?_imgViewWidth.constant = SCREEN_WIDTH;
? ?_imgViewHeight.constant = HeadViewDefaultH + tabH;
? ?_imgViewTopMargin.constant = 0;
? ?[UIView animateWithDuration:.3 animations:^{
? ? ? ?[_userHeadImgVIew.superview layoutIfNeeded];
? ?} completion:^(BOOL finished) {
? ? ? ?isHaveScaled = YES;
? ? ? ?[self showSaveImgView];
? ?}];
}
- (void)hideBigImgAnimation{
? ?[self hideSaveImgView];
? ?_imgViewWidth.constant = initImgW;
? ?_imgViewHeight.constant = initImgH;
? ?_imgViewTopMargin.constant = initImgTopMarginValue;
? ?[UIView animateWithDuration:.3 animations:^{
? ? ? ?[_userHeadImgVIew.superview layoutIfNeeded];
? ?} completion:^(BOOL finished) {
? ? ? ?isHaveScaled = NO;
? ? ? ?_userHeadImgVIew.layer.cornerRadius = _userHeadImgVIew.width/2;
? ? ? ?_userHeadImgVIew.layer.borderWidth = 2.5;
? ? ? ?_userHeadImgVIew.layer.borderColor = [[UIColor colorWithHexString:@"#ffffff" withAlpha:0.5] CGColor];
? ? ? ?_userHeadImgVIew.layer.masksToBounds = YES;
? ? ? ?[self animationShowView];
? ?}];
}
至于為什么用[_useHeadImgView.superView layoutIfNeeded];
可以查閱一下layoutIfNeeded的調(diào)用機(jī)制。有空的話我也會認(rèn)真學(xué)習(xí)一下layoutIfNeeded調(diào)用機(jī)制 并分享給大家