스파르타 코딩 클럽 ☝🏻 - 개발일지 2

DaY·2021년 7월 2일
1

스파르타코딩클럽

목록 보기
5/37
post-thumbnail

과제 Github

화면 구성

SegmentedControl - poker 이미지 삽입
Button - 타이머 On / Off 버튼
Label - 경과 시간

Code

타이머 리셋

func resetTimer() {
    timer?.invalidate()
    timer = nil
    timerButton.setTitle("Start", for: .normal)
}

경과 시간 라벨

  • 분 = 남은 시간을 60으로 나눈 몫
  • 초 = 남은 시간을 60으로 나눈 나머지
  • 남은 시간이 10 미만일 때, 홀수 초의 화면 색 변경
  • 라벨에 애니메이션 효과 삽입
func setUpTimeLabel() {
    var minutes = self.timeRange / 60
    var seconds = self.timeRange % 60

    if self.timeRange < 10 {
        if timeRange % 2 != 0 {
            self.view.backgroundColor = UIColor.orange
        } else {
            self.view.backgroundColor = UIColor.white
        }
    }

    UIView.transition(with: self.timeLabel, duration: 0.3, options: .transitionFlipFromTop) {
        if self.timeRange > 0 {
            self.timeLabel.text = String(format: "%02d : %02d", minutes, seconds)
        } else {
            self.timeLabel.text = "DONE"
        }
    } completion: { (animated) in
    }
}

SegmentedControl

컨트롤의 인덱스마다 시간 범위 변경

func setUpTimeRange() {
    var seg = segmentControl.selectedSegmentIndex

    if seg == 0 {
        self.timeRange = 180
    } else if seg == 1 {
        self.timeRange = 240
    } else {
        self.timeRange = 600
    }

    self.setUpTimeLabel()
}

컨트롤의 값(인덱스 값)이 변할때마다 타이머 리셋 및 시간 범위 변경

@IBAction func segmentValueChanged(_ sender: Any) {
    self.view.backgroundColor = UIColor.white
    self.resetTimer()
    self.setUpTimeRange()
}

타이머 On / Off 버튼

타이머 버튼이 눌리면 버튼의 타이틀 변경 및 라벨 업데이트

@IBAction func touchUpTimerButton(_ sender: Any) {
    if timer != nil {
        resetTimer()
        return
    }

    self.timerButton.setTitle("Quit", for: .normal)
    self.setUpTimeRange()
    self.view.backgroundColor = UIColor.white

    timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { t in
        self.timeRange -= 1
        self.setUpTimeLabel()

        if self.timeRange == 0 {
            self.resetTimer()
        }
    }
}

결과

소감

기초반이라 그런지 친절하게 과제까지 어떻게 해야하는지 힌트를 주신다. 함께 구축하는 기본 틀과 과제의 힌트까지 차근차근 생각하며 수행하면 과제에 대한 부담보단 즐거움을 느낄 수 있는 것 같다.

0개의 댓글