Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Parallax Background
// Copyright © 2015 C4
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: The above copyright
// notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//import C4
import UIKit
class ParallaxBackground : CanvasController {
let speeds : [CGFloat] = [0.08,0.0,0.10,0.12,0.15,1.0,0.8,1.0]
var scrollviews : [InfiniteScrollView]!
let signCount : CGFloat = 12.0
var scrollviewOffsetContext = 0
override func setup() {
scrollviews = [InfiniteScrollView]()
for i in 0..<speeds.count {
let layer = createLayer(speeds[i])
canvas.add(layer)
scrollviews.append(layer)
}
if let topLayer = scrollviews.last {
topLayer.addObserver(self, forKeyPath: "contentOffset", options: .New, context: &scrollviewOffsetContext)
}
}
func createLayer(speed: CGFloat) -> InfiniteScrollView {
let frame = view.bounds
let layer = InfiniteScrollView(frame: frame)
var contentSize = CGSizeMake(frame.size.width * 2.0 * signCount * speed, frame.size.height)
contentSize.width += speed < 1.0 ? view.frame.width : 0
layer.contentSize = contentSize
let dx = Double(layer.contentSize.width) / 100.0
let dy = Double(layer.contentSize.height) / Double(speeds.count+1)
var center = Point(dx, Double(scrollviews.count + 1) * dy)
let fontSize = Double(scrollviews.count + 1) * 6.0
let font = Font(name:"AvenirNext-DemiBold", size: fontSize)!
repeat {
let label = TextShape(text: "\(scrollviews.count)", font: font)!
label.center = center
layer.add(label)
center.x += dx
} while center.x < Double(layer.contentSize.width)
return layer
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if context == &scrollviewOffsetContext {
let sv = object as! InfiniteScrollView
let offset = sv.contentOffset
for i in 0..<scrollviews.count-1 {
let layer = scrollviews[i]
layer.contentOffset = CGPointMake(offset.x * speeds[i], 0.0)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment