UIViewを回転させる – Swift3 Xcode9

環境

  • UIView
  • 回転
  • Swift3
  • Xcode9
  • Swift
  • Xcode
  • animation
  • transform

実行

on.gif

説明

uiviewのanimateメソッドを使うとtransformでuiviewに視覚的な変化を与える際に動きを加えることができてリッチなUIを実装する際とても便利です。

実装

//
//  ViewController.swift
//  Rotate-view-sample
//
//  Created by ryosuke-hujisawa on 2017/10/07.
//  Copyright © 2017年 ryosuke-hujisawa. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var RotateView: UIView!
    @IBAction func RotateViewOnTap(_ sender: Any) {

        UIView.animate(withDuration: 0.5,animations: { () -> Void in
            self.RotateView.transform = self.RotateView.transform.scaledBy(x: -1.0, y: 1.0)
            self.RotateView.transform = self.RotateView.transform.scaledBy(x: -1.0, y: 1.0)
        })


    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

ソース

GitHub