Ios 以Swift格式逐行淡入UITableViewCell

Ios 以Swift格式逐行淡入UITableViewCell,ios,swift,uitableview,animation,Ios,Swift,Uitableview,Animation,我是swift的新手,我正在尝试一个UITableView,这些单元格将被设置动画,以便一个接一个地出现。我该怎么做?另外,如果新出现的单元格行不在屏幕上(隐藏在表格下方)。当每个单元格出现时,如何向上移动表格 var tableData1: [String] = ["1", "2", "3", "4", "5", "6", "7"] override func viewWillAppear(animated: Bool) { tableView.scr

我是swift的新手,我正在尝试一个UITableView,这些单元格将被设置动画,以便一个接一个地出现。我该怎么做?另外,如果新出现的单元格行不在屏幕上(隐藏在表格下方)。当每个单元格出现时,如何向上移动表格

    var tableData1: [String] = ["1", "2", "3", "4", "5", "6", "7"]

     override func viewWillAppear(animated: Bool) {
           tableView.scrollEnabled=false
    tableView.alpha=0.0
            NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(3), target: self, selector: "animateTable", userInfo: nil, repeats: false)
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tableData1.count
    }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell     {

        let cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell
        cell.lblCarName.textAlignment = NSTextAlignment.Justified;
return cell
}

func animateTable() {

//what should be the code?//
}

在UITableView中,当行到达您的“视野范围”时,它们会自动为您准备好。我假设您能够滚动tableView,至少一开始不能,因此我们可以通过编程方式滚动它,使行在滚动时显示。我们是怎么做到的?实际上,UITableView有一个方法,可以将其滚动到特定行:

scrollToRowAtIndexPath(indexPath : NSIndexPath, atScrollPosition : UITableViewScrollPosition, animated : Bool);
我太累了,无法编写代码,所以我将尝试解释一下。首先,对于您将alpha设置为0的每个单元格,只要加载它们(cellforrowatinexpath)。 那么让我们假设我们的屏幕适合前5行。您将按顺序(使用UIView.animateWithDuration)设置它们的字母的动画,直到第五个字母(索引4),然后使用5、6、,。。。直到剩下的部分(使用scrollPosition=.Bottom)。对于它们中的每一个,你会在它们加载后立即设置动画。记住在这两次互动之间留出一些时间。(首先设置动画,然后运行NSTimer到第二个,然后继续)。当然,布尔值应该是真的。

Step-1 在初始化单元格的
cellforrowatinexpath
方法中,像这样隐藏它

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell     {
    let cell: TblCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TblCell
    cell.continueLabel.textAlignment = .justified
    cell.contentView.alpha = 0
    return cell
}
步骤2 让我们制作淡入淡出动画
UITableViewDelegate
具有
willDisplayCell
方法,该方法能够检测到当您滚动到顶部或底部时,第一个单元格将显示在窗口上

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    UIView.animate(withDuration: 0.8, animations: {
        cell.contentView.alpha = 1
    })   
}

您的淡入淡出动画正在进行中。最重要的是,您不能在运行时直接设置单元格的alpha,因为iOS正在对单元格进行一些特殊的内部处理,作为
UITableView
的一部分,并忽略您的设置。因此,如果您设置了单元格的
contentView
,一切都会很好。

以下是一些代码,可以帮助您开始。在我的 我将
UITableView
子类化,并覆盖
layoutSubviews
方法。在其中,可以根据单元在视图中的相对位置操纵单元。在这个例子中,我在底部淡出它们

import UIKit

public class MyCustomTableView: UITableView {

    // MARK: - Layout

    public override func layoutSubviews() {
        super.layoutSubviews()

        let indexpaths = indexPathsForVisibleRows!
        let totalVisibleCells = indexpaths.count - 1
        if totalVisibleCells <= 0 { return }

        for index in 0...totalVisibleCells {
            let indexPath = indexpaths[index] 
            if let cell = cellForRowAtIndexPath(indexPath) {
                if let superView = superview {
                    let point = convertPoint(cell.frame.origin, toView:superView)
                    let pointScale = point.y / CGFloat(superView.bounds.size.height)
                    cell.contentView.alpha = 1 - pointScale
                }
            }
        }
    }
}
导入UIKit
公共类MyCustomTableView:UITableView{
//标记:-布局
公共覆盖函数布局子视图(){
super.layoutSubviews()
让indexpaths=indexPathsForVisibleRows!
让totalVisibleCells=indexpaths.count-1

如果totalVisibleCells逐个显示每个可见单元格,则可以通过播放alpha和duration值来实现

extension UITableView {

    func fadeVisibleCells() {

        var delayDuration: TimeInterval = 0.0

        for cell in visibleCells {

            cell.alpha = 0.0

            UIView.animate(withDuration: delayDuration) {
                cell.alpha = 1.0
            }

            delayCounter += 0.30
        }
    }
}

我认为这个方法将是一个很好的解决办法

  • 将所有单元格alpha设置为0
  • 在tableView中设置动画(:,willDisplay:,forRowAt:)
  • 为每个
    indexPath.row设置延迟
func tableView(tableView:UITableView,willDisplay单元格:UITableView单元格,forRowAt indexath:indexPath){
cell.alpha=0
UIView.animate(持续时间:0.5,延迟:0.05*Double(indexPath.row),动画:{
cell.alpha=1
})
}

谢谢!我已经将alpha设置为0。我不确定的是如何使单元格一个接一个地显示?它们同时显示?如何控制或分配每个单元格?方法“CellForRowatineXpath”可以调用您实现的,以获取特定行的单元格。若要使它们逐个显示,请使用NSTime.scheduledTimerWithTimeInterval调用它们。例如,第一行传递1,第二行传递2,第三行传递3。我尝试了这一方法,代码可以正常工作,但我希望有一个稍微不同的行为。您的代码会淡出所有r同时使用ows。我只希望tableView的每一行(最下面一行)都淡出。我如何重新使用您的代码来实现这一点?另外,我可以在2秒钟后删除最下面一行吗?谢谢。请添加一些关于您的逻辑的说明来解释您的代码!这将非常棒!
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell .alpha = 1.0
         let transform = CATransform3DTranslate(CATransform3DIdentity, 0, 3000, 1200)
       //let transform = CATransform3DTranslate(CATransform3DIdentity, 250, 0, 1250)
     //let transform = CATransform3DTranslate(CATransform3DIdentity, 250, 1250, 0)
      // let transform = CATransform3DTranslate(CATransform3DIdentity, -250, 300, 120)


   cell.layer.transform = transform




    UIView.animate(withDuration: 1.0) {
        cell.alpha = 1.0
        cell.layer.transform = CATransform3DIdentity
        cell.layer.transform = CATransform3DIdentity
    }
}