アナログ時計の回転 – Swift3.0

 

環境

  • Xcode9
  • Xcode
  • Swift
  • Swift3.0
  • 時計
  • アナログ時計
  • 回転
  • ドラッグ

ソース

GitHub

実行

on.gif

実装

import UIKit
var MovedHourCount:Float = 0.0
var hG : Int = 0
var mG : Int = 0
var hour_rad : Float = 0

class ViewController: UIViewController {

    @IBOutlet weak var whattimenowMinute: UILabel!
    @IBOutlet weak var whattimenow: UILabel!
    var test : Float = 0
    var test2 : Float = 0
    var myView: UIView!
    var myView2: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        myView2 = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 100))
        myView2.center = self.view.center
        myView2.backgroundColor = UIColor.green;
        self.view.addSubview(myView2)
        myView2.layer.anchorPoint = CGPoint(x: 0.5, y: 1.0)
        let box2 = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
        box2.backgroundColor = UIColor.blue
        myView2.addSubview(box2)
        myView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 100))
        myView.center = self.view.center
        myView.backgroundColor = UIColor.red;
        self.view.addSubview(myView)
        myView.layer.anchorPoint = CGPoint(x: 0.5, y: 1.0)
        let box = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
        box.backgroundColor = UIColor.blue
        myView.addSubview(box)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent!) {
        let touch = touches.first!
        if touch.view === myView2.subviews[0] {
            let position = touch.location(in: self.view)
            let inC = CGPoint(x:view.frame.size.width / 2, y:view.frame.size.height / 2)
            var angle = atan2f(Float(position.y - inC.y), Float(position.x - inC.x)) + Float(Double.pi / 2)
            if(angle < 0){
                angle = atan2f(Float(position.y - inC.y), Float(position.x - inC.x)) + Float(Double.pi / 2) + Float(Double.pi * 2)
            }
            let HOUR_RADIUS = (Double.pi / 6)
            _ = (HOUR_RADIUS / 60)
            let h = (angle / Float(HOUR_RADIUS) ).truncatingRemainder(dividingBy: Float(12))
            var test_angel = Float(Double(angle) / Double.pi/2*360)
            print(test_angel)
            print(h)
            hG = Int((h).truncatingRemainder(dividingBy: 12))
            print("hGは\(hG)です。mGは\(mG)です。")
            let MINUTE_RADIUS = (Double.pi / 30)
            let m = (angle / Float(MINUTE_RADIUS) )
            whattimenowMinute.text = String(hG)
            myView2.transform = CGAffineTransform(rotationAngle:
                CGFloat(Float(HOUR_RADIUS) * Float(hG) +
                    hour_rad))
            print("\(Int((Int(m * 6) * Int(Double.pi)) / Int(180.0)))")
        }

        //red
        if touch.view === myView.subviews[0] {
            let position = touch.location(in: self.view)
            let inC = CGPoint(x:view.frame.size.width / 2, y:view.frame.size.height / 2)
            var angle = atan2f(Float(position.y - inC.y), Float(position.x - inC.x)) + Float(Double.pi / 2)
            if(angle < 0){
            angle = angle + Float(2 * Double.pi)
            }else if(angle >= Float(2 * Double.pi) ){
            angle = angle - Float(2 * Double.pi)
            }
            let HOUR_RADIUS = (Double.pi / 6)
            let MIN_HOUR_RADIUS = (HOUR_RADIUS / 60)
            let h = (angle / Float(HOUR_RADIUS) ).truncatingRemainder(dividingBy: Float(12))
            let MINUTE_RADIUS = (Double.pi / 30)
            let m = (angle / Float(MINUTE_RADIUS) )
            if(mG > 55 && m < 5){
                hG = (hG + 1)%12
            } else if(mG < 5 && m > 55){
                hG = (hG - 1)%12
                if(hG == -1){
                hG = 11
             }
            }

            mG =  Int(m)
            print("hGは\(hG)です。mGは\(mG)です。")
            whattimenow.text = String(mG)
            whattimenowMinute.text = String(hG)
            myView.transform = CGAffineTransform(rotationAngle: CGFloat((Float(mG * 6) * Float(Double.pi)) / 180.0))
            myView2.transform = CGAffineTransform(rotationAngle:CGFloat(Float(HOUR_RADIUS) * Float(hG) + Float(MIN_HOUR_RADIUS) * Float(mG)))
            hour_rad = Float(MIN_HOUR_RADIUS) * Float(mG)
            print(CGFloat(Float(HOUR_RADIUS) * Float(hG) + Float(MIN_HOUR_RADIUS) * Float(mG)))
        }
    }
}