【问题标题】:TableViewCell animation in swiftswift中的TableViewCell动画
【发布时间】:2015-01-07 11:00:00
【问题描述】:

我正在关注THIS 教程并使用此代码实现该动画:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.layer.transform = CATransform3DMakeScale(0.1,0.1,1)
    UIView.animateWithDuration(0.25, animations: {
        cell.layer.transform = CATransform3DMakeScale(1,1,1)
    })

}

但我想更新此单元格动画中的某些内容,例如当我滚动到 tableView 时,单元格在开始时像 (0.1,0.1,1) 一样小,之后它像 (1,1,1) 一样缩放,但我想像气泡类型效果一样应用开始时很小,之后它以 (1,1,1) 的原始比例出现,一个是缩放,然后再次进入其原始比例,如 (1,1,1)

请指导我如何实现该动画?

编辑:

我试过了,但它不是那么顺利,也不是我想要的。

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.layer.transform = CATransform3DMakeScale(0.1,0.1,1)
    UIView.animateWithDuration(0.3, animations: {
        cell.layer.transform = CATransform3DMakeScale(1,1,1)
    })
    UIView.animateWithDuration(1, animations: {
        cell.layer.transform = CATransform3DMakeScale(2,2,2)
    })
    UIView.animateWithDuration(0.3, animations: {
        cell.layer.transform = CATransform3DMakeScale(1,1,1)
    })

}

【问题讨论】:

    标签: uitableview animation swift


    【解决方案1】:

    您需要的是缓出动画。欲了解更多信息,请查看http://easings.net

    你可以使用这个库https://github.com/jjackson26/JMJParametricAnimation/tree/master/JMJParametricAnimationDemo制作参数动画

    目前,您尝试做的效果可以使用下面的代码大致完成。你必须一个接一个地做缩放动画。您所做的方式使所有动画一起开始。
    完成块中添加下一个动画代码会在动画之后启动它。您可以进一步调整时间以获得所需的粗略效果。

        cell.layer.transform = CATransform3DMakeScale(0.1,0.1,1)
        UIView.animateWithDuration(0.3, animations: {
            cell.layer.transform = CATransform3DMakeScale(1.05,1.05,1)
            },completion: { finished in
                UIView.animateWithDuration(0.1, animations: {
                    cell.layer.transform = CATransform3DMakeScale(1,1,1)
                })
        })
    

    【讨论】:

    • 将 UIView.animateWithDuration(duration,animation:{ // your code }) 更改为 UIView.animate(withDuration: duration_time, animations: { // your code }) for swift 5
    【解决方案2】:

    斯威夫特 4

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
        UIView.animate(withDuration: 0.4) {
            cell.transform = CGAffineTransform.identity
        }
    }
    

    【讨论】:

    • 太棒了,正是我需要的??
    【解决方案3】:

    Swift 3 版本就像魅力一样!

    cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1)
        UIView.animate(withDuration: 0.3, animations: {
            cell.layer.transform = CATransform3DMakeScale(1.05, 1.05, 1)
        },completion: { finished in
            UIView.animate(withDuration: 0.1, animations: {
                cell.layer.transform = CATransform3DMakeScale(1, 1, 1)
            })
        })
    

    【讨论】:

    • Swift4.1 cell.layer.transform = CATransform3DMakeScale(0.1,0.1,1) UIView.animate(withDuration: 0.3, 动画: { cell.layer.transform = CATransform3DMakeScale (1.05,1.05,1) },completion: { 在 UIView.animate 中完成(withDuration: 0.1, 动画: { cell.layer.transform = CATransform3DMakeScale(1,1,1) }) })
    【解决方案4】:

    使用此代码在表格视图中获取螺旋单元格动画

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        //1. Setup the CATransform3D structure
       var rotation = CATransform3DMakeRotation( CGFloat((90.0 * M_PI)/180), 0.0, 0.7, 0.4);
        rotation.m34 = 1.0 / -600
    
        //2. Define the initial state (Before the animation)
        cell.layer.shadowOffset = CGSize(width: 10.0, height: 10.0)
        cell.alpha = 0;
        cell.layer.transform = rotation;
        cell.layer.anchorPoint = CGPoint(x: 0.0, y: 0.5)
    
        //3. Define the final state (After the animation) and commit the animation
        cell.layer.transform = rotation
        UIView.animate(withDuration: 0.8, animations:{cell.layer.transform = CATransform3DIdentity})
        cell.alpha = 1
        cell.layer.shadowOffset = CGSize(width: 0, height: 0)
        UIView.commitAnimations()
    }
    

    【讨论】:

      【解决方案5】:

      使用此代码在您的 tableview 中实现动画

      func tableView(_ tableView: UITableView, willDisplay cell:
          UITableViewCell, forRowAt indexPath: IndexPath) {
          let rotationAngleInRadians = 360.0 * CGFloat(.pi/360.0)
        //  let rotationTransform = CATransform3DMakeRotation(rotationAngleInRadians, -500, 100, 0)
          let rotationTransform = CATransform3DMakeRotation(rotationAngleInRadians, 0, 0, 1)
          cell.layer.transform = rotationTransform
          UIView.animate(withDuration: 1.0, animations: {cell.layer.transform = CATransform3DIdentity})
      }
      

      【讨论】:

        【解决方案6】:

        使用此代码从左侧动画化 Tableview 单元格

            func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
            let rotationTransform = CATransform3DTranslate(CATransform3DIdentity, -200, 0, 0)
            cell.layer.transform = rotationTransform
            cell.alpha = 0.5
        
            UIView.animate(withDuration: 0.5){
                cell.layer.transform = CATransform3DIdentity
                cell.alpha = 1.0
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-31
          • 1970-01-01
          • 2021-04-15
          • 2017-08-11
          • 1970-01-01
          相关资源
          最近更新 更多