您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發基礎

iOS TableView 入場動畫實作(一):3D 翻轉、位移與淡入

編輯:iOS 開發基礎

授權轉載,作者:判若兩人丶(Github)

前言

見過不少很酷的動畫,一直想積累一些簡單易用的效果以備項目之需,後續也會持續更新同類內容。

GitHub 下載地址:LRTableViewRollAnimation

代碼實現

首先在 cell 中封裝一個方法,把「錨點 + 角度 + 透視」三件事一起處理掉:

+ (instancetype)cellFromXib:(UITableView *)tableView cellAnchorPoint:(CGPoint)cellAnchorPoint angle:(CGFloat)angle {
    LRAnimationCell *cell = [tableView dequeueReusableCellWithIdentifier:LRCellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
    }
    // 動畫設置
    CATransform3D transform = CATransform3DMakeRotation(angle, 0.0, 0.5, 0.3);
    transform.m34 = -1.0 / 500.0;   // 透視效果
    cell.layer.transform = transform;
    cell.layer.anchorPoint = cellAnchorPoint;   // 錨點
    [UIView animateWithDuration:0.6 animations:^{
        cell.layer.transform = CATransform3DIdentity;
    }];
    return cell;
}

關於 m34 = -1.0/500.0,官方註釋是這樣寫的:

// m34:實現透視效果(近大遠小),默認為 0,數值越小越明顯
struct CATransform3D {
    CGFloat m11, m12, m13, m14;
    CGFloat m21, m22, m23, m24;
    CGFloat m31, m32, m33, m34;
    CGFloat m41, m42, m43, m44;
};
typedef struct CATransform3D CATransform3D;

回到視圖控制器,添加幾個狀態屬性:

@property (nonatomic, assign) CGFloat lastScrollOffset;
/** 設置 cell 角度 */
@property (nonatomic, assign) CGFloat angle;
/** 設置 cell 錨點 */
@property (nonatomic, assign) CGPoint cellAnchorPoint;

在 TableView 的代理裡調用剛才封裝的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LRAnimationCell *cell = [LRAnimationCell cellFromXib:tableView cellAnchorPoint:_cellAnchorPoint angle:_angle];
    cell.backgroundImage.image = LRGetImage(indexPath.row + 1);
    return cell;
}

最後實現 scrollViewDidScroll,根據上拉還是下拉決定動畫的錨點與方向,讓整體節奏更協調:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView != self.tableView) return;
    CGFloat y = scrollView.contentOffset.y;
    if (y > _lastScrollOffset) {
        // 用戶往上拖動:x=0 y=0 左側;x=1 y=0 右側
        _angle = - kAngle;
        _cellAnchorPoint = CGPointMake(1, 0);
    } else {
        // 用戶往下拖動:x=0 y=1 左側;x=1 y=1 右側
        _angle = - kAngle;
        _cellAnchorPoint = CGPointMake(0, 1);
    }
    _lastScrollOffset = y;
}

方向完全可以按喜好調整:

// x, y 決定錨點:_cellAnchorPoint = CGPointMake(x, y);
// 上拉時動畫的起始邊:左 x=0 y=0,右 x=1 y=0
// 下拉時動畫的起始邊:左 x=0 y=1,右 x=1 y=1
// 想要中心翻轉:x=0.5 y=0.5

以上對應效果圖 1。效果圖 2 和 3 用的是另一套完全獨立的實現,可以單獨拿去使用:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DRotate(transform, 0, 0, 0, 1);   // 漸變
    transform = CATransform3DTranslate(transform, -200, 0, 0); // 從左側水平移入
    // transform = CATransform3DScale(transform, 0, 0, 0);     // 由小變大
    cell.layer.transform = transform;
    cell.layer.opacity = 0.0;
    [UIView animateWithDuration:0.6 animations:^{
        cell.layer.transform = CATransform3DIdentity;
        cell.layer.opacity = 1;
    }];
}

相關文章:iOS TableView 滾動時的視覺差效果。

整體寫起來並不複雜,喜歡的朋友請點個讚,也歡迎留言補充或指出不足。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved