Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

이런것들을 수정했습니다. #6

Merged
merged 3 commits into from
Oct 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
111 changes: 94 additions & 17 deletions Sources/WaterDropsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,43 @@ open class WaterDropsView: UIView {

fileprivate var isStarted = false

fileprivate var randomDuration : TimeInterval {
return TimeInterval(arc4random_uniform(UInt32(self.maxDuration - self.minDuration))) + self.minDuration
}

fileprivate var randomLength : CGFloat {
return CGFloat(arc4random_uniform(UInt32(self.maxLength - self.minLength))) + self.minLength
}

fileprivate var randomRect : CGRect {

// make random number
let randomX: CGFloat = CGFloat(arc4random_uniform(UInt32(self.frame.width)))
let randomSize: CGFloat = CGFloat(arc4random_uniform(UInt32(self.maxDropSize - self.minDropSize))) + self.minDropSize

// make waterdrop
let positionY = direction == .up ? self.frame.height - randomSize : 0

return CGRect(x: randomX, y: positionY, width: randomSize, height: randomSize)
}

private var waterAnimations = [CAAnimation]()
private var animationGroup : CAAnimationGroup?

public init(frame: CGRect = CGRect.zero, build: waterDropBuildClosure) {
super.init(frame: frame)
addAppCycleObserver()
build(self)
}

public override init(frame: CGRect) {
super.init(frame: frame)
addAppCycleObserver()
}

public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
addAppCycleObserver()
}

open override func awakeFromNib() {
Expand All @@ -71,44 +97,71 @@ open class WaterDropsView: UIView {
public func stopAnimation() {
isStarted = false
self.layer.sublayers?.forEach {
$0.removeAllAnimations()
$0.removeFromSuperlayer()
}
}

fileprivate func makeRandomWaterDrops(num: Int, direction: DropDirection = .up) {
for _ in 1...num {
randomWaterdrop(direction: direction)
@objc open func pauseAnimation() {

layer.sublayers?.forEach {
if let animation = $0.animation(forKey: "animationGroup") {
waterAnimations.append(animation)
$0.pause()
}
}

}

fileprivate func randomRect() -> CGRect {
@objc open func resumeAnimation() {

// make random number
let randomX: CGFloat = CGFloat(arc4random_uniform(UInt32(self.frame.width)))
let randomSize: CGFloat = CGFloat(arc4random_uniform(UInt32(self.maxDropSize - self.minDropSize))) + self.minDropSize
// make waterdrop
let positionY = direction == .up ? self.frame.height : -randomSize
if !waterAnimations.isEmpty {
for i in 0 ..< waterAnimations.count {
layer.sublayers?[i].add(waterAnimations[i], forKey: "animationGroup")
}
waterAnimations.removeAll()
}

return CGRect(x: randomX, y: positionY, width: randomSize, height: randomSize)
layer.sublayers?.forEach {
$0.resume()
}
}


fileprivate func addAppCycleObserver() {

NotificationCenter.default.addObserver(self,
selector: #selector(pauseAnimation),
name: .UIApplicationWillResignActive,
object: nil)

NotificationCenter.default.addObserver(self,
selector: #selector(resumeAnimation),
name: .UIApplicationWillEnterForeground,
object: nil)

}

fileprivate func makeRandomWaterDrops(num: Int, direction: DropDirection = .up) {
for _ in 1...num {
randomWaterdrop(direction: direction)
}
}

fileprivate func randomWaterdrop(direction: DropDirection = .up) {

let waterDropLayer = CAShapeLayer()
let path = UIBezierPath(ovalIn: randomRect())
let path = UIBezierPath(ovalIn: randomRect)

waterDropLayer.path = path.cgPath
waterDropLayer.fillColor = self.color.cgColor
self.layer.addSublayer(waterDropLayer)

startLayerAnimation(layer: waterDropLayer)

}

fileprivate func startLayerAnimation(layer: CAShapeLayer) {

let randomDuration: TimeInterval = TimeInterval(arc4random_uniform(UInt32(self.maxDuration - self.minDuration))) + self.minDuration
let randomLength: CGFloat = CGFloat(arc4random_uniform(UInt32(self.maxLength - self.minLength))) + self.minLength

let length = direction == .up ? -randomLength : randomLength

// animation
Expand All @@ -120,7 +173,7 @@ open class WaterDropsView: UIView {
alphaAnimation.fromValue = layer.opacity
alphaAnimation.toValue = 0.0

let animationGroup = CAAnimationGroup()
animationGroup = CAAnimationGroup()
animationGroup.animations = [dropAnimation, alphaAnimation]
animationGroup.duration = randomDuration
animationGroup.repeatCount = .greatestFiniteMagnitude
Expand All @@ -133,7 +186,7 @@ open class WaterDropsView: UIView {
if isStarted {
self.layer.sublayers?.forEach({
if let waterDropLayer = $0 as? CAShapeLayer {
waterDropLayer.path = UIBezierPath(ovalIn: randomRect()).cgPath
waterDropLayer.path = UIBezierPath(ovalIn: randomRect).cgPath
startLayerAnimation(layer: waterDropLayer)
}
})
Expand All @@ -142,3 +195,27 @@ open class WaterDropsView: UIView {
}
}

extension CALayer {

fileprivate func pause() {

let pausedTime = self.convertTime(CACurrentMediaTime(), from: nil)
self.speed = 0.0
self.timeOffset = pausedTime

}

fileprivate func resume() {

let pausedTime = self.timeOffset
self.speed = 1.0
self.timeOffset = 0.0
self.beginTime = 0.0
let timeSincePause = self.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
self.beginTime = timeSincePause

}

}