3장. 원하는 이미지 화면에 출력(사이즈 변경)

4장. 데이트 피커 사용해 날짜 선택하기(날짜 포맷, 타이머 개발)

Do it 스위프트

# 원하는 이미지 화면에 출력(사이즈 변경)

이미지를 선언하고, 변수에 할당하는 것은 다른 언어와 비슷합니다. 다만, 이미지 사이즈를 변경할 때 변수타입이 CGFloat타입임을 알아야 하며, 크기조절할 때 ImageView객체.frame.size의 값을 CGSize(width:,height:)로 재설정한다는 것을 기억하면 됩니다. 

var isZoom = false
var imgOn : UIImage?
var imgOff : UIImage?

     
@IBOutlet var imgView: UIImageView!
    
@IBOutlet var btnResize: UIButton!

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        imgOn = UIImage(named: "lamp_on.png")
        imgOff = UIImage(named: "lamp_off.png")
        
        imgView.image = imgOn
        
}

@IBAction func btnResizeImage(_ sender: UIButton) {
        let scale : CGFloat = 2.0
        var newWidth:CGFloat , newHeight:CGFloat
        
        if (isZoom) {
            newWidth = imgView.frame.width / scale
            newHeight = imgView.frame.height / scale
            
            btnResize.setTitle("축소", for: .normal)
        } else {
            newWidth = imgView.frame.width * scale
            newHeight = imgView.frame.height * scale
            
            btnResize.setTitle("확대", for: .normal)
        }
        
        imgView.frame.size = CGSize(width: newWidth, height: newHeight)
        isZoom = !isZoom
}

데이트 피커 사용해 날짜 선택하기(날짜 포맷, 타이머 개발)

라이브러리에서 데이트피커(Date Picker)를 선택해서 메인스토리보드에 배치하고, Attributes inspector 를 클릭한 후, Style을 wheel로 변경하고, mode를 원하는 값을 설정합니다. 기본 설정값은 Date and Time입니다. 표기를 한글로 바꾸려면 Attributes inspector 에서 Locale 값을 Korean 으로 변경합니다. 

// fomatter를 이용해서 날짜와 시간을 표시하는 방법
@IBAction func changeDatePicker(_ sender: UIDatePicker) {
    let datePickerView = sender
        
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm EEEE"
    lblPickerTime.text = "선택시간 : " + formatter.string(from:datePickerView.date)
}
필드 심벌 결과 의미
년도(Year) yy 23 두 자리로 연도 표시
yyyy 2023 네 자리로 연도 표시
월(Month) M 9 한글자로 월 표시
MM 09 두 자리로 월 표시
MMM Sep 영문 3자리로 월 표시
MMMM September 영문 풀단어로 월 표시
주(Week) w 6 1~52까지 연간 주 순서(week of year) 표시
ww 06 01~52까지 2자리로 연간 주 순서(week of year) 표시
W 4 1~6까지 월간 주 순서(week of month) 표시
일(Day) d 8 1~31 까지 일을 표시
dd 08 01~32 까지 2자리로 일을 표시
D 35 1~366까지 연간 일 순서(day of year)를 표시
DD 35 01~366까지 연간 일 순서(day of year)를 표시
DDD 035 001~366까지 연간 일 순서(day of year)를 표시
요일(weekday) E,EE,EEE Mon Sunday~Saturday까지 3글자로 요일 표시
EEEE Monday Sunday~Saturday까지 요일 전체 이름 표시
EEEEE M 한 글자 약어 요일 표시
e 4 1~7까지 주간 날짜 순서 표시
ee 04 01~07까지 주간 날짜 순서 표시
시기(period) a PM AM/PM 표시
시간(Hour) h 3 1~12까지 시각을 표시
hh 03 01~12까지 시각을 표시
H 15 1~24까지 24시간 시각을 표시
HH 15 01~24까지 24시간 시각을 표시
분(minute) m 36 0~59까지 분을 표시
mm 36 00~59까지 두자리로 분을 표시
초(second) s 44 0~59까지 초를 표시
ss 44 00~59까지 두자리로 초를 표시
지역(Zone) z GMT+09:00 타임존 표시
Z +0900 GMT 시간차 표시
// Timer.scheduledTimer을 이용해서 1초 단위로 화면의 시간을 갱신
class ViewController: UIViewController {
    
    let timeSelector:Selector = #selector(ViewController.updateTime)
    let interval = 1.0
    var count = 0

    @IBOutlet var lblCurrentTime: UILabel!
    @IBOutlet var lblPickerTime: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        Timer.scheduledTimer(timeInterval: interval, target: self, selector:timeSelector, userInfo: nil, repeats: true)
    }
    
    @objc func updateTime() {
        let date = Date()
        
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss EEEE"
        lblCurrentTime.text = "현재시각 : " + formatter.string(from: date)
        
    }
}

요약 
- DatePicker에 Timer.scheduledTimer()를 이용해서 현재 시간을 1초단위로 갱신할 수 있고, 특정시간에 이벤트를 발생시킬수 있다.  

 

Posted by 목표를 가지고 달린다
,