Transform.swift 0.1.2

Transform.swift 0.1.2

TestsTested āœ“
LangLanguage SwiftSwift
License MIT
ReleasedLast Release May 2016
SPMSupports SPM āœ—

Maintained by Jeff Hurray.



Transform.swift

Transform.swift offers a Swiftier API for applying transforms to UIView and its subclasses.

let scale = Transform.Scale(sx: 0.5, sy: 0.5)
let rotate = Transform.Rotate(rotation: .Degrees(45))
let scaleAndRotate = scale + rotate
myView.transform += scaleAndRotate

myView.transform3D = .Translate(tx: 40, ty: 0, tz: 100).CATransform

Instalation

Usage

Transform.swift exposes 3 enums, Transform, Transform3D, and Rotation.

Rotation

A convenience enum that allows the developer to easily define the rotation they want. The cases are pretty self explanitory.

  • .Degrees(degrees: CGFloat)
  • .Radians(radians: Double)
    • Takes a double to make it easier to pass the M_PI definitions from Darwin.C.Math

Transform

  • .Identity
  • .Translate(tx: CGFloat, ty: CGFloat)
  • .Scale(sx: CGFloat, sy: CGFloat)
  • .Rotate(rotation: Rotation)
  • .Init(a: CGFloat, b: CGFloat, c: CGFloat, d: CGFloat, tx: CGFloat, ty: CGFloat) for the adventurous type šŸ˜Ž
  • .Custom(t: CGAffineTransform)

Methods

A group of methods that calculate how the transform affects a CGRect, CGSize, and CGPoint respectively.

public func transformedRect(rect: CGRect) -> CGRect
public func transformedSize(size: CGSize) -> CGSize
public func transformedPoint(point: CGPoint) -> CGPoint

Returns the underlying CGAffineTransform

public var CGATransform: CGAffineTransform { get }

Transform3D

  • .Identity
  • .Translate(tx: CGFloat, ty: CGFloat, tz: CGFloat)
  • .Scale(sx: CGFloat, sy: CGFloat, sz: CGFloat)
  • .Rotate(rotation: Rotation, x: CGFloat, y: CGFloat, z: CGFloat)
  • .Affine(t: CGAffineTransform)
  • .Custom(t: CATransform3D)

Methods

Returns the underlying CATransform3D

public var CATransform : CATransform3D { get }

Returns a true if the 3d transform can be converted into an affine transform, false if it cannot.

public var isAffine: Bool { get }

Returns the underlying CGAffineTransform. Will throw an error if the 3D transform cannot be converted into an affine transform.

public func affineTransform() throws -> CGAffineTransform 

Shared Utilities

  • Inverting: let inverted = t.inverted
  • Identity Conformation: let isIdentity = t.isIdentity
  • Concatenation: let scaleAndMove = .Scale(sx: 0.5, sy:1).concat(.Translate(tx: 40, ty: -100))

Operators

  • == Equality
    • Returns Bool
    • Transform == Transform
    • Transform == CGAffineTransform
    • Transform3D == Transform3D
    • Transform3D == CATransform3D

  • + Additive
    • Returns the type being added
    • Transform + Transform
    • CGAffineTransform + CGAffineTransform
    • Transform3D + Transform3D
    • CATransform3D + CATransform3D

  • += Assignment
    • Modifies the left variable of the operator
    • Transform += Transform
    • CGAffineTransform += Transform
    • Transform3D += Transform3D
    • CATransform3D += Transform3D

  • <- Assignment
    • Assigns a value to the left variable of the operator
    • CGAffineTransform <- Transform
    • CATransform3D <- Transform3D
    • Semantics to assign the transform enum to the old API since you cant overload the = operator.

UIView Extension

2 Variables have been added to UIView to add settable and gettable interfaces for Transform and Transform3D.

var affineTransform: Transform
var transform3D: Transform3D

Contact Info

Feel free to email me at [email protected]. Iā€™d love to hear your thoughts on this, or see examples where this has been used.

You can also hit me up on twitter @JeffHurray.

Contributing

If you want to add functionality please open an issue and/or create a pull request.

License

Transform.swift is available under the MIT license. See the LICENSE file for more information.