iOS, swift3 重力感应(Accelerator,加速计)

效果
录屏.gif
代码
// ViewController.swift
import UIKit
import CoreMotion
import simd

class ViewController: UIViewController {
  let roundedArea: RoundedView = {
    let v = RoundedView()
    v.backgroundColor = .blue
    v.alpha = 0.7
    v.frame.size = CGSize(width: 200, height: 200)
    return v
  }()
  let plusImageView: UIImageView = {
    let iv = UIImageView()
    iv.frame.size = CGSize(width: 20, height: 20)
    iv.image = UIImage(named: "ios7-plus-empty")
    iv.tintColor = .black
    return iv
  }()
  let airplaneImageView: UIImageView = {
    let iv = UIImageView()
    iv.frame.size = CGSize(width: 30, height: 30)
    iv.image = UIImage(named: "airplane")
    return iv
  }()
  
  let motionManager = CMMotionManager()
  
  override func viewDidLoad() {
    super.viewDidLoad()
    roundedArea.center = view.center
    view.addSubview(roundedArea)
    plusImageView.center = view.center
    view.addSubview(plusImageView)
    airplaneImageView.center = view.center
    view.addSubview(airplaneImageView)
  }
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    startMotionUpdates()
  }
  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    stopMotionUpdates()
  }
  func startMotionUpdates() {
    guard motionManager.isAccelerometerAvailable else { return }
    motionManager.accelerometerUpdateInterval = TimeInterval(0.01)
    motionManager.showsDeviceMovementDisplay = true
    
    motionManager.startAccelerometerUpdates(
      to: .main,
      withHandler: { (accelerometerData, error) in
        guard let accelerometerData = accelerometerData else { return }

        let offsetCenter: double2 =
          [accelerometerData.acceleration.x,
           accelerometerData.acceleration.y]
        self.setAirplaneCenter(in: offsetCenter)
    })
  }
  func stopMotionUpdates() {
    guard motionManager.isAccelerometerAvailable else { return }
    
    motionManager.stopAccelerometerUpdates()
  }
  func setAirplaneCenter(in offsetCenter: double2) {
    let offsetX = offsetCenter.x
    let offsetY = offsetCenter.y
    print("offsetX: \(offsetX) ## offsetY: \(offsetY)")
    let originCenter = airplaneImageView.center
    let newCenter = CGPoint(
      x: Double(originCenter.x)+offsetX,
      y: Double(originCenter.y)-offsetY)
    if roundedArea.contains(point: newCenter) {
      airplaneImageView.center = newCenter
    }
  }
}

class RoundedView: UIView {
  override func layoutSubviews() {
    super.layoutSubviews()
    let radius: CGFloat = self.bounds.size.height / 2.0
    self.layer.cornerRadius = radius
    //    self.clipsToBounds = true
  }
}
extension UIView {
  func contains(point: CGPoint) -> Bool {
    let radius = bounds.width / 2
    let dx = point.x-center.x
    let dy = point.y-center.y
    if (dx*dx + dy*dy) <= radius*radius {
      return true
    } else {
      return false
    }
  }
}
iOS, swift3 重力感应(Accelerator,加速计)_第1张图片
airplane.png
iOS, swift3 重力感应(Accelerator,加速计)_第2张图片
ios7-plus-empty.png

你可能感兴趣的:(iOS, swift3 重力感应(Accelerator,加速计))