ios – Animating UIImageView rotation during UISwipeActionsConfiguration in UITableView


I am trying to add a swipe action to my UITableView cell with a rotating icon animation. The goal is to have an arrow icon initially pointing up and smoothly rotating to the right during the swipe action. I’ve attempted to achieve this by using UIContextualAction and rotating the UIImageView associated with the action. However, I’m facing issues with the animation.

Here is a simplified version 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.transform = CGAffineTransform(rotationAngle: angle)
                }
            }
        }
    }
}


override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let moreAction = UIContextualAction(style: .normal, title: "More") { (action, view, completion) in
        // Perform actions when "More" button is tapped
        print("More option selected for item: \(self.items[indexPath.row])")
        // Add your custom actions here

        // Call completion handler to indicate that the action was performed
        completion(true)
    }

    // Customize the appearance of the action button
    moreAction.backgroundColor = UIColor.blue

    // Add an image view to the action
    let imageView = UIImageView(image: UIImage(systemName: "arrow.up.circle"))
    imageView.contentMode = .scaleAspectFit

    // Rotate the image view from pointing up to pointing right
    imageView.transform = CGAffineTransform(rotationAngle: 0)

    // Add the image view to the action's view
    moreAction.image = UIGraphicsImageRenderer(size: imageView.bounds.size).image { _ in
        imageView.drawHierarchy(in: imageView.bounds, afterScreenUpdates: true)
    }

    let swipeConfiguration = UISwipeActionsConfiguration(actions: [moreAction])
    return swipeConfiguration
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img