2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CGAffineTransform

Last updated at Posted at 2018-12-23

CGAffineTransformのパターンを理解するため作成

ezgif.com-video-to-gif

#ソースコード
Touchすると順に
Acale,Miller,Rotate,Trans,Combine
が処理される

ViewController.swift

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var label: UILabel!
    
    var transScale = CGAffineTransform() //拡大
    var transTrans = CGAffineTransform() //移動
    var transRotate = CGAffineTransform() //回転
    var transMiller = CGAffineTransform() //反転
    
    //画面の横幅・縦幅取得
    var screenWidth: CGFloat!
    var screenHeight: CGFloat!
    
    var counter = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        //画面の横幅、縦幅を取得
        screenWidth = view.frame.size.width
        screenHeight = view.frame.size.height
        ////imageViewの初期化
        initImageView()
    }
    
    func initImageView(){
        // UIimageインスタンスの生成
        let image:UIImage = UIImage(named: "sample")!
        
        //UIimage 初期化
        imageView.image = image
        
        //画像を中心に位置するよう設定
        imageView.center = CGPoint(x: screenWidth / 2, y: screenHeight / 2)
        
        //UIimageviewをビューに追加
        self.view.addSubview(imageView)
    }
    
    @IBAction func button(_ sender: Any) {
        if(counter == 0){
            //画像を拡大
            transScale = CGAffineTransform(scaleX: 2, y: 2)
            imageView.transform = transScale
            
            label.text = "Scale"
        }
        else if(counter == 1){
            //画像を反転
            transMiller = CGAffineTransform(scaleX: -1.5, y: 1.5)
            imageView.transform = transMiller
            
            label.text = "Miller"
        }
        else if(counter == 2){
            //画像を35度回転 CGFloat.pi =πを表す
            let angle = 35 * CGFloat.pi / 180
            transRotate = CGAffineTransform(rotationAngle: CGFloat(angle))
            imageView.transform = transRotate
            
            label.text = "Rotate"
            
        }
        else if(counter == 3){
            //画像をx=20 Y=30 移動
            transTrans = CGAffineTransform(translationX: 20, y: 30)
            imageView.transform = transTrans
            
            label.text = "Trans"
        }
        else if(counter == 4){
            //affineを合成する
            //画像を反転と縦長に拡大する
            let trans1 = CGAffineTransform(scaleX: -1.5 , y: 2.0)
            let trans2 = CGAffineTransform(translationX: -50, y: -30)
            //二つのaffineを合成する
            let trans3 = trans1.concatenating(trans2);
            imageView.transform = trans3
            
            label.text = "Combine"
        }
        else{
            //元に戻す
            imageView.center = CGPoint(x:screenWidth / 2, y: screenHeight / 2)
            transScale = CGAffineTransform(scaleX: 1, y: 1)
            imageView.transform = transScale
            label.text = "Original"
            
            //カウンターが5になったときに−1する
            counter = -1
        }
        
        //ボタンが押されるたびに+1される カウンターを+1してる
        counter += 1
    }

}

#画面の横幅・縦幅取得
var screenWidth: CGFloat!
var screenHeight: CGFloat!

#二つのaffineを合成する
trans1.concatenating(trans2)

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?