iOS TableView給力動畫的簡單實現(xiàn)(一)

前言:

之前看見過很多動畫, 都非常炫酷, 所以想弄一些比較簡單的動畫, 以后再項目中可能會用到, 以后還會持續(xù)更新類似的動畫效果!

GitHub下載地址: iOS TableView給力動畫的簡單實現(xiàn)(一)

效果圖:

效果圖1.gif

9E476008-B52D-4475-8074-16600E022097.gif

效果圖2.gif

![48FA7E34-6D34-4968-84C5-E1ED291DB8E8.gif](http://upload-images.jianshu.io/upload_images/2486658-897989e85e5729ca.gif?imageMogr2/auto-orient/strip)

效果圖3.gif

代碼實現(xiàn)

首先我們要在cell中實現(xiàn)一個方法:

  • (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];
    

    }
    //動畫設(shè)置
    CATransform3D transform = CATransform3DMakeRotation(angle, 0.0, 0.5, 0.3);

    transform.m34 = -1.0/500.0; // 設(shè)置透視效果

    cell.layer.transform = transform;
    //錨點
    cell.layer.anchorPoint = cellAnchorPoint;

    [UIView animateWithDuration:0.6 animations:^{
    cell.layer.transform = CATransform3DIdentity;
    }];

    return cell;
    }

我們來簡單解釋下transform.m34 = -1.0/500.0;屬性的設(shè)置
在CATransform3D中

// m34:實現(xiàn)透視效果(意思就是:近大遠小), 它的值默認是0, 這個值越小越明顯
/* Homogeneous three-dimensional transforms. */
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;

我們要在ViewController中添加幾個屬性:

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

在TableView的代理中實現(xiàn)+ (instancetype)cellFromXib:(UITableView *)tableView cellAnchorPoint:(CGPoint)cellAnchorPoint angle:(CGFloat)angle;方法:

  • (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;
    }

最后實現(xiàn)<UIScrollViewDelegate>代理方法, 這個方法主要就是監(jiān)聽TableView往上拖動還是往下拖動,目的就是實現(xiàn)動畫的協(xié)調(diào)性,看著更和諧一些!

//滾動監(jiān)聽

  • (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 右
    _angle = - kAngle;
    _cellAnchorPoint = CGPointMake(1, 0);

    } else {//用戶往下拖動
    // x=0 y=1 -angle 左
    // x=1 y=1 右
    _angle = - kAngle;
    _cellAnchorPoint = CGPointMake(0, 1);
    }
    //存儲最后的y值
    _lastScrollOffset = y;

}

動畫的方向需要根據(jù)自己喜好去設(shè)置:

x,y值為: _cellAnchorPoint = CGPointMake(x, y);
-angle值為: _angle = - kAngle;

//用戶往下拖動時候動畫出現(xiàn)的初始位置:
左邊位置: x=0 y=1 -angle
右邊位置: x=1 y=1

//用戶往上拖動時候動畫出現(xiàn)的初始位置:
左邊位置: x=0 y=0
右邊位置: x=1 y=0 -angle

//在中心點翻轉(zhuǎn)設(shè)置:
x=0.5 y=0.5

以上都是效果圖1的效果, 再來看看效果圖2和3:

下面的代碼實現(xiàn)是獨立的, 跟效果圖1完全是分開實現(xiàn)的, 大家可以拿出去單獨使用!

  • (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;
      }];
    

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Core Animation Core Animation,中文翻譯為核心動畫,它是一組非常強大的動畫處理API,...
    45b645c5912e閱讀 3,056評論 0 21
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,566評論 6 30
  • Core Animation其實是一個令人誤解的命名。你可能認為它只是用來做動畫的,但實際上它是從一個叫做Laye...
    小貓仔閱讀 3,802評論 1 4
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺iOS動畫全貌。在這里你可以看...
    F麥子閱讀 5,141評論 5 13
  • 先看看CAAnimation動畫的繼承結(jié)構(gòu) CAAnimation{ CAPropertyAnimation { ...
    時間不會倒著走閱讀 1,689評論 0 1