首先我們知道navigationBar的結(jié)構(gòu)圖NavigationBar 結(jié)構(gòu)圖渴丸,所以我們要做到這種效果,最終是要改變圖片的alpha庆冕。基于此有如下3種方法劈榨。
1.設(shè)置漸變圖片
根據(jù)上面設(shè)置為透明的方法,我們最直接能想到的還是setBackgroundImage,根據(jù)滑動距離去設(shè)置圖片的alpha访递。
可以根據(jù)圖片alpha和color封裝一個圖像類別如下:
+ (UIImage*)imageWithColor:(UIColor*)color{CGRectrect =CGRectMake(0.0f,0.0f,1.0f,1.0f);UIGraphicsBeginImageContext(rect.size);CGContextRefcontext =UIGraphicsGetCurrentContext();CGContextSetFillColorWithColor(context, [colorCGColor]);CGContextFillRect(context, rect);UIImage*image =UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();returnimage;}
2.根據(jù)運(yùn)行時的特性,動態(tài)綁定可以參考動態(tài)綁定demo可參考GitHub - ltebean/LTNavigationBar: UINavigationBar Category which allows you to change its appearance dynamically
3.直接獲取NavigationBar子視圖ImageView,然后設(shè)置他的透明度.然后用一個全局的imageView引用他。從結(jié)構(gòu)圖中可以看出來,他是NavigationBar的子視圖,我們可以通過for...in循環(huán)遍歷navigationBar.subviews,然后獲得這個view.或者直接直接獲取UIImage = self.navigationController.navigationBar.subviews.firstObject;
第一次寫博文同辣,若有不妥之處拷姿,望賜教!