Yestay Muratov
Yestay Muratov

Reputation: 1370

swift, rotate image view and set rotated image view to uibutton

I want to click button and rotate its image with animation. After animation is done I want to set new rotated image to the button. However for some reason the rotated image is not set to button for normal state. It is set on highlighted state. I have boolean value isPriceOrdered , according to this bool value I set arrow up or down. Here is a code:

   UIView.animateWithDuration(0.3, animations: {
             self.buttonimage.imageView!.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
        }, completion: {
            (success) in
            if self.isPriceOrderAsc == true {
                self.isPriceOrderAsc = false
                let rotatedimage = UIImage(CGImage: (self.buttonimage.imageView!.image!.CGImage)!, scale: CGFloat(1.0), orientation: UIImageOrientation.DownMirrored)

                self.buttonimage.setImage(rotatedimage, forState: .Normal)


            }else{
                self.isPriceOrderAsc = true
                let rotatedimage = UIImage(CGImage: (self.buttonimage.imageView?.image!.CGImage)!, scale: CGFloat(1.0), orientation: UIImageOrientation.UpMirrored)

                self.buttonimage.setImage(rotatedimage, forState: .Normal)
            }
    })

Upvotes: 0

Views: 931

Answers (1)

Yestay Muratov
Yestay Muratov

Reputation: 1370

The problem was that I didt save original transform of button before transforming the image. Before transforming the image degree save the transform original image. Here is a code:

var originalTransform: CGAffineTransform?

 UIView.animateWithDuration(0.3, animations: {

            if self.buttonPriceOrder.imageView != nil {
                originalTransform = self.buttonPriceOrder.imageView!.transform
                self.buttonPriceOrder.imageView!.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
            }

        }, completion: {
            (success) in

            if success {

                self.buttonPriceOrder.imageView?.transform = originalTransform!

                if self.isPriceOrderAsc == true {
                    self.isPriceOrderAsc = false
                    let rotatedimage = UIImage(CGImage: (self.buttonPriceOrder.imageView!.image!.CGImage)!, scale: CGFloat(1.0), orientation: UIImageOrientation.DownMirrored)
                    self.buttonPriceOrder.setImage(rotatedimage, forState: .Normal)

                }else{
                    self.isPriceOrderAsc = true
                    let rotatedimage = UIImage(CGImage: (self.buttonPriceOrder.imageView?.image!.CGImage)!, scale: CGFloat(1.0), orientation: UIImageOrientation.UpMirrored)
                    self.buttonPriceOrder.imageView?.image = nil
                    self.buttonPriceOrder.setImage(rotatedimage, forState: .Normal)

                }
                self.isFilterActive = true
                self.delegate.filterUpdated(self)
            }

    })

After image is rotated before setting that image to button, set button transform to original one.

Upvotes: 1

Related Questions