convert a segmented control order to two separate funcs

19 Views Asked by At

In my swift code below It uses a a segmented control to control the rotation of an object. The segmented control sets the direction either clockwise or counter clockwise. Then there is an action button that starts the action. What I would like to do is remove the segmented control and the action button and have the left button set the direction counter clockwise and start the action and then right button set the direction to clockwise and start the action.

  var rotation: UIViewPropertyAnimator?
    
    var isRotating: Bool = false
    enum RotationDirection: Int {
        case clockwise
        case counterclockwise
    }
 @IBAction func doRotate(_ sender: UIButton) {
        defer {
            let buttonTitle = isRotating ? "Stop rotating" : "Rotate"
            sender.setTitle(buttonTitle, for: .normal)
        }
        if isRotating  {
            isRotating = false
        } else {
            isRotating = true
            rotate()
        }
    }
    
    func rotate() {
        
        guard isRotating else { return }
        
        let rotationAngle = rotationDirectionControl.selectedSegmentIndex == RotationDirection.clockwise.rawValue ? CGFloat.pi/2 : -CGFloat.pi/2
        
        
        self.rotation = UIViewPropertyAnimator(duration: 0.5, curve: .linear, animations: {
            self.football.transform = self.football.transform.rotated(by: rotationAngle)
        })
        
        self.rotation?.addCompletion { [weak self] (_) in
            self?.rotate()
        }
        self.rotation?.startAnimation()
        
        
        

    }

Combine that into these 2 methods

   @objc func leftM(){
    
    }
    
    
    @objc func rightM(){
        
    }
0

There are 0 best solutions below