Skip to content

Commit

Permalink
[#27] fix : 여러 base, player 동시 이동 시 마지막 것만 반영되ᄂ…
Browse files Browse the repository at this point in the history
…ᅳᆫ 버그 수정
  • Loading branch information
eeeesong committed May 13, 2021
1 parent 0851f4a commit caa4510
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class GamePlayViewController: UIViewController {
@IBOutlet weak var batterInfoView: PitcherInfoView!
@IBOutlet weak var ballCountView: BallCountView!
@IBOutlet weak var groundView: GroundView!


private let delayAmount = 0.2
private var totalDelay = -0.2

enum ViewID {
static let storyboard = "GamePlay"
static let segue = "selectPitcher"
Expand Down Expand Up @@ -137,7 +140,8 @@ extension GamePlayViewController {
.publisher(for: BaseManager.notiName)
.sink { data in
if let movementType = data.userInfo?["movement"] as? BaseMovement {
DispatchQueue.main.async {
self.totalDelay += self.delayAmount
DispatchQueue.main.asyncAfter(deadline: .now() + self.totalDelay) {
switch movementType {
case .homeToFirst:
self.groundView.homeTofirstBase()
Expand All @@ -147,7 +151,10 @@ extension GamePlayViewController {
self.groundView.secondBaseToThirdBase()
case .thirdToHome:
self.groundView.thirdBaseToHome()
case .reset:
self.groundView.reset()
}
self.totalDelay -= self.delayAmount
}
}
}.store(in: &cancelBag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ class GroundView: UIView {
private var homeBaseProperty: BaseDrawingProperty?

private var baseLayers: [Int: CALayer] = [:]
private var playerLayer: PlayerLayer?
private var playerLayer: [PlayerLayer] = []

private let totalTime = 1.5
private let fastDelay = 0.0
private let lateDelay = 0.5

func configure() {
self.backgroundColor = UIColor(cgColor: Color.groundGreen)
Expand Down Expand Up @@ -140,14 +144,18 @@ class GroundView: UIView {
guard let firstBaseProperty = self.firstBaseProperty, let secondBaseProperty = self.secondBaseProperty,
let thirdBaseProperty = self.thirdBaseProperty, let homeBaseProperty = self.homeBaseProperty else { return }

let baseProperties = [firstBaseProperty, secondBaseProperty, thirdBaseProperty]
var baseProperties = [firstBaseProperty, secondBaseProperty, thirdBaseProperty]

baseProperties.forEach { baseProperty in
self.addBase(id: baseProperty.id, origin: baseProperty.origin, size: baseProperty.size)
}

addPlayer(to: homeBaseProperty)

baseProperties.append(homeBaseProperty)

baseProperties.forEach { baseProperty in
addPlayer(to: baseProperty)
}

}

private func addBase(id: Int, origin: CGPoint, size: CGSize) {
Expand Down Expand Up @@ -177,7 +185,7 @@ class GroundView: UIView {

layer.addSublayer(player)

self.playerLayer = player
self.playerLayer.append(player)

}

Expand All @@ -186,14 +194,25 @@ class GroundView: UIView {
//MARK: - Animation Methods
extension GroundView {

func reset() {
playerLayer.forEach { playerLayer in
playerLayer.opacity = 0
}
baseLayers.forEach { (_, baseLayer) in
baseLayer.backgroundColor = Color.base
}
}

func homeTofirstBase() {
guard let homeBaseProperty = homeBaseProperty, let firstBaseProperty = firstBaseProperty else { return }

playerLayer[0].opacity = 0

CATransaction.begin()
CATransaction.setAnimationDuration(6)
CATransaction.setAnimationDuration(totalTime)

animatePlayer(with: homeBaseProperty, firstBaseProperty, duration: 1, delay: 3)
animateBase(id: 1, toSelected: true, duration: 1, delay: 5)
animatePlayer(with: homeBaseProperty, firstBaseProperty, duration: 1, delay: fastDelay)
animateBase(id: 1, toSelected: true, duration: 1, delay: lateDelay)

CATransaction.commit()

Expand All @@ -202,12 +221,14 @@ extension GroundView {
func firstBaseToSecondBase() {
guard let firstBaseProperty = firstBaseProperty, let secondBaseProperty = secondBaseProperty else { return }

playerLayer[1].opacity = 0

CATransaction.begin()
CATransaction.setAnimationDuration(6)
CATransaction.setAnimationDuration(totalTime)

animatePlayer(with: firstBaseProperty, secondBaseProperty, duration: 1, delay: 3)
animateBase(id: 1, toSelected: false, duration: 1, delay: 3)
animateBase(id: 2, toSelected: true, duration: 1, delay: 5)
animatePlayer(with: firstBaseProperty, secondBaseProperty, duration: 1, delay: fastDelay)
animateBase(id: 1, toSelected: false, duration: 1, delay: fastDelay)
animateBase(id: 2, toSelected: true, duration: 1, delay: lateDelay)

CATransaction.commit()

Expand All @@ -216,48 +237,71 @@ extension GroundView {
func secondBaseToThirdBase() {
guard let secondBaseProperty = secondBaseProperty, let thirdBaseProperty = thirdBaseProperty else { return }

playerLayer[2].opacity = 0

CATransaction.begin()
CATransaction.setAnimationDuration(6)
CATransaction.setAnimationDuration(totalTime)

animatePlayer(with: secondBaseProperty, thirdBaseProperty, duration: 1, delay: 3)
animateBase(id: 2, toSelected: false, duration: 1, delay: 3)
animateBase(id: 3, toSelected: true, duration: 1, delay: 5)
animatePlayer(with: secondBaseProperty, thirdBaseProperty, duration: 1, delay: fastDelay)
animateBase(id: 2, toSelected: false, duration: 1, delay: fastDelay)
animateBase(id: 3, toSelected: true, duration: 1, delay: lateDelay)

CATransaction.commit()
}

func thirdBaseToHome() {
guard let thirdBaseProperty = thirdBaseProperty, let homeBaseProperty = homeBaseProperty else { return }

playerLayer[3].opacity = 0

CATransaction.begin()
CATransaction.setAnimationDuration(6)
CATransaction.setAnimationDuration(totalTime)

animatePlayer(with: thirdBaseProperty, homeBaseProperty, duration: 1, delay: 3)
animateBase(id: 3, toSelected: false, duration: 1, delay: 3)
animatePlayer(with: thirdBaseProperty, homeBaseProperty, duration: 1, delay: fastDelay)
animateBase(id: 3, toSelected: false, duration: 1, delay: fastDelay)

CATransaction.commit()
}

private func animatePlayer(with fromBaseInfo: BaseDrawingProperty,_ toBaseInfo: BaseDrawingProperty, duration: Double, delay: Double) {

guard let player = playerLayer else { return }
let targetIndex = toBaseInfo.id - 1

guard playerLayer.count >= targetIndex else { return }

let player = playerLayer[targetIndex]
player.opacity = 100

let offsetY = player.frame.height / 2
let fromPosition = fromBaseInfo.centerMovedBy(x: 0, y: offsetY)
let toPosition = toBaseInfo.centerMovedBy(x: 0, y: offsetY)

let positionAnimation = CASpringAnimation(keyPath: #keyPath(PlayerLayer.position))
positionAnimation.damping = 10
positionAnimation.mass = 0.7
positionAnimation.fromValue = fromPosition
positionAnimation.toValue = toPosition
positionAnimation.timeOffset = delay
positionAnimation.duration = duration

player.add(positionAnimation, forKey: #keyPath(PlayerLayer.position))

let opacityAnimation = CABasicAnimation(keyPath: #keyPath(PlayerLayer.opacity))
opacityAnimation.fromValue = 0
opacityAnimation.byValue = 100
opacityAnimation.timeOffset = delay
opacityAnimation.duration = duration * 0.1
player.add(opacityAnimation, forKey: #keyPath(PlayerLayer.opacity))

let opacityAnimation2 = CABasicAnimation(keyPath: #keyPath(PlayerLayer.opacity))
opacityAnimation2.fromValue = 100
opacityAnimation2.byValue = 0
opacityAnimation2.timeOffset = delay + (duration * 0.9)
opacityAnimation2.duration = duration * 0.1
player.add(opacityAnimation2, forKey: #keyPath(PlayerLayer.opacity))

CATransaction.setCompletionBlock {
player.position = toPosition
if targetIndex == 3 { player.opacity = 0 }
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PlayerLayer: CALayer {
func configure() {
addBody()
addFace()
self.opacity = 0
}

private func addBody() {
Expand Down

0 comments on commit caa4510

Please sign in to comment.