-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSKSpriteNode-Helpers
67 lines (55 loc) · 2.42 KB
/
SKSpriteNode-Helpers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Helpers.swift
// mm2
//
// Created by John McManus on 09/11/2015.
// Copyright © 2015 AppyAppster Limited. All rights reserved.
//
import SpriteKit
extension SKSpriteNode {
func tileNode (tile: SKTexture){
UIGraphicsBeginImageContext(self.size)
let context = UIGraphicsGetCurrentContext()
let tileSize = CGRect(x: 0.0, y: 0.0, width: tile.size().width, height: tile.size().height)
CGContextDrawTiledImage(context, tileSize, tile.CGImage)
var tiledBackground = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
tiledBackground = tiledBackground.imageRotatedByDegrees(180, flip: false)
let finalImage = SKTexture(CGImage: tiledBackground.CGImage!)
self.texture = finalImage
}
}
extension UIImage {
public func imageRotatedByDegrees(degrees: CGFloat, flip: Bool) -> UIImage {
let radiansToDegrees: (CGFloat) -> CGFloat = {
return $0 * (180.0 / CGFloat(M_PI))
}
let degreesToRadians: (CGFloat) -> CGFloat = {
return $0 / 180.0 * CGFloat(M_PI)
}
// calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox = UIView(frame: CGRect(origin: CGPointZero, size: size))
let t = CGAffineTransformMakeRotation(degreesToRadians(degrees));
rotatedViewBox.transform = t
let rotatedSize = rotatedViewBox.frame.size
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let bitmap = UIGraphicsGetCurrentContext()
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0, rotatedSize.height / 2.0);
// // Rotate the image context
CGContextRotateCTM(bitmap, degreesToRadians(degrees));
// Now, draw the rotated/scaled image into the context
var yFlip: CGFloat
if(flip){
yFlip = CGFloat(-1.0)
} else {
yFlip = CGFloat(1.0)
}
CGContextScaleCTM(bitmap, yFlip, -1.0)
CGContextDrawImage(bitmap, CGRectMake(-size.width / 2, -size.height / 2, size.width, size.height), CGImage)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}