Rotating a UIButton by 90 degrees every time the button is clicked

后端 未结 2 833
攒了一身酷
攒了一身酷 2021-02-07 01:22

How do you rotate a UIButton by 90 degrees each time the button is clicked and also keep track of each rotated position/angle?

Here is the code I have so far but it only

2条回答
  •  失恋的感觉
    2021-02-07 01:51

    Swift 4:

    @IBOutlet weak var expandButton: UIButton!
    
    var sectionIsExpanded: Bool = true {
        didSet {
            UIView.animate(withDuration: 0.25) {
                if self.sectionIsExpanded {
                    self.expandButton.transform = CGAffineTransform.identity
                } else {
                    self.expandButton.transform = CGAffineTransform(rotationAngle: -CGFloat.pi / 2.0)
                }
            }
        }
    }
    
    @IBAction func expandButtonTapped(_ sender: UIButton) {
        sectionIsExpanded = !sectionIsExpanded
    }
    

提交回复
热议问题