I’m making an attempt so as to add a swipe motion to my UITableView cell with a rotating icon animation. The objective is to have an arrow icon initially pointing up and easily rotating to the precise through the swipe motion. I’ve tried to realize this by utilizing UIContextualAction and rotating the UIImageView related to the motion. Nevertheless, I am going through points with the animation.
Here’s a simplified model of my code:
class CustomTableViewCell: UITableViewCell {
func rotateIcon(angle: CGFloat) {
for subview in contentView.subviews {
if let imageView = subview as? UIImageView {
UIView.animate(withDuration: 0.25) {
imageView.rework = CGAffineTransform(rotationAngle: angle)
}
}
}
}
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let moreAction = UIContextualAction(type: .regular, title: "Extra") { (motion, view, completion) in
// Carry out actions when "Extra" button is tapped
print("Extra choice chosen for merchandise: (self.gadgets[indexPath.row])")
// Add your customized actions right here
// Name completion handler to point that the motion was carried out
completion(true)
}
// Customise the looks of the motion button
moreAction.backgroundColor = UIColor.blue
// Add a picture view to the motion
let imageView = UIImageView(picture: UIImage(systemName: "arrow.up.circle"))
imageView.contentMode = .scaleAspectFit
// Rotate the picture view from pointing as much as pointing proper
imageView.rework = CGAffineTransform(rotationAngle: 0)
// Add the picture view to the motion's view
moreAction.picture = UIGraphicsImageRenderer(measurement: imageView.bounds.measurement).picture { _ in
imageView.drawHierarchy(in: imageView.bounds, afterScreenUpdates: true)
}
let swipeConfiguration = UISwipeActionsConfiguration(actions: [moreAction])
return swipeConfiguration
}