大阪市中央区 システムソフトウェア開発会社

営業時間:平日09:15〜18:15
MENU

KotlinとSwiftでアナログ時計を作るーー長針、短針、秒針の更新処理(後)

著者:川上洋
公開日:2019/07/20
最終更新日:2019/07/20
カテゴリー:技術情報 雑記

こんにちは。川上です。

Swift版アナログ時計作成している長針、短針、秒針の更新や関連処理の諸々。

Timerで使った一般的な(?)表示の更新。

 
 // -- ライフサイクル:初期化メソッド
    override func viewDidLoad() {
        super.viewDidLoad()
 
        ・・・
        // ーーー Timerスケジュール --
        timerGameFunc()
    }

  func timerGameFunc() {
         Timer.scheduledTimer(timeInterval: 1.0,
            target: self,
            selector: #selector(ViewController.timerUpdate),
            userInfo: nil,
            repeats: true)
        
    }

    @objc func timerUpdate() {
        // --- アナログ時計Timer時の各針表示更新
        startTimeClock()
    }

MyPrg内でアッチャコッチャで使っている諸々の処理の色々。
・日付Dateの取得処理

struct TotnDate {
    // MARK: +++ add Weekday [ "日", "月", "火", "水", "木", "金", "土"];
    // MARK: ++++ Dateから 年・月・日・時・分・秒 の各Intを取得
    static func convertNSDateToYMDHMS (date: NSDate) -> (yy:Int,mth:Int,day:Int,hh:Int,min:Int,sec:Int,wkday:Int) {

        let gcal = NSCalendar(identifier: NSCalendar.Identifier.gregorian)!
        let flags : NSCalendar.Unit = [NSCalendar.Unit.year
            ,NSCalendar.Unit.month
            ,NSCalendar.Unit.day
            ,NSCalendar.Unit.hour
            ,NSCalendar.Unit.hour
            ,NSCalendar.Unit.minute
            ,NSCalendar.Unit.second
            ,NSCalendar.Unit.nanosecond
            ,NSCalendar.Unit.weekday]
        let dComp = gcal.components(flags, from: date as Date)
        let lYYY = dComp.year //as! Int
        let lMM   = dComp.month //as! Int
        let lDD   = dComp.day //as! Int
        let lHH   = dComp.hour //as! Int
        let lMINI   = dComp.minute //as! Int
        let lSEC   = dComp.second //as! Int
     // -- Weekday [ "日", "月", "火", "水", "木", "金", "土"]
        let lWeekDay = dComp.weekday! - 1   
        
        return (yy:lYYY!,mth:lMM!,day:lDD!,hh:lHH!,min:lMINI!,sec:lSEC!,wkday:lWeekDay)
        
    }
}

> Date取得ログ:datetpl =(yy: 2019, mth: 7, day: 18, hh: 13, min: 17, sec: 22, wkday: 4)

・ImageViewのリサイズ

// --- UIImageのextension :
extension UIImage {
    // MARK:+++ リサイズ resize
    func resize(size _size: CGSize) -> UIImage? {
        let widthRatio = _size.width / size.width
        let heightRatio = _size.height / size.height
        let ratio = widthRatio < heightRatio ? widthRatio : heightRatio
        let resizedSize = CGSize(width: size.width * ratio, height: size.height * ratio)
        
        UIGraphicsBeginImageContextWithOptions(resizedSize, false, 0.0) 
        draw(in: CGRect(origin: .zero, size: resizedSize))
        let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return resizedImage
    }
}

現状、Swift4.2で使えますが、Swift5.0では、ワーニング潰しが要るかも・・です

iPhone用の動画はココです。

ではでは。

    次の記事 :
    上に戻る