Swift4滚动UITableview后,动画UITableview框架是错误的

Posted

技术标签:

【中文标题】Swift4滚动UITableview后,动画UITableview框架是错误的【英文标题】:Swift4 after scrolling UITableview, animating UITableview frame is wrong 【发布时间】:2020-08-31 08:10:54 【问题描述】:

testView 是子视图 有testView和view NSLayout

self.view.addSubview(testView)
testView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: testView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: testView, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0.0).isActive = true
self.testWidth = NSLayoutConstraint(item: testView, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1.0, constant: 0.0)
self.testWidth.isActive = true
self.testHeight = NSLayoutConstraint(item: testView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 1.0, constant: 0.0)
self.testHeight.isActive=true

tblview 是 UITableview tblview 是 testView 的子视图 有 tblview 和 testView NSLayout

self.testView.addSubview(self.tblView)
self.tblView.translatesAutoresizingMaskIntoConstraints=false
self.tblView.register(NotYetWriteCell.self, forCellReuseIdentifier: "NotYetWriteCellPage")
NSLayoutConstraint(item: tblView, attribute: .centerY, relatedBy: .equal, toItem: self.testView, attribute: .centerY, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: tblView, attribute: .centerX, relatedBy: .equal, toItem: self.testView, attribute: .centerX, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: tblView, attribute: .width, relatedBy: .equal, toItem: self.testView, attribute: .width, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: tblView, attribute: .height, relatedBy: .equal, toItem: self.testView, attribute: .height, multiplier: 1.0, constant: 0.0).isActive = true

UITableview 委托,数据源

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return 50


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    print("cell")
    let cell = tableView.dequeueReusableCell(withIdentifier: "NotYetWriteCellPage", for: indexPath) as! NotYetWriteCell
    
    cell.contentView.layoutIfNeeded()
                   
    
    return cell

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
  
    print(self.tblView.frame.height)
    return self.testView.frame.height/7
    

动画动作代码

self.testWidth.isActive = false
self.testHeight.isActive = false

self.testHeight = NSLayoutConstraint(item: testView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 0.5, constant: 0.0)
self.testHeight.isActive = true
self.testWidth = NSLayoutConstraint(item: testView, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 0.5, constant: 0.0)
self.testWidth.isActive = true
UIView.transition(with: self.view, duration: 2.0, options: .curveEaseIn, animations: 
    self.testView.layoutIfNeeded()
, completion:  (finish) in
    if finish
        self.took=true
    
        
)

【问题讨论】:

嗨,欢迎来到 Stack Overflow!请提供更多关于什么不起作用的细节。是否有任何错误,或者它没有按您预期的方式工作?请参阅tour 和How to Ask。 【参考方案1】:

我的自定义 Cell this.

class NotYetWriteCell: UITableViewCell 
let mainImage=UIView()
let mainLabel=UILabel()
var animation:NSLayoutConstraint!

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) 
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    mainLabel.text="tableCell"
    //mainLabel.textAlignment = .center
    mainImage.addSubview(mainLabel)
    mainLabel.translatesAutoresizingMaskIntoConstraints = false
    //self.mainLabel.numberOfLines = 0
    
    NSLayoutConstraint(item: mainLabel, attribute: .width, relatedBy: .equal, toItem: mainImage, attribute: .width, multiplier: 1.0, constant: 0.0).isActive=true
    
    NSLayoutConstraint(item: mainLabel, attribute: .centerX, relatedBy: .equal, toItem: mainImage, attribute: .centerX, multiplier: 1.0, constant: 0.0).isActive=true
    animation = NSLayoutConstraint(item: mainLabel, attribute: .centerY, relatedBy: .equal, toItem: mainImage, attribute: .centerY, multiplier: 1.0, constant: 0.0)
    animation.isActive = true
    
    mainImage.backgroundColor = .groupTableViewBackground
    self.contentView.addSubview(mainImage)
    mainImage.translatesAutoresizingMaskIntoConstraints=false
    mainImage.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0.0).isActive=true
    mainImage.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0.0).isActive=true
    mainImage.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -10.0).isActive=true
    mainImage.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 10.0).isActive=true
    self.selectionStyle = .none
    contentView.backgroundColor = .groupTableViewBackground



required init?(coder aDecoder: NSCoder) 
    fatalError("init(coder:) has not been implemented")

【讨论】:

以上是关于Swift4滚动UITableview后,动画UITableview框架是错误的的主要内容,如果未能解决你的问题,请参考以下文章

当 UITableView 开始滚动时,动画视图

单元格中的 UITableView 动画导致平滑滚动中断

UITableView 滚动时动画单元格

UITableView, setContentOffset 动画:NO 不会在 iPad 上滚动

UITableView 动画在部分之间滚动,同时临时删除过多的单元格以简化动画

UITableView 视差标题滚动动画(TuneIn)