Home Multi selection table view - Rotate image in custom header when section expands/collapses
Reply: 2

Multi selection table view - Rotate image in custom header when section expands/collapses

Alex
1#
Alex Published in 2018-01-13 05:16:31Z

Problem: When a section is expanded, the arrow in the section header rotates to point up. When I tap a cell, the arrow starts pointing down & rotates clockwise to point up (the arrow "twitches").

How do I get rid of the arrow's "twitch"? I want the arrow to point up when the section is expanded and point down when the section is collapsed.

CollapsibleFilterTableViewHeader

func setExpanded(expanded: Bool) {
    //Get angle of arrow (0.0 for pointing up or 180.0 for pointing down)
    let rad: Double = atan2( Double(arrowImageView.transform.b), Double(arrowImageView.transform.a))
    let deg: CGFloat = CGFloat(rad) * (CGFloat(180) / CGFloat.pi )

    if (expanded && deg == 0.0) {
        arrowImageView.rotate(.pi)
    }
}

FiltersViewController

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        header.setExpanded(expanded: filterSection.isExpanded)
        header.delegate = self
        return header
    }

func toggleSection(header: CollapsibleFilterTableViewHeader, section: Int) {
        let isExpanded = !filterSection.isExpanded

        // Toggle isExpanded
        filterSection.isExpanded = isExpanded
        header.setExpanded(expanded: isExpanded)

        filtersTableView.reloadSections(NSIndexSet(index: section) as IndexSet, with: .automatic)
    }

Sources: Stackoverflow Question, Medium Article

Alex
2#
Alex Reply to 2018-01-13 05:28:46Z

Try this out

func setExpanded(expanded: Bool) {

      if (expanded) {
        arrowImageView.transform = CGAffineTransform(rotationAngle: CGFloat.pi)

      } else {
       arrowImageView.transform =  CGAffineTransform.identity

      } 
    }

Set rotation when expanded otherwise provide CGAffineTransform.identity to rest

Hope it is helpful to you

Bhadresh Kathiriya
3#
Bhadresh Kathiriya Reply to 2018-01-13 05:31:12Z

Please try this code

  func setExpanded(expanded: Bool) {

  if (expanded) {
       arrowImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi));

  }else{
        arrowImageView.transform = CGAffineTransform(rotationAngle: 0);
  }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.33405 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO