Skip to content

Instantly share code, notes, and snippets.

@jkosoy
Created April 14, 2016 23:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jkosoy/aa94d4e86972c2136375c7f500ffa473 to your computer and use it in GitHub Desktop.
Save jkosoy/aa94d4e86972c2136375c7f500ffa473 to your computer and use it in GitHub Desktop.
Calculating for different device Aspect Ratios, used in Melody Jams
//
// CoreGraphics+AspectRatio.swift
// EmptySpriteKitGame
//
// Created by Jamie Kosoy on 11/6/15.
// Copyright © 2015 Arbitrary. All rights reserved.
//
import UIKit
import Foundation
extension CGRect {
init(sx: CGFloat, sy: CGFloat, swidth: CGFloat, sheight: CGFloat) {
self.init(origin: CGPoint(sx: sx, sy: sy), size: CGSize(swidth: swidth, sheight: sheight))
}
}
extension CGPoint {
init(sx: CGFloat, sy: CGFloat) {
self.init(x: sx.xForIdiom(), y: sy.yForIdiom())
}
func pointForIdiom() -> CGPoint {
return CGPoint(sx: self.x, sy: y)
}
}
extension CGSize {
init(swidth: CGFloat, sheight: CGFloat) {
self.init(width: swidth.widthForIdiom(), height: sheight.heightForIdiom())
}
func sizeForIdiom() -> CGSize {
return CGSize(swidth: self.width, sheight: self.height)
}
// http://stackoverflow.com/questions/6278876/how-to-know-the-image-size-after-applying-aspect-fit-for-the-image-in-an-uiimage
static func aspectFit(aspectRatio: CGSize, inout boundingSize: CGSize) -> CGSize {
let mW = boundingSize.width / aspectRatio.width;
let mH = boundingSize.height / aspectRatio.height;
if( mH < mW ) {
boundingSize.width = boundingSize.height / aspectRatio.height * aspectRatio.width;
}
else if( mW < mH ) {
boundingSize.height = boundingSize.width / aspectRatio.width * aspectRatio.height;
}
return boundingSize;
}
static func aspectFill(aspectRatio: CGSize, inout minimumSize: CGSize) -> CGSize {
let mW = minimumSize.width / aspectRatio.width;
let mH = minimumSize.height / aspectRatio.height;
if( mH > mW ) {
minimumSize.width = minimumSize.height / aspectRatio.height * aspectRatio.width;
}
else if( mW > mH ) {
minimumSize.height = minimumSize.width / aspectRatio.width * aspectRatio.height;
}
return minimumSize;
}
}
extension CGFloat {
private static let WidthRatio: CGFloat = 1024 / 568
private static let HeightRatio: CGFloat = 768 / 320
private static let PadIdiomXOffset: CGFloat = ((568 * HeightRatio) - 1024) / 2
static let iPadAspectRatio = HeightRatio
func widthForIdiom() -> CGFloat {
switch UIDevice.currentDevice().userInterfaceIdiom {
case .Pad:
return self * CGFloat.HeightRatio
default:
return self
}
}
func heightForIdiom() -> CGFloat {
switch UIDevice.currentDevice().userInterfaceIdiom {
case .Pad:
return self * CGFloat.HeightRatio
default:
return self
}
}
func xForIdiom() -> CGFloat {
switch UIDevice.currentDevice().userInterfaceIdiom {
case .Pad:
return (self * CGFloat.HeightRatio) - CGFloat.PadIdiomXOffset
default:
return self
}
}
func yForIdiom() -> CGFloat {
switch UIDevice.currentDevice().userInterfaceIdiom {
case .Pad:
return self * CGFloat.HeightRatio
default:
return self
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment