Skip to content

Instantly share code, notes, and snippets.

@stulevine
Created February 25, 2017 13:11
Show Gist options
  • Save stulevine/3d0a360b7059b1faee1d4a534de81389 to your computer and use it in GitHub Desktop.
Save stulevine/3d0a360b7059b1faee1d4a534de81389 to your computer and use it in GitHub Desktop.
Swift 3 - Xcode Playground Code
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
var str = "Hello, playground"
extension UIImage {
class func circle(diameter: CGFloat, color: UIColor) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(CGSize(width: diameter, height: diameter), false, 0)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
context.saveGState()
let rect = CGRect(x: 0, y: 0, width: diameter, height: diameter)
context.setFillColor(color.cgColor)
context.fillEllipse(in: rect)
context.restoreGState()
let image = UIGraphicsGetImageFromCurrentImageContext()
return image
}
class func xImage(width: CGFloat, color: UIColor, weight: CGFloat = 3.0) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: width), false, 0)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
context.saveGState()
context.setStrokeColor(color.cgColor)
context.setLineCap(CGLineCap.round)
context.setLineWidth(weight)
let adjustedX: CGFloat = 0.0+weight
let adjustedY: CGFloat = width-weight
context.move(to: CGPoint(x: adjustedX, y: adjustedX))
context.addLine(to: CGPoint(x: adjustedY, y: adjustedY))
context.move(to: CGPoint(x: adjustedX, y: adjustedY))
context.addLine(to: CGPoint(x: adjustedY, y: adjustedX))
context.strokePath()
context.restoreGState()
let image = UIGraphicsGetImageFromCurrentImageContext()
return image
}
}
let view = UIView(frame: CGRect(x: 50, y: 50, width: 300, height: 600))
view.backgroundColor = UIColor.white
let xSize: CGFloat = 75
let xImage = UIImage.xImage(width: xSize, color: UIColor.blue, weight: 10)
let circle = UIImage.circle(diameter: 30, color: UIColor.red)
let imageView = UIImageView(image: xImage)
imageView.contentMode = .center
let circleImageView = UIImageView(image: circle)
circleImageView.contentMode = .center
view.addSubview(imageView)
view.addSubview(circleImageView)
UIView.animate(withDuration: 2.0, delay: 2.0, options: .curveEaseInOut, animations: {
imageView.frame.origin = CGPoint(x: 100, y: 300)
circleImageView.frame.origin = CGPoint(x: 50, y: 400)
}, completion: nil)
PlaygroundPage.current.liveView = view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment