UITableView 是iOS日常開發(fā)中經(jīng)常使用到的控件。tableView的普通展示效果比較生硬鹊杖,為了提升APP的活力积瞒,提升體驗(yàn),我們可以對(duì)根據(jù)tableView的特點(diǎn)缰贝,操作Cell實(shí)現(xiàn)一些動(dòng)畫效果。
我寫了一個(gè)簡(jiǎn)單的動(dòng)畫集 TableViewAnimationKit,只需要一行代碼就可以讓tableView實(shí)現(xiàn)動(dòng)畫
目前有大概10個(gè)動(dòng)畫绽左,后續(xù)會(huì)優(yōu)化增加蹋凝。
源碼放到Github上: TableViewAnimationKit歡迎大家star房交、下載,交流溝通。
正文
一帜慢、效果展示:
二、使用方法
TableViewAnimationKit調(diào)用各個(gè)動(dòng)畫的方法都為類方法,只需一行代碼就可以調(diào)用。
eg:
[TableViewAnimationKit shakeAnimationWithTableView:tableView];
TableViewAnimationKit提供的動(dòng)畫類方法
+ (void)moveAnimationWithTableView:(UITableView *)tableView;
+ (void)alphaAnimationWithTableView:(UITableView *)tableView;
+ (void)fallAnimationWithTableView:(UITableView *)tableView;
+ (void)shakeAnimationWithTableView:(UITableView *)tableView;
+ (void)overTurnAnimationWithTableView:(UITableView *)tableView;
+ (void)toTopAnimationWithTableView:(UITableView *)tableView;
+ (void)springListAnimationWithTableView:(UITableView *)tableView;
+ (void)shrinkToTopAnimationWithTableView:(UITableView *)tableView;
+ (void)layDonwAnimationWithTableView:(UITableView *)tableView;
+ (void)roteAnimationWithTableView:(UITableView *)tableView;
三怜森、源碼講解
先舉其中一個(gè)動(dòng)畫效果為例子:
動(dòng)畫效果為Cell左右各自插入翅萤。
實(shí)現(xiàn)代碼很簡(jiǎn)單如下:
-
(void)shakeAnimationWithTableView:(UITableView *)tableView {
NSArray *cells = tableView.visibleCells;
for (int i = 0; i < cells.count; i++) {
UITableViewCell cell = [cells objectAtIndex:i];
if (i%2 == 0) {
cell.transform = CGAffineTransformMakeTranslation(-XS_SCREEN_WIDTH,0);
}else {
cell.transform = CGAffineTransformMakeTranslation(XS_SCREEN_WIDTH,0);
}
[UIView animateWithDuration:0.4 delay:i0.03 usingSpringWithDamping:0.75 initialSpringVelocity:1/0.75 options:0 animations:^{
cell.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {}];
}
}
主要思路為:
獲得tableview的visibleCells數(shù)組恐疲,進(jìn)行遍歷,對(duì)每個(gè)執(zhí)行動(dòng)畫套么,不同cell的執(zhí)行時(shí)間培己、方向有所差異,一起構(gòu)成整個(gè)動(dòng)畫胚泌。
四省咨、其他一些動(dòng)畫效果
后語
源碼放到Github上: TableViewAnimationKit有需要的同學(xué)可以下載、star玷室,目前只算Demo級(jí)別零蓉,后面會(huì)繼續(xù)優(yōu)化、增加動(dòng)畫穷缤。如有什么想法敌蜂,歡迎進(jìn)行技術(shù)交流。