Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // RippleView.swift
- // Salt
- //
- // Created by EDI_MBP on 15.01.16.
- // Copyright © 2016 EDO. All rights reserved.
- //
- import UIKit
- import CoreGraphics
- class RippleView: UIView {
- var buffer1 = [Int]()
- var buffer2 = [Int]()
- var image = UIImage()
- let force = 512
- let damp = 0.99
- let cols = 256
- let rows = 256
- var newPoint = CGPoint()
- var hasNew = true
- override init(frame: CGRect) {
- super.init(frame: frame)
- addBehaviour()
- }
- convenience init() {
- self.init(frame:CGRect.zero)
- }
- required init(coder aDecoder: NSCoder) {
- fatalError("This class does not support NSCoding")
- }
- func addBehaviour() {
- backgroundColor = UIColor.yellowColor()
- print("Size of Int: \(sizeof(Int))")
- buffer1 = [Int](count: cols*rows*sizeof(Int), repeatedValue: 0)
- buffer2 = [Int](count: cols*rows*sizeof(Int), repeatedValue: 0)
- newPoint = CGPoint(x: 50, y: 50)
- print("Size of buffer1: \(buffer1.count)")
- let provider = CGDataProviderCreateWithData(nil, buffer2, buffer2.count, nil)
- let imageRef = CGImageCreate(cols, rows, 8, 8, rows, CGColorSpaceCreateDeviceGray(), CGBitmapInfo.ByteOrderDefault, provider, nil, false, CGColorRenderingIntent.RenderingIntentDefault)
- image = UIImage.init(CGImage: imageRef!)
- NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: "bang", userInfo: nil, repeats: true)
- }
- func bang() {
- self.setNeedsDisplay()
- }
- func swapBuffers() {
- let swap = buffer1
- buffer1 = buffer2
- buffer2 = swap
- }
- func positionX(x: NSInteger, y:NSInteger) -> NSInteger {
- return ((y * cols) + x)
- }
- override func drawRect(rect: CGRect) {
- if(hasNew) {
- let x = NSInteger(newPoint.x)
- let y = NSInteger(newPoint.y)
- //print("Has new Point: (\(x), \(y))")
- let blip = force
- buffer1[positionX(x, y: y)] = blip
- buffer1[positionX(x-1, y: y)] = blip
- buffer1[positionX(x+1, y: y)] = blip
- buffer1[positionX(x-1, y: y-1)] = blip
- buffer1[positionX(x, y: y-1)] = blip
- buffer1[positionX(x+1, y: y-1)] = blip
- buffer1[positionX(x-1, y: y+1)] = blip
- buffer1[positionX(x, y: y+1)] = blip
- buffer1[positionX(x+1, y: y+1)] = blip
- hasNew = false
- }
- // propagate waves
- processRipples(buffer1, dest:&buffer2)
- // apply waves to image
- applyBuffer()
- // draw image
- image.drawAtPoint(CGPointMake(0.0, 0.0))
- // swap buffers
- swapBuffers()
- }
- func applyBuffer() {
- }
- func processRipples(source:[Int], inout dest: [Int]) {
- var position = 0
- for y in 1...(rows - 1) {
- for x in 1...(cols - 1) {
- position = (y * cols) + x
- dest[position] = (((source[position - 1] + source[position + 1] + source[position - cols] + source[position + cols]) >> 1) - dest[position])
- dest[position] = Int(Double(dest[position]) * damp)
- }
- }
- }
- override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
- let point = (touches.first! as UITouch).locationInView(self)
- if(point.x >= 1 && point.x < 255 && point.y >= 1 && point.y < 255) {
- hasNew = true
- newPoint = point
- newPoint.y = CGFloat(rows) - newPoint.y
- }
- }
- override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
- let point = (touches.first! as UITouch).locationInView(self)
- if(point.x >= 1 && point.x < 255 && point.y >= 1 && point.y < 255) {
- hasNew = true
- newPoint = point
- newPoint.y = CGFloat(rows) - newPoint.y
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement