Skip to content

Instantly share code, notes, and snippets.

@julianjames
Last active November 27, 2016 23:45
// Playground - noun: a place where people can play
import UIKit
for i in 1...5 {
for var value = 0.0; value < M_PI * 2; value += 0.1 {
var y = sin(value)
}
}
class GradientView : UIView {
override func drawRect(rect: CGRect) {
let context : CGContextRef = UIGraphicsGetCurrentContext()
let dotBlue = UIColor(red: 39.0/255.0, green: 119.0/255.0, blue: 174.0/255.0, alpha:1.0 ).CGColor
let lineBlue = UIColor(red: 35.0/255.0, green: 80.0/255.0, blue: 145.0/255.0, alpha:0.2 ).CGColor
let startBlue = UIColor(red: 27.0/255.0, green: 94.0/255.0, blue: 125.0/255.0, alpha:1.0 ).CGColor
let endBlue = UIColor(red: 44.0/255.0, green: 60.0/255.0, blue: 110.0/255.0, alpha:1.0 ).CGColor
let locations: [CGFloat] = [0.0, 0.75]
let colors = [startBlue, endBlue]
let colorSpace = CGColorSpaceCreateDeviceRGB()
let gradient : CGGradientRef = CGGradientCreateWithColors(colorSpace, colors, locations)
let startPoint : CGPoint = CGPointMake(0.0, 0.0)
let endPoint : CGPoint = CGPointMake(rect.size.width,rect.size.height)
CGContextDrawLinearGradient(context, gradient,startPoint, endPoint, 0)
let diam : CGFloat = 6.0
let radius : CGFloat = diam / 2.0
let step : CGFloat = 60.0
let startX : CGFloat = (rect.size.width % step) * 0.5
let startY : CGFloat = (rect.size.height % step) * 0.5
CGContextSetStrokeColorWithColor(context, lineBlue)
CGContextSetLineWidth(context, 2.0)
for var i = startX; i < rect.size.width; i += 2.0 * step {
CGContextMoveToPoint(context, i, 0.0)
CGContextAddLineToPoint(context, i, rect.size.height)
CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 4.0, lineBlue)
}
for var j = startX; j < rect.size.width; j += 2.0 * step {
CGContextMoveToPoint(context, 0.0, j)
CGContextAddLineToPoint(context, rect.size.width, j)
CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 4.0, lineBlue)
}
CGContextStrokePath(context)
for var i = startX; i < rect.size.width; i += step {
for var j = startY; j < rect.size.height; j += step {
let point = CGPointMake(rect.origin.x + i, rect.origin.y + j)
CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 4.0, dotBlue)
CGContextSetFillColorWithColor(context, dotBlue)
let circle = CGRectMake(point.x - radius, point.y-radius, diam, diam)
CGContextAddEllipseInRect(context, circle)
}
}
CGContextFillPath(context)
}
}
var baseView = GradientView (frame:CGRectMake(0.0, 0.0, 1024.0, 768.0))
@progress44
Copy link

For anyone that might need this in Swift 3


    override func draw(_ rect: CGRect) {
        let context : CGContext = UIGraphicsGetCurrentContext()!

        let dotBlue = UIColor(red: 39.0/255.0, green: 119.0/255.0, blue: 174.0/255.0, alpha:1.0 ).cgColor
        let lineBlue = UIColor(red: 35.0/255.0, green: 80.0/255.0, blue: 145.0/255.0, alpha:0.2 ).cgColor
        let startBlue = UIColor(red: 27.0/255.0, green: 94.0/255.0, blue: 125.0/255.0, alpha:1.0 ).cgColor
        let endBlue = UIColor(red: 44.0/255.0, green: 60.0/255.0, blue: 110.0/255.0, alpha:1.0 ).cgColor

        let locations: [CGFloat] = [0.0, 0.75]

        let colors = [startBlue, endBlue]

        let colorSpace = CGColorSpaceCreateDeviceRGB()

        let gradient : CGGradient = CGGradient(colorsSpace: colorSpace, colors: colors as CFArray, locations: locations)!

        let startPoint : CGPoint = CGPoint(x: 0.0, y: 0.0)
        let endPoint : CGPoint = CGPoint(x: rect.size.width, y: rect.size.height)

        context.drawLinearGradient(gradient,start: startPoint, end: endPoint, options: CGGradientDrawingOptions(rawValue: UInt32(0)))

        let diam : CGFloat = 6.0
        let radius : CGFloat = diam / 2.0
        let step : CGFloat = 60.0
        let startX : CGFloat = rect.size.width.truncatingRemainder(dividingBy: step) * 0.5
        let startY : CGFloat = rect.size.height.truncatingRemainder(dividingBy: step) * 0.5

        context.setStrokeColor(lineBlue)
        context.setLineWidth(2.0)

        let forEnd = rect.size.width
        let forStep = 2.0 * step
        var i = 0.0 as CGFloat

        while i < forEnd {
            context.move(to: CGPoint(x: i, y: 0.0))
            context.addLine(to: CGPoint(x: i, y: rect.size.height))
            context.setShadow(offset: CGSize(width: 0.0, height: 0.0) , blur: 4.0, color: lineBlue)
            i += forStep
        }

        var j = startX
        while j < rect.size.width {
            context.move(to: CGPoint(x: 0.0, y: j))
            context.addLine(to: CGPoint(x: rect.size.width, y: i))
            context.setShadow(offset: CGSize(width: 0.0, height: 0.0) , blur: 4.0, color: lineBlue)

            j += forStep
        }

        context.strokePath()
        i = startX

        while i < rect.size.width {

            j = startY
            while j < rect.size.height {

                let point = CGPoint(x: rect.origin.x + i, y: rect.origin.y + j)

                context.setShadow(offset: CGSize(width: 0.0, height: 0.0), blur: 4.0, color: dotBlue)
                context.setFillColor(dotBlue)

                let circle = CGRect(x: point.x - radius, y: point.y-radius, width: diam, height: diam)
                context.addEllipse(in: circle)

                j += step
            }

            i += step
        }

        context.fillPath()

    }

@vilmosk
Copy link

vilmosk commented Nov 27, 2016

@progress44: Oh man, I didn't check the comments!

Here is my swift3 version too!

// Playground - noun: a place where people can play

import AppKit

for i in 1...5 {
    for var value in stride(from: 0.0, to: 3.14*2, by: 0.1){
        var y = sin(value)
    }
}

class GradientView : NSView {
    
    override func draw(_ rect: CGRect) {
        
        let context : CGContext = (NSGraphicsContext.current()?.cgContext)!
        
        let dotBlue = NSColor(red: 39.0/255.0, green: 119.0/255.0, blue: 174.0/255.0, alpha:1.0 ).cgColor
        let lineBlue = NSColor(red: 35.0/255.0, green: 80.0/255.0, blue: 145.0/255.0, alpha:0.2 ).cgColor
        let startBlue = NSColor(red: 27.0/255.0, green: 94.0/255.0, blue: 125.0/255.0, alpha:1.0 ).cgColor
        let endBlue = NSColor(red: 44.0/255.0, green: 60.0/255.0, blue: 110.0/255.0, alpha:1.0 ).cgColor
        
        let locations: [CGFloat] = [0.0, 0.75]
        
        let colors = [startBlue, endBlue]
        
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        
        let gradient : CGGradient = CGGradient(colorsSpace: colorSpace, colors: colors as CFArray, locations: locations)!
        
        let startPoint : CGPoint = CGPoint(x:0.0, y:0.0)
        let endPoint : CGPoint = CGPoint(x:rect.size.width,y:rect.size.height)
        
        context.drawLinearGradient(gradient,start: startPoint, end: endPoint, options: CGGradientDrawingOptions(rawValue: 0))
        
        let diam : CGFloat = 6.0
        let radius : CGFloat = diam / 2.0
        let step : CGFloat = 60.0
        let startX : CGFloat = (rect.size.width.truncatingRemainder(dividingBy: step) ) * 0.5
        let startY : CGFloat = (rect.size.height.truncatingRemainder(dividingBy: step) ) * 0.5
        
        context.setStrokeColor(lineBlue)
        context.setLineWidth(2.0)
        
        for var i in stride(from: startX, to: rect.size.width, by: 2.0 * step) {
            context.move(to: CGPoint(x:i, y: 0.0))
            context.move(to: CGPoint(x:i, y:rect.size.height))
            context.setShadow(offset: CGSize(width:0, height:0), blur: 4.0, color: lineBlue)
        }
        
        for var j in stride(from: startX, to: rect.size.width, by: 2.0 * step) {
            context.move(to: CGPoint(x:0.0, y: j))
            context.move(to: CGPoint(x:rect.size.width, y:j))
            context.setShadow(offset: CGSize(width:0, height:0), blur: 4.0, color: lineBlue)
        }
        
        
        context.strokePath()
        for var i in stride(from: startX, to: rect.size.width, by: step) {
            for var j in stride(from: startY, to: rect.size.height, by: step) {
                let point = CGPoint(x:rect.origin.x + i, y:rect.origin.y + j)
                
                context.setShadow(offset: CGSize(width:0, height:0), blur: 4.0, color: dotBlue)
                
                context.setFillColor(dotBlue)
                
                let circle = CGRect(x:point.x - radius, y:point.y-radius, width:diam, height:diam)
                
                context.addEllipse(in: circle)
            }
            
        }
        
        context.fillPath()
    }
    
}

var baseView = GradientView (frame:CGRect(x:0.0, y:0.0, width:1024.0, height:768.0))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment