Update background

This commit is contained in:
Ali 2021-04-30 12:37:13 +04:00
parent 08f90a322c
commit e06aa34c33

View File

@ -47,7 +47,24 @@ private func generateGradientComponent(size: CGSize, color: UIColor) -> UIImage?
} }
public final class GradientBackgroundNode: ASDisplayNode { public final class GradientBackgroundNode: ASDisplayNode {
private var pointImages: [UIImageView] = [] private final class PointImage {
let stack: [UIImageView]
init(image: UIImage, count: Int) {
self.stack = (0 ..< count).map { _ in
let imageView = UIImageView(image: image)
imageView.alpha = min(1.0, (1.0 / CGFloat(count)) * 1.2)
return imageView
}
}
func updateFrame(frame: CGRect, transition: ContainedViewLayoutTransition) {
for imageView in stack {
transition.updateFrame(view: imageView, frame: frame)
}
}
}
private var pointImages: [PointImage] = []
private let dimView: UIView private let dimView: UIView
private var phase: Int = 0 private var phase: Int = 0
@ -72,13 +89,22 @@ public final class GradientBackgroundNode: ASDisplayNode {
UIColor(rgb: 0xFBE37D) UIColor(rgb: 0xFBE37D)
] ]
let layerCount = 2
for i in 0 ..< colors.count { for i in 0 ..< colors.count {
let pointImage = UIImageView(image: generateGradientComponent(size: CGSize(width: 300.0, height: 300.0), color: colors[i].withMultiplied(hue: 1.0, saturation: 1.1, brightness: 1.1))) let image = generateGradientComponent(size: CGSize(width: 300.0, height: 300.0), color: colors[i].withMultiplied(hue: 1.0, saturation: 1.1, brightness: 1.0))!
//pointImage.layer.compositingFilter = "multiplyBlendMode"
self.view.addSubview(pointImage) let pointImage = PointImage(image: image, count: layerCount)
self.pointImages.append(pointImage) self.pointImages.append(pointImage)
} }
for i in 0 ..< layerCount {
for pointImage in self.pointImages {
self.view.addSubview(pointImage.stack[i])
}
}
self.view.addSubview(self.dimView) self.view.addSubview(self.dimView)
} }
@ -90,37 +116,44 @@ public final class GradientBackgroundNode: ASDisplayNode {
let positions: [CGPoint] let positions: [CGPoint]
let basePositions: [CGPoint] = [ let basePositions: [CGPoint] = [
CGPoint(x: 0.2, y: 0.2), CGPoint(x: 0.80, y: 0.10),
CGPoint(x: 0.2, y: 0.8), CGPoint(x: 0.60, y: 0.20),
CGPoint(x: 0.8, y: 0.8), CGPoint(x: 0.35, y: 0.25),
CGPoint(x: 0.8, y: 0.2), CGPoint(x: 0.25, y: 0.60),
] CGPoint(x: 0.20, y: 0.90),
CGPoint(x: 0.40, y: 0.80),
switch self.phase % 4 { CGPoint(x: 0.65, y: 0.75),
case 0: CGPoint(x: 0.75, y: 0.40)
positions = basePositions ]/*.map { point -> CGPoint in
case 1: var point = point
positions = shiftArray(array: basePositions, offset: 1) if point.x < 0.5 {
case 2: point.x *= 0.5
positions = shiftArray(array: basePositions, offset: 2) } else {
case 3: point.x = 1.0 - (1.0 - point.x) * 0.5
positions = shiftArray(array: basePositions, offset: 3)
default:
preconditionFailure()
} }
if point.y < 0.5 {
point.y *= 0.5
} else {
point.y = 1.0 - (1.0 - point.x) * 0.5
}
return point
}*/
for i in 0 ..< positions.count { positions = shiftArray(array: basePositions, offset: self.phase % 8)
for i in 0 ..< positions.count / 2 {
if self.pointImages.count <= i { if self.pointImages.count <= i {
break break
} }
let pointCenter = CGPoint(x: size.width * positions[i].x, y: size.height * positions[i].y) let position = positions[i * 2]
let pointSize = CGSize(width: size.width * 2.0, height: size.height * 2.0) let pointCenter = CGPoint(x: size.width * position.x, y: size.height * position.y)
transition.updateFrame(view: self.pointImages[i], frame: CGRect(origin: CGPoint(x: pointCenter.x - pointSize.width / 2.0, y: pointCenter.y - pointSize.height / 2.0), size: pointSize)) let pointSize = CGSize(width: size.width * 1.8, height: size.height * 1.5)
self.pointImages[i].updateFrame(frame: CGRect(origin: CGPoint(x: pointCenter.x - pointSize.width / 2.0, y: pointCenter.y - pointSize.height / 2.0), size: pointSize), transition: transition)
} }
} }
public func animateEvent(transition: ContainedViewLayoutTransition) { public func animateEvent(transition: ContainedViewLayoutTransition) {
self.phase = (self.phase + 1) % 4 self.phase = self.phase + 1
if let size = self.validLayout { if let size = self.validLayout {
self.updateLayout(size: size, transition: transition) self.updateLayout(size: size, transition: transition)
} }