如何为tapped tableView单元设置动画并为其设置动画,就像使用CGAffineTransformScale一样,并在滚动tableView时删除动画。如果单元格点击被轻敲,我需要调用didSelect tableView单元格方法。
import UIKit
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var cellDataView: UIView!
var longTapCallback: ((_ success: Bool) -> Void)?
override func awakeFromNib() {
super.awakeFromNib()
self.setLongTapGuesture()
}
func setLongTapGuesture() {
let guesture = UILongPressGestureRecognizer(target: self, action: #selector(CustomTableViewCell.lognTaponCell))
self.contentView.addGestureRecognizer(guesture)
}
@objc func lognTaponCell(guesture: UILongPressGestureRecognizer) {
if guesture.state != .ended {
self.longTapCallback?(true)
} else {
self.longTapCallback?(false)
}
}
}
import UIKit
class AnimationTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 15
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as? CustomTableViewCell
cell?.longTapCallback = { (success) in
if success {
self.selectedIndexPath = indexPath
self.startLongTapGuesture(atCell: cell!)
} else {
self.stopLongTapGuesture(atCell: cell!)
}
}
return cell!
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Did Select Called")
}
}
extension AnimationTableViewController {
func startLongTapGuesture(atCell cell: CustomTableViewCell) {
UIView.animate(withDuration: 0.0, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 6, options: .beginFromCurrentState, animations: {
}, completion: { (finished) in
cell.contentView.transform = CGAffineTransform.init(scaleX: 1.6, y: 1.6)
})
}
func stopLongTapGuesture(atCell cell: CustomTableViewCell) {
UIView.animate(withDuration: 0.0, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 6, options: [], animations: {
}, completion: { (finished) in
cell.contentView.transform = CGAffineTransform.init(scaleX: 1, y: 1)
})
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120.0
}}
来自模拟器的屏幕截图:
答案 0 :(得分:1)
正如我上面提到的,我使用了一个闭包回调来获取TableViewCell上的长按手势。
我的情景:
如果检测到TableView滚动,我将通过迭代 scrollViewDidScroll 方法中的可见单元格来删除应用于单元格Object的动画,如下所示。
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
let cellsArray = self.tableView.visibleCells
for cell in cellsArray {
UIView.animate(withDuration: 0.0, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 6, options: [], animations: {
}, completion: { (finished) in
cell.contentView.transform = CGAffineTransform.init(scaleX: 1, y: 1)
})
}}
我修改了 cellForRowAtIndexPath 方法,如下所示:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as? CustomTableViewCell
cell?.longTapCallback = { (success) in
if success {
//Identifies the Long Tap on the Cell.
self.startLongTapGuesture(atCell: cell!)
} else {
//Identifies the Release of Long tap on Cell.
self.tableView(self.tableView, didSelectRowAt: indexPath)
}
}
return cell!
}
这适用于Similer到AppStore应用程序。感谢你提供有用的想法,你评论 Mr.Sandeep Bhandari's。