Swift4滚动UITableview后,设置UITableview帧的动画是错误的
testView是子视图 有testView和view布局Swift4滚动UITableview后,设置UITableview帧的动画是错误的,swift,uitableview,animation,nslayoutconstraint,Swift,Uitableview,Animation,Nslayoutconstraint,testView是子视图 有testView和view布局 self.view.addSubview(testView) testView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint(item: testView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0
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
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
tblview是UITableview
tblview是testView的子视图
有tblview和testView布局
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
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
}
})
这是我的定制手机
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")
}
}您好,欢迎来到Stack Overflow!请提供有关哪些不起作用的详细信息。是否有任何错误,或者它是否没有按照您预期的方式工作?请参阅和。