syds
syds

Reputation: 322

UIButton with shadow rotating without shadow rotating

Currently I have a button that draws its properties from a UIButton extension.

    func mapControllerButtons(image: String, selector: Selector) -> UIButton {
        let button = UIButton()
        button.setImage(UIImage(named: image)!.withRenderingMode(.alwaysOriginal), for: .normal)
        button.layer.shadowColor    = UIColor.black.cgColor
        button.layer.shadowOffset   = CGSize(width: 0.0, height: 3.0)
        button.layer.shadowRadius   = 5
        button.layer.shadowOpacity  = 0.5
        button.layer.masksToBounds  = false
        button.addTarget(self, action: selector, for: .touchUpInside)
        return button
    }
}

This creates a shadow underneath the button, however I have another function that rotates the button around, to signal to the user that data is being loaded from an API.

Currently what I am doing is creating the button with a shadow and overlaying it with a button that does not have a shadow. The shadowless button is the one that will be tapped and rotate.

I have a gut feeling that there's gotta be a better way of doing this than creating two buttons having one eat memory just so it can be a placeholder (for the shadow effect).

How can I go about creating a button that can rotate with animateWith func while the shadow stays in its place?

Here is my animation code block.

    func animateRefreshButton() {
        UIView.animate(withDuration: 1.0, delay: 0.0, options: .repeat, animations: {
            self.refreshButton.transform = CGAffineTransform(rotationAngle: .pi)
        })
    }

Upvotes: 0

Views: 35

Answers (1)

Rob C
Rob C

Reputation: 5073

You could try rotating the button's imageView instead of the whole button:

func animateRefreshButton() {
    UIView.animate(withDuration: 1.0, delay: 0.0, options: .repeat, animations: {
        self.refreshButton.imageView?.transform = CGAffineTransform(rotationAngle: .pi)
    })
}

Upvotes: 1

Related Questions