Flipping an UIImage

Hi all,

I'm making an app that I have to have a flip animation when someone taps a button.


Here's the code:


import WatchKit
import Foundation


class InterfaceController: WKInterfaceController {

  @IBOutlet var coin: WKInterfaceImage!
  @IBOutlet var txt: WKInterfaceLabel!

    override func awake(withContext context: Any?) {
        super.awake(withContext: context)
        
        txt.setText("Heads or Tails?")


  }
  @IBAction func CallIt()
  {

  let array = ["Heads","Tails"]
  let randomIndex = Int(arc4random_uniform(UInt32(array.count)))

  if randomIndex == 1
  {

  let up = UIImage(named: "Heads")

  coin.setImage(up)
  txt.setText("Heads")

  }else {

  let down = UIImage(named: "Tails")
  coin.setImage(down)
  txt.setText("Tails")

}
}
}


Thanks in advance,

Dan Uff

Replies

The flip is equivalent to a scale change where you scale x by -1.


You will want to use a CGAffineTransformScale. In Swift I think it will be something like:


view.transform=view.transform.scaledBy(x: -1.0 , y: 1.0)

Thanks for the help. But it doesn't seem to work, I get a:


Use of unresolved identifier 'view'


I also read somewhere that the watch doesn't allow an image to flip (unless it was another viewController, which wouldnt work in this case), and that the UIView (or view) wouldn't be recgonized (which is hard to believe, I know).


Thanks for the info anyway. I'm still looking!

Dan

The 'view' referered to your UIImageView. You will be able to flip an image on a watch - not the actual view itself but the content of the UIImageView or the UIImage itself. The key is to go back to the basic cgImage and scale x by -1. I found this Swift code with a simple search:


h ttps://gist.github.com/schickling/b5d86cb070130f80bb40



Your case is, I think, .leftMirrored

Thanks. I'll give it a try.


Dan