我有这个CABasicAnimation代码,我想将此代码执行到TableView的单行。这可能吗?我想创建一个TableViewRow的抖动效果。
func shakeView(){
let midX = tableView.center.x
let midY = tableView.center.y
let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.06
animation.repeatCount = 4
animation.autoreverses = true
animation.fromValue = CGPoint(x: midX - 10, y: midY)
animation.toValue = CGPoint(x: midX + 10, y: midY)
tableView.layer.add(animation, forKey: "position")
}
答案 0 :(得分:2)
下面您将找到在敲击单元时摇动单元格的代码(根据需要进行调整)。我已经改变了你的方法,因为它为表格视图设置了动画而不是单元格。
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseID", for: indexPath) as UITableViewCell
cell.textLabel?.text = "test"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
shakeView(cell as? UIView)
}
func shakeView(_ view: UIView?) {
guard let view = view else { return }
let midX = view.center.x
let midY = view.center.y
let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.06
animation.repeatCount = 4
animation.autoreverses = true
animation.fromValue = CGPoint(x: midX - 10, y: midY)
animation.toValue = CGPoint(x: midX + 10, y: midY)
view.layer.add(animation, forKey: "position")
}
}