Share
Embed
Mastering CGAffineTransform
マスタリング CGAffineTransform
iOSDC Japan 2018 LT
撮影した映像の向きがおかしい?Viewの位置計算で困ってる?
とても良い機会です。5分でCGAffineTransformをマスターしましょう!
CGAffineTransformはちっぽけなstructですが、使いこなせば非常に強力なAPIです。基本知識から組み合わせて掛けるときの考え方のコツなどを具体例と共にまとめました。もう座標計算に悩んで時間を使うことはありません!
Chiharu Nameki
September 01, 2018
Transcript
31 slides
-
ߦ ઍय़ʢͳΊ͖ ͪΔʣ גࣜձࣾαΠόʔΤʔδΣϯτ ϚελϦϯά CGAffineTransform iOSDC Japan 2018 Sep.
1 Twitter, Qiita, GitHub @Ridwy -
SwiftͰͷఆٛ public struct CGAffineTransform { public var a: CGFloat public
var b: CGFloat public var c: CGFloat public var d: CGFloat public var tx: CGFloat public var ty: CGFloat } -
ੜ static var identity: CGAffineTransform init(a: CGFloat, b: CGFloat, c:
CGFloat, d: CGFloat, tx: CGFloat, ty: CGFloat) init(translationX tx: CGFloat, y ty: CGFloat) init(scaleX sx: CGFloat, y sy: CGFloat) init(rotationAngle angle: CGFloat) ฏߦҠಈ ֦େɾॖখ ճస -
ద༻ let q = p.applying(transform) CGPoint CGSize
αΠζ CGRect ྖҬ pʹtransformΛద༻ͯ͠qʹҠ͢ (x, y) (x, y) width height width height -
q = pABC let q = p.applying(a).applying(b).applying(c) let q =
p.applying(a.concatenating(b).concatenating(c)) ࿈ଓͯ͠ద༻ concatenating Ͱ1ͭͷมʹ·ͱΊΒΕΔ ͜ΕΒֶతʹɺ͜͏ ίʔυͱࣜͰॻ͘ॱ൪͕Ұॹʂ -
let length: CGFloat = 512 let squareSize = CGSize(width: length,
height: length) UIGraphicsBeginImageContextWithOptions(squareSize, false, 1) let ctx = UIGraphicsGetCurrentContext()! let width = image.size.width let height = image.size.height let scale = min(1.0, length / min(width, height)) let transform = CGAffineTransform.identity .concatenating(.init(translationX: -width/2, y: -height/2)) .concatenating(.init(scaleX: scale, y: scale)) .concatenating(.init(translationX: length/2, y: length/2)) ctx.concatenate(transform) image.draw(in: CGRect(origin: .zero, size: image.size)) let resultImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() ཁٻΛͯ͢ຬͨ͢ɺUIImageΛม͢Δίʔυʢશจʣ -
let length: CGFloat = 512 let squareSize = CGSize(width: length,
height: length) UIGraphicsBeginImageContextWithOptions(squareSize, false, 1) let ctx = UIGraphicsGetCurrentContext()! let width = image.size.width let height = image.size.height let scale = min(1.0, length / min(width, height)) let transform = CGAffineTransform.identity .concatenating(.init(translationX: -width/2, y: -height/2)) .concatenating(.init(scaleX: scale, y: scale)) .concatenating(.init(translationX: length/2, y: length/2)) ctx.concatenate(transform) image.draw(in: CGRect(origin: .zero, size: image.size)) let resultImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() ॖখͱΫϩοϓͷͨΊʹΞϑΟϯมΛར༻ -
let length: CGFloat = 512 let squareSize = CGSize(width: length,
height: length) UIGraphicsBeginImageContextWithOptions(squareSize, false, 1) let ctx = UIGraphicsGetCurrentContext()! let width = image.size.width let height = image.size.height let scale = min(1.0, length / min(width, height)) let transform = CGAffineTransform.identity .concatenating(.init(translationX: -width/2, y: -height/2)) .concatenating(.init(scaleX: scale, y: scale)) .concatenating(.init(translationX: length/2, y: length/2)) ctx.concatenate(transform) image.draw(in: CGRect(origin: .zero, size: image.size)) let resultImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() ॖখͱΫϩοϓͷͨΊʹΞϑΟϯมΛར༻ -
let length: CGFloat = 512 let squareSize = CGSize(width: length,
height: length) UIGraphicsBeginImageContextWithOptions(squareSize, false, 1) let ctx = UIGraphicsGetCurrentContext()! let width = image.size.width let height = image.size.height let scale = min(1.0, length / min(width, height)) let transform = CGAffineTransform.identity .concatenating(.init(translationX: -width/2, y: -height/2)) .concatenating(.init(scaleX: scale, y: scale)) .concatenating(.init(translationX: length/2, y: length/2)) ctx.concatenate(transform) image.draw(in: CGRect(origin: .zero, size: image.size)) let resultImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() ॖখͱΫϩοϓͷͨΊʹΞϑΟϯมΛར༻ -
let length: CGFloat = 512 let squareSize = CGSize(width: length,
height: length) UIGraphicsBeginImageContextWithOptions(squareSize, false, 1) let ctx = UIGraphicsGetCurrentContext()! let width = image.size.width let height = image.size.height let scale = min(1.0, length / min(width, height)) let transform = CGAffineTransform.identity .concatenating(.init(translationX: -width/2, y: -height/2)) .concatenating(.init(scaleX: scale, y: scale)) .concatenating(.init(translationX: length/2, y: length/2)) ctx.concatenate(transform) image.draw(in: CGRect(origin: .zero, size: image.size)) let resultImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() ॖখͱΫϩοϓͷͨΊʹΞϑΟϯมΛར༻ -
let length: CGFloat = 512 let squareSize = CGSize(width: length,
height: length) UIGraphicsBeginImageContextWithOptions(squareSize, false, 1) let ctx = UIGraphicsGetCurrentContext()! let width = image.size.width let height = image.size.height let scale = min(1.0, length / min(width, height)) let transform = CGAffineTransform.identity .concatenating(.init(translationX: -width/2, y: -height/2)) .concatenating(.init(scaleX: scale, y: scale)) .concatenating(.init(translationX: length/2, y: length/2)) ctx.concatenate(transform) image.draw(in: CGRect(origin: .zero, size: image.size)) let resultImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() ॖখͱΫϩοϓͷͨΊʹΞϑΟϯมΛར༻ -
let length: CGFloat = 512 let squareSize = CGSize(width: length,
height: length) UIGraphicsBeginImageContextWithOptions(squareSize, false, 1) let ctx = UIGraphicsGetCurrentContext()! let width = image.size.width let height = image.size.height let scale = min(1.0, length / min(width, height)) let transform = CGAffineTransform.identity .concatenating(.init(translationX: -width/2, y: -height/2)) .concatenating(.init(scaleX: scale, y: scale)) .concatenating(.init(translationX: length/2, y: length/2)) ctx.concatenate(transform) image.draw(in: CGRect(origin: .zero, size: image.size)) let resultImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() ॖখͱΫϩοϓͷͨΊʹΞϑΟϯมΛར༻ -
ΞϑΟϯมΛ͞Βʹม͢Δ func translatedBy(x tx: CGFloat, y ty: CGFloat) -> CGAffineTransform
func scaledBy(x sx: CGFloat, y sy: CGFloat) -> CGAffineTransform func rotated(by angle: CGFloat) -> CGAffineTransform -
มͷมʁ transform = transform .translatedBy(x: 10, y: 0) .rotated(by: .pi/2)
.scaledBy(x: 1.4, y: 1.4) .translatedBy(x: 5, y: 0) ʢಓҊͷΑ͏ͳΠϝʔδʣ ϩʔΧϧͷ࠲ඪܥ͕มΘ͍ͬͯ͘ x y -
มͷมʁ transform = transform .translatedBy(x: 10, y: 0) .rotated(by: .pi/2)
.scaledBy(x: 1.4, y: 1.4) .translatedBy(x: 5, y: 0) ʢಓҊͷΑ͏ͳΠϝʔδʣ ϩʔΧϧͷ࠲ඪܥ͕มΘ͍ͬͯ͘ x y -
มͷมʁ transform = transform .translatedBy(x: 10, y: 0) .rotated(by: .pi/2)
.scaledBy(x: 1.4, y: 1.4) .translatedBy(x: 5, y: 0) ʢಓҊͷΑ͏ͳΠϝʔδʣ ϩʔΧϧͷ࠲ඪܥ͕มΘ͍ͬͯ͘ x y -
มͷมʁ transform = transform .translatedBy(x: 10, y: 0) .rotated(by: .pi/2)
.scaledBy(x: 1.4, y: 1.4) .translatedBy(x: 5, y: 0) ʢಓҊͷΑ͏ͳΠϝʔδʣ ϩʔΧϧͷ࠲ඪܥ͕มΘ͍ͬͯ͘ x y -
มͷมʁ transform = transform .translatedBy(x: 10, y: 0) .rotated(by: .pi/2)
.scaledBy(x: 1.4, y: 1.4) .translatedBy(x: 5, y: 0) ʢಓҊͷΑ͏ͳΠϝʔδʣ ϩʔΧϧͷ࠲ඪܥ͕มΘ͍ͬͯ͘ x y -
ֶతʹɺ͜͏ concatenating()CGRectͳͲͷapplying()ͱରଆ͔Βֻ͚Δ let transform = CGAffineTransform.identity .translatedBy(x: 10, y: 0)
// A .rotated(by: .pi/2) // B .scaledBy(x: 1.4, y: 1.4) // C .translatedBy(x: 5, y: 0) // D let q = p.applying(transform) q = pDCBA -
@objc func handlePan(_ recognizer: UIPanGestureRecognizer) { let t = recognizer.translation(in:
stamp.superview) stamp.center = stamp.center.applying(.init(translationX: t.x, y: t.y)) recognizer.setTranslation(.zero, in: stamp.superview) } @objc func handlePinch(_ recognizer: UIPinchGestureRecognizer) { let scale = recognizer.scale stamp.transform = stamp.transform.scaledBy(x: scale, y: scale) recognizer.scale = 1 } @objc func handleRotation(_ recognizer: UIRotationGestureRecognizer){ let rotation = recognizer.rotation stamp.transform = stamp.transform.rotated(by: rotation) recognizer.rotation = 0 } ֦େॖখͱճస → transform Ҡಈ → center ελϯϓͷUIViewͷϓϩύςΟΛมߋ࣮ͯ͠ݱ -
·ͱΊʢߟ͑ํͷίπʣ • CGPoint, CGSize, CGRectͷ applying() superviewͷ࠲ඪܥͷதͰద༻͞ΕΔ • ΞϑΟϯมΛ͞Βʹม͢Δϝιου
translatedBy(x:y:), scaledBy(x:y:), rotated(by:) ϩʔΧϧͳ࠲ඪܥΛม͢Δ