Skip to content

Commit

Permalink
Add card alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
liujunliuhong committed Oct 27, 2023
1 parent 1731864 commit 4271933
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
9 changes: 7 additions & 2 deletions DragCardContainer/Sources/BasicInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import QuartzCore
public struct BasicInfo {
public let translation: CGPoint
public let scale: CGFloat
public let alpha: CGFloat
public let rotationAngle: CGFloat

public var transform: CGAffineTransform {
Expand All @@ -62,11 +63,15 @@ public struct BasicInfo {
// return t1.concatenating(t2).concatenating(t3)
}

public init(translation: CGPoint, scale: CGFloat, rotationAngle: CGFloat) {
public init(translation: CGPoint, scale: CGFloat, alpha: CGFloat, rotationAngle: CGFloat) {
self.translation = translation
self.scale = scale
self.alpha = alpha
self.rotationAngle = rotationAngle
}

public static let `default` = BasicInfo(translation: .zero, scale: 1, rotationAngle: 0)
public static let `default` = BasicInfo(translation: .zero,
scale: 1,
alpha: 1.0,
rotationAngle: 0)
}
9 changes: 8 additions & 1 deletion DragCardContainer/Sources/CardEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ extension CardEngine {
func final() {
for model in cardModels {
model.cardView.transform = model.targetBasicInfo.transform
model.cardView.alpha = model.targetBasicInfo.alpha
}
}

Expand Down Expand Up @@ -222,6 +223,7 @@ extension CardEngine {
options: [.curveLinear, .allowUserInteraction]) {
for model in self.cardModels {
model.cardView.transform = model.targetBasicInfo.transform
model.cardView.alpha = model.targetBasicInfo.alpha
}
}
}
Expand Down Expand Up @@ -257,6 +259,7 @@ extension CardEngine {
cardView.layer.anchorPoint = metrics.cardAnchorPoint
cardView.frame = metrics.cardFrame
cardView.transform = .identity
cardView.alpha = 1.0
cardView.delegate = self
cardContainer.containerView.addSubview(cardView)

Expand Down Expand Up @@ -349,6 +352,7 @@ extension CardEngine {
cardView.layer.anchorPoint = metrics.cardAnchorPoint
cardView.frame = metrics.cardFrame
cardView.transform = metrics.minimumBasicInfo.transform
cardView.alpha = metrics.minimumBasicInfo.alpha
cardView.delegate = self
cardView.isUserInteractionEnabled = false
cardContainer.containerView.insertSubview(cardView, at: 0)
Expand Down Expand Up @@ -392,6 +396,7 @@ extension CardEngine {
for model in self.cardModels {
if model != currentResetCardModel {
model.cardView.transform = model.targetBasicInfo.transform
model.cardView.alpha = model.targetBasicInfo.alpha
}
}
} completion: { finished in
Expand Down Expand Up @@ -550,12 +555,13 @@ extension CardEngine: CardDelegate {

for model in cardModels {
let scale = model.currentBasicInfo.scale + (model.targetBasicInfo.scale - model.currentBasicInfo.scale) * percentage
let alpha = model.currentBasicInfo.alpha + (model.targetBasicInfo.alpha - model.currentBasicInfo.alpha) * percentage
let translation = model.currentBasicInfo.translation + (model.targetBasicInfo.translation - model.currentBasicInfo.translation) * percentage
let rotationAngle = model.currentBasicInfo.rotationAngle + (model.targetBasicInfo.rotationAngle - model.currentBasicInfo.rotationAngle) * percentage

let transform = CGAffineTransform(translationX: translation.x, y: translation.y)
model.cardView.transform = transform.rotated(by: rotationAngle).scaledBy(x: scale, y: scale)

model.cardView.alpha = alpha
// let t1 = CGAffineTransform(translationX: translation.x, y: translation.y)
// let t2 = CGAffineTransform(scaleX: scale, y: scale)
// let t3 = CGAffineTransform(rotationAngle: rotationAngle)
Expand Down Expand Up @@ -583,6 +589,7 @@ extension CardEngine: CardDelegate {
options: [.curveLinear, .allowUserInteraction]) {
for model in self.cardModels {
model.cardView.transform = model.targetBasicInfo.transform
model.cardView.alpha = model.targetBasicInfo.alpha
}
}

Expand Down
44 changes: 39 additions & 5 deletions DragCardContainer/Sources/ScaleMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ public final class ScaleMode {
}
}

/// Minimum card alpha.
public var minimumCardAlpha: CGFloat = 0.2 {
didSet {
minimumCardAlpha = max(.zero, min(minimumCardAlpha, 1.0))
}
}

/// Maximum rotaion angle.
public var maximumAngle: CGFloat = 0 {
didSet {
Expand Down Expand Up @@ -142,46 +149,73 @@ extension ScaleMode: Mode {
magnitudeScale = CGFloat(1.0 - minimumScale) / CGFloat(visibleCount - 1)
}

var magnitudeAlpha: CGFloat
if visibleCount <= 1 {
magnitudeAlpha = 1
} else {
magnitudeAlpha = CGFloat(1.0 - minimumCardAlpha) / CGFloat(visibleCount - 1)
}

var basicInfos: [BasicInfo] = []

switch direction {
case .bottom:
for i in 0..<visibleCount {
let radius = maximumAngle.isZero ? 0.0 : CGFloat(CGFloat.random(in: Range(uncheckedBounds: (-maximumAngle, maximumAngle)))).radius
let scale = 1.0 - magnitudeScale * CGFloat(i)
let alpha = 1.0 - magnitudeAlpha * CGFloat(i)
let translation = CGPoint(x: .zero, y: cardSpacing * CGFloat(i))
let basicInfo = BasicInfo(translation: translation, scale: scale, rotationAngle: radius)
let basicInfo = BasicInfo(translation: translation,
scale: scale,
alpha: alpha,
rotationAngle: radius)
basicInfos.append(basicInfo)
}
case .top:
for i in 0..<visibleCount {
let radius = maximumAngle.isZero ? 0.0 : CGFloat(CGFloat.random(in: Range(uncheckedBounds: (-maximumAngle, maximumAngle)))).radius
let scale = 1.0 - magnitudeScale * CGFloat(i)
let alpha = 1.0 - magnitudeAlpha * CGFloat(i)
let translation = CGPoint(x: .zero, y: -cardSpacing * CGFloat(i))
let basicInfo = BasicInfo(translation: translation, scale: scale, rotationAngle: radius)
let basicInfo = BasicInfo(translation: translation,
scale: scale,
alpha: alpha,
rotationAngle: radius)
basicInfos.append(basicInfo)
}
case .left:
for i in 0..<visibleCount {
let radius = maximumAngle.isZero ? 0.0 : CGFloat(CGFloat.random(in: Range(uncheckedBounds: (-maximumAngle, maximumAngle)))).radius
let scale = 1.0 - magnitudeScale * CGFloat(i)
let alpha = 1.0 - magnitudeAlpha * CGFloat(i)
let translation = CGPoint(x: -cardSpacing * CGFloat(i), y: .zero)
let basicInfo = BasicInfo(translation: translation, scale: scale, rotationAngle: radius)
let basicInfo = BasicInfo(translation: translation,
scale: scale,
alpha: alpha,
rotationAngle: radius)
basicInfos.append(basicInfo)
}
case .right:
for i in 0..<visibleCount {
let radius = maximumAngle.isZero ? 0.0 : CGFloat(CGFloat.random(in: Range(uncheckedBounds: (-maximumAngle, maximumAngle)))).radius
let scale = 1.0 - magnitudeScale * CGFloat(i)
let alpha = 1.0 - magnitudeAlpha * CGFloat(i)
let translation = CGPoint(x: cardSpacing * CGFloat(i), y: .zero)
let basicInfo = BasicInfo(translation: translation, scale: scale, rotationAngle: radius)
let basicInfo = BasicInfo(translation: translation,
scale: scale,
alpha: alpha,
rotationAngle: radius)
basicInfos.append(basicInfo)
}
case .center:
for i in 0..<visibleCount {
let radius = maximumAngle.isZero ? 0.0 : CGFloat(CGFloat.random(in: Range(uncheckedBounds: (-maximumAngle, maximumAngle)))).radius
let scale = 1.0 - magnitudeScale * CGFloat(i)
let basicInfo = BasicInfo(translation: .zero, scale: scale, rotationAngle: radius)
let alpha = 1.0 - magnitudeAlpha * CGFloat(i)
let basicInfo = BasicInfo(translation: .zero,
scale: scale,
alpha: alpha,
rotationAngle: radius)
basicInfos.append(basicInfo)
}
}
Expand Down

0 comments on commit 4271933

Please sign in to comment.