Advertisement
Guest User

RippleView.swift

a guest
Jan 16th, 2016
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.66 KB | None | 0 0
  1. //
  2. //  RippleView.swift
  3. //  Salt
  4. //
  5. //  Created by EDI_MBP on 15.01.16.
  6. //  Copyright © 2016 EDO. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import CoreGraphics
  11.  
  12. class RippleView: UIView {
  13.  
  14.     var buffer1 = [Int]()
  15.     var buffer2 = [Int]()
  16.    
  17.     var image = UIImage()
  18.    
  19.     let force = 512
  20.     let damp = 0.99
  21.    
  22.     let cols = 256
  23.     let rows = 256
  24.    
  25.     var newPoint = CGPoint()
  26.    
  27.     var hasNew = true
  28.    
  29.    
  30.     override init(frame: CGRect) {
  31.         super.init(frame: frame)
  32.         addBehaviour()
  33.     }
  34.    
  35.     convenience init() {
  36.         self.init(frame:CGRect.zero)
  37.     }
  38.    
  39.     required init(coder aDecoder: NSCoder) {
  40.         fatalError("This class does not support NSCoding")
  41.     }
  42.    
  43.     func addBehaviour() {
  44.        
  45.         backgroundColor = UIColor.yellowColor()
  46.        
  47.         print("Size of Int: \(sizeof(Int))")
  48.        
  49.         buffer1 = [Int](count: cols*rows*sizeof(Int), repeatedValue: 0)
  50.         buffer2 = [Int](count: cols*rows*sizeof(Int), repeatedValue: 0)
  51.        
  52.         newPoint = CGPoint(x: 50, y: 50)
  53.        
  54.         print("Size of buffer1: \(buffer1.count)")
  55.        
  56.         let provider = CGDataProviderCreateWithData(nil, buffer2, buffer2.count, nil)
  57.        
  58.         let imageRef = CGImageCreate(cols, rows, 8, 8, rows, CGColorSpaceCreateDeviceGray(), CGBitmapInfo.ByteOrderDefault, provider, nil, false, CGColorRenderingIntent.RenderingIntentDefault)
  59.        
  60.         image = UIImage.init(CGImage: imageRef!)
  61.        
  62.         NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: "bang", userInfo: nil, repeats: true)
  63.        
  64.     }
  65.    
  66.     func bang() {
  67.        
  68.         self.setNeedsDisplay()
  69.        
  70.     }
  71.    
  72.     func swapBuffers() {
  73.        
  74.         let swap = buffer1
  75.         buffer1 = buffer2
  76.         buffer2 = swap
  77.        
  78.     }
  79.    
  80.     func positionX(x: NSInteger, y:NSInteger) -> NSInteger {
  81.        
  82.         return ((y * cols) + x)
  83.        
  84.     }
  85.    
  86.     override func drawRect(rect: CGRect) {
  87.        
  88.         if(hasNew) {
  89.            
  90.             let x = NSInteger(newPoint.x)
  91.             let y = NSInteger(newPoint.y)
  92.            
  93.             //print("Has new Point: (\(x), \(y))")
  94.            
  95.             let blip = force
  96.            
  97.             buffer1[positionX(x, y: y)] = blip
  98.             buffer1[positionX(x-1, y: y)] = blip
  99.             buffer1[positionX(x+1, y: y)] = blip
  100.            
  101.             buffer1[positionX(x-1, y: y-1)] = blip
  102.             buffer1[positionX(x, y: y-1)] = blip
  103.             buffer1[positionX(x+1, y: y-1)] = blip
  104.            
  105.             buffer1[positionX(x-1, y: y+1)] = blip
  106.             buffer1[positionX(x, y: y+1)] = blip
  107.             buffer1[positionX(x+1, y: y+1)] = blip
  108.            
  109.             hasNew = false
  110.            
  111.         }
  112.        
  113.         // propagate waves
  114.         processRipples(buffer1, dest:&buffer2)
  115.        
  116.         // apply waves to image
  117.         applyBuffer()
  118.        
  119.         // draw image
  120.         image.drawAtPoint(CGPointMake(0.0, 0.0))
  121.        
  122.         // swap buffers
  123.         swapBuffers()
  124.        
  125.     }
  126.    
  127.     func applyBuffer() {
  128.        
  129.        
  130.        
  131.     }
  132.    
  133.     func processRipples(source:[Int], inout dest: [Int]) {
  134.        
  135.         var position = 0
  136.        
  137.         for y in 1...(rows - 1) {
  138.            
  139.             for x in 1...(cols - 1) {
  140.                
  141.                 position = (y * cols) + x
  142.                
  143.                 dest[position] = (((source[position - 1] + source[position + 1] + source[position - cols] + source[position + cols]) >> 1) - dest[position])
  144.                
  145.                 dest[position] = Int(Double(dest[position]) * damp)
  146.                
  147.             }
  148.            
  149.         }
  150.        
  151.     }
  152.    
  153.     override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
  154.        
  155.         let point = (touches.first! as UITouch).locationInView(self)
  156.        
  157.         if(point.x >= 1 && point.x < 255 && point.y >= 1 && point.y < 255) {
  158.            
  159.             hasNew = true
  160.            
  161.             newPoint = point
  162.            
  163.             newPoint.y = CGFloat(rows) - newPoint.y
  164.            
  165.         }
  166.        
  167.     }
  168.    
  169.     override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
  170.        
  171.         let point = (touches.first! as UITouch).locationInView(self)
  172.        
  173.         if(point.x >= 1 && point.x < 255 && point.y >= 1 && point.y < 255) {
  174.            
  175.             hasNew = true
  176.            
  177.             newPoint = point
  178.            
  179.             newPoint.y = CGFloat(rows) - newPoint.y
  180.            
  181.         }
  182.        
  183.     }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement