從乙個看似簡單的卡片動畫說起

2021-07-26 03:08:48 字數 3531 閱讀 8063

一天,產品經理過來找我,要我實現卡片的動畫,就是很多view疊在一起,可以上拉讓view移走,下拉讓view出現.看起來很簡單的動畫,沒有多做深入的思考,直接開工了,然後......乙個禮拜的恐怖生涯來臨了

//新增每個view,並給每個view新增手勢

-(nsmutablearray *)cardviewarray

}return _cardviewarray;

}//手勢狀態

typedef ns_enum(nsinteger,scrollerviewstate)

;//手勢操作

-(void)movepan:(uipangesturerecognizer *) gesture

else

}else

else

}break;

//具體每個狀態所做的操作就不寫了,因為最後證明是無用功,挺繁瑣的.

case scrollerviewstatefooternoticeshow:

break;

case scrollerviewstateheadernoticeshow:

break;

case scrollerviewstatedragup:

break;

case scrollerviewstatedragdown:

break;

default:

break;}}

@inte***ce popularcardview : uiscrollview想法是美好的,現實是骨幹的.繼承uiscrollview後,手勢失效了.my god,手勢和uiscrollview衝突了.

uicollectionview是uitableview的加強版,因為uicollectionviewlayout,可以自定義布局,功能真是太強大了,它的基礎特性就不多介紹了,還不是很熟悉的小夥伴們可以看uicollectionview由淺入深

*實現思路

@inte***ce cardviewlayout : uicollectionviewlayout繼承uicollectionviewlayout實現自定義布局

/**

* 該方法返回collectionview的contentsize的大小

*/-(cgsize)collectionviewcontentsize

contentsize大小就是每個card的高度之和加上尾檢視的高度 

/**

* 該方法為每個cell繫結乙個layout屬性~

*/- (nsarray *)layoutattributesforelementsinrect:(cgrect)rect

nsindexpath *headerindexpath = [nsindexpath indexpathforitem:0 insection:0];

uicollectionviewlayoutattributes *headerattributes = [self layoutattributesforsupplementaryviewofkind:uicollectionelementkindsectionheader atindexpath:headerindexpath];

[array addobject:headerattributes];

nsindexpath *footerindexpath = [nsindexpath indexpathforitem:_numberofcellsinsection - 1 insection:0];

uicollectionviewlayoutattributes *footerattributes = [self layoutattributesforsupplementaryviewofkind:uicollectionelementkindsectionfooter atindexpath:footerindexpath];

[array addobject:footerattributes];

return array;

}

給每個cell和頭檢視尾檢視新增layout屬性

/**

* 為每個cell設定attribute

*/- (uicollectionviewlayoutattributes *)layoutattributesforitematindexpath:(nsindexpath *)indexpath

//當邊界發生改變時,是否應該重新整理布局。如果yes則在邊界變化(一般是scroll到其他地方)時,將重新計算需要的布局資訊。必須設定為yes

- (bool)shouldinvalidatelayoutforboundschange:(cgrect)newbounds

//修正cell的位置,當cell移動超過一定比例就飛走

- (cgpoint)targetcontentoffsetforproposedcontentoffset:(cgpoint)proposedcontentoffset withscrollingvelocity:(cgpoint)velocity

return proposedcontentoffset;

}/**

* 為每個header和footer設定attribute

*/- (uicollectionviewlayoutattributes *)layoutattributesforsupplementaryviewofkind:(nsstring *)elementkind atindexpath:(nsindexpath *)indexpath else

if (elementkind == uicollectionelementkindsectionfooter)

return attributes;

}

探探的卡片好友推薦:實現層疊的卡片動畫,並且能夠移動.但是移走的不能在移回來.

探探直接用view層疊,新增手勢完成動畫.我寫了個小demo,實現了層疊的動畫,能夠上拉和下移.因為是用collectionview實現的,和探探的不大一樣. 

這個任務實現了乙個多禮拜,走了好多彎路.雖然令人抓狂了點,不過確實學到不少,對每個控制項的屬性有了更深的了解.

靜下心來總結下,發現了交流和思考的重要性. 

mybatis快取,從乙個「靈異」事件說起

talk is cheap.show me the code.該問題涉及的主要 實現包括 listlistgoodstrack param criteria goodstrackquerycriteria criteria 2.xml定義 select 3.service定義 service tra...

從乙個失敗的專案說起 需求和設計

由於是乙個小專案,感覺需求也簡單,再加上時間緊,如果從需求開始一步步來,時間肯定來不及,在這種情況下,專案就匆匆的開始了。為了節省時間,分層 設計等等都不去考慮了,想到哪寫到哪,完全瀑布式開發。直接結果是,完工時間一拖再拖,最後不得不決定下一版本整個推倒重來。庡l恀鉒m?專案失敗的原因有兩個 需求分...

小甲魚 C 3 從乙個程式說起

www.fishc.com include 對乙個整型陣列求和 int addarray int array,int n int main int size sizeof data sizeof int int size sizeof data sizeof data 0 printf 結果是 d ...