diff --git a/MotionKit.xcodeproj/project.pbxproj b/MotionKit.xcodeproj/project.pbxproj index f9980c2..835c615 100644 --- a/MotionKit.xcodeproj/project.pbxproj +++ b/MotionKit.xcodeproj/project.pbxproj @@ -166,9 +166,11 @@ TargetAttributes = { 75FDED541A9541F800283A97 = { CreatedOnToolsVersion = 6.1.1; + LastSwiftMigration = 0800; }; 75FDED5F1A9541F800283A97 = { CreatedOnToolsVersion = 6.1.1; + LastSwiftMigration = 0800; }; }; }; @@ -332,6 +334,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -347,6 +350,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -364,6 +368,7 @@ INFOPLIST_FILE = MotionKitTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -377,6 +382,7 @@ INFOPLIST_FILE = MotionKitTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -399,6 +405,7 @@ 75FDED6D1A9541F800283A97 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 75FDED6E1A9541F800283A97 /* Build configuration list for PBXNativeTarget "MotionKitTests" */ = { isa = XCConfigurationList; @@ -407,6 +414,7 @@ 75FDED701A9541F800283A97 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/MotionKit.xcodeproj/project.xcworkspace/xcuserdata/Adi.xcuserdatad/UserInterfaceState.xcuserstate b/MotionKit.xcodeproj/project.xcworkspace/xcuserdata/Adi.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..9a23c97 Binary files /dev/null and b/MotionKit.xcodeproj/project.xcworkspace/xcuserdata/Adi.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/MotionKit.xcodeproj/xcuserdata/Adi.xcuserdatad/xcschemes/MotionKit.xcscheme b/MotionKit.xcodeproj/xcuserdata/Adi.xcuserdatad/xcschemes/MotionKit.xcscheme new file mode 100644 index 0000000..36348d9 --- /dev/null +++ b/MotionKit.xcodeproj/xcuserdata/Adi.xcuserdatad/xcschemes/MotionKit.xcscheme @@ -0,0 +1,99 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/MotionKit.xcodeproj/xcuserdata/Adi.xcuserdatad/xcschemes/xcschememanagement.plist b/MotionKit.xcodeproj/xcuserdata/Adi.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..d78602b --- /dev/null +++ b/MotionKit.xcodeproj/xcuserdata/Adi.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,27 @@ + + + + + SchemeUserState + + MotionKit.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 75FDED541A9541F800283A97 + + primary + + + 75FDED5F1A9541F800283A97 + + primary + + + + + diff --git a/MotionKit/MotionKit.swift b/MotionKit/MotionKit.swift index eb0860c..b1d1504 100644 --- a/MotionKit/MotionKit.swift +++ b/MotionKit/MotionKit.swift @@ -15,20 +15,20 @@ import CoreMotion //_______________________________________________________________________________________________________________ // this helps retrieve values from the sensors. @objc protocol MotionKitDelegate { - optional func retrieveAccelerometerValues (x: Double, y:Double, z:Double, absoluteValue: Double) - optional func retrieveGyroscopeValues (x: Double, y:Double, z:Double, absoluteValue: Double) - optional func retrieveDeviceMotionObject (deviceMotion: CMDeviceMotion) - optional func retrieveMagnetometerValues (x: Double, y:Double, z:Double, absoluteValue: Double) - - optional func getAccelerationValFromDeviceMotion (x: Double, y:Double, z:Double) - optional func getGravityAccelerationValFromDeviceMotion (x: Double, y:Double, z:Double) - optional func getRotationRateFromDeviceMotion (x: Double, y:Double, z:Double) - optional func getMagneticFieldFromDeviceMotion (x: Double, y:Double, z:Double) - optional func getAttitudeFromDeviceMotion (attitude: CMAttitude) + @objc optional func retrieveAccelerometerValues (_ x: Double, y:Double, z:Double, absoluteValue: Double) + @objc optional func retrieveGyroscopeValues (_ x: Double, y:Double, z:Double, absoluteValue: Double) + @objc optional func retrieveDeviceMotionObject (_ deviceMotion: CMDeviceMotion) + @objc optional func retrieveMagnetometerValues (_ x: Double, y:Double, z:Double, absoluteValue: Double) + + @objc optional func getAccelerationValFromDeviceMotion (_ x: Double, y:Double, z:Double) + @objc optional func getGravityAccelerationValFromDeviceMotion (_ x: Double, y:Double, z:Double) + @objc optional func getRotationRateFromDeviceMotion (_ x: Double, y:Double, z:Double) + @objc optional func getMagneticFieldFromDeviceMotion (_ x: Double, y:Double, z:Double) + @objc optional func getAttitudeFromDeviceMotion (_ attitude: CMAttitude) } -@objc(MotionKit) public class MotionKit :NSObject{ +@objc(MotionKit) open class MotionKit :NSObject{ let manager = CMMotionManager() var delegate: MotionKitDelegate? @@ -52,27 +52,29 @@ import CoreMotion * given NSOperationQueue will be cancelled. You can access the retrieved values either by a * Trailing Closure or through a Delgate. */ - public func getAccelerometerValues (interval: NSTimeInterval = 0.1, values: ((x: Double, y: Double, z: Double) -> ())? ){ - var valX: Double! - var valY: Double! - var valZ: Double! - if manager.accelerometerAvailable { + open func getAccelerometerValues (_ interval: TimeInterval = 0.1, values: ((_ x: Double, _ y: Double, _ z: Double) -> ())? ){ + + if manager.isAccelerometerAvailable { manager.accelerometerUpdateInterval = interval - manager.startAccelerometerUpdatesToQueue(NSOperationQueue(), withHandler: { - (data, error) in + manager.startAccelerometerUpdates(to: OperationQueue(), withHandler: { + (accelerationData, error) in if let isError = error { - NSLog("Error: %@", isError) + NSLog("Error: \(isError)") } - valX = data!.acceleration.x - valY = data!.acceleration.y - valZ = data!.acceleration.z - if values != nil{ - values!(x: valX,y: valY,z: valZ) + guard let data = accelerationData else { + return } - let absoluteVal = sqrt(valX * valX + valY * valY + valZ * valZ) - self.delegate?.retrieveAccelerometerValues!(valX, y: valY, z: valZ, absoluteValue: absoluteVal) + + let valX = data.acceleration.x + let valY = data.acceleration.y + let valZ = data.acceleration.z + + values?(valX,valY,valZ) + + let absoluteVal = sqrt((valX * valX) + (valY * valY) + (valZ * valZ)) + self.delegate?.retrieveAccelerometerValues?(valX, y: valY, z: valZ, absoluteValue: absoluteVal) }) } else { NSLog("The Accelerometer is not available") @@ -88,27 +90,28 @@ import CoreMotion * given NSOperationQueue will be cancelled. You can access the retrieved values either by a * Trailing Closure or through a Delegate. */ - public func getGyroValues (interval: NSTimeInterval = 0.1, values: ((x: Double, y: Double, z:Double) -> ())? ) { + open func getGyroValues (_ interval: TimeInterval = 0.1, values: ((_ x: Double, _ y: Double, _ z:Double) -> ())? ) { - var valX: Double! - var valY: Double! - var valZ: Double! - if manager.gyroAvailable{ + if manager.isGyroAvailable{ manager.gyroUpdateInterval = interval - manager.startGyroUpdatesToQueue(NSOperationQueue(), withHandler: { - (data, error) in + manager.startGyroUpdates(to: OperationQueue(), withHandler: { + (rotationRateData, error) in if let isError = error{ - NSLog("Error: %@", isError) + NSLog("Error: \(isError)") } - valX = data!.rotationRate.x - valY = data!.rotationRate.y - valZ = data!.rotationRate.z - if values != nil{ - values!(x: valX, y: valY, z: valZ) + guard let data = rotationRateData else { + return } - let absoluteVal = sqrt(valX * valX + valY * valY + valZ * valZ) + + let valX = data.rotationRate.x + let valY = data.rotationRate.y + let valZ = data.rotationRate.z + + values?(valX, valY, valZ) + + let absoluteVal = sqrt((valX * valX) + (valY * valY) + (valZ * valZ)) self.delegate?.retrieveGyroscopeValues!(valX, y: valY, z: valZ, absoluteValue: absoluteVal) }) @@ -124,29 +127,30 @@ import CoreMotion * Starts magnetometer updates, providing data to the given handler through the given queue. * You can access the retrieved values either by a Trailing Closure or through a Delegate. */ - @available(iOS, introduced=5.0) - public func getMagnetometerValues (interval: NSTimeInterval = 0.1, values: ((x: Double, y:Double, z:Double) -> ())? ){ + @available(iOS, introduced: 5.0) + open func getMagnetometerValues (_ interval: TimeInterval = 0.1, values: ((_ x: Double, _ y:Double, _ z:Double) -> ())? ){ - var valX: Double! - var valY: Double! - var valZ: Double! - if manager.magnetometerAvailable { + if manager.isMagnetometerAvailable { manager.magnetometerUpdateInterval = interval - manager.startMagnetometerUpdatesToQueue(NSOperationQueue()){ - (data, error) in + manager.startMagnetometerUpdates(to: OperationQueue()){ + (magneticFieldData, error) in if let isError = error{ - NSLog("Error: %@", isError) + NSLog("Error: \(isError)") } - valX = data!.magneticField.x - valY = data!.magneticField.y - valZ = data!.magneticField.z - if values != nil{ - values!(x: valX, y: valY, z: valZ) + guard let data = magneticFieldData else { + return } - let absoluteVal = sqrt(valX * valX + valY * valY + valZ * valZ) - self.delegate?.retrieveMagnetometerValues!(valX, y: valY, z: valZ, absoluteValue: absoluteVal) + + let valX = data.magneticField.x + let valY = data.magneticField.y + let valZ = data.magneticField.z + + values?(valX, valY, valZ) + + let absoluteVal = sqrt((valX * valX) + (valY * valY) + (valZ * valZ)) + self.delegate?.retrieveMagnetometerValues?(valX, y: valY, z: valZ, absoluteValue: absoluteVal) } } else { @@ -165,20 +169,24 @@ import CoreMotion * attitudeReferenceFrame to determine this. You can access the retrieved values either by a * Trailing Closure or through a Delegate. */ - public func getDeviceMotionObject (interval: NSTimeInterval = 0.1, values: ((deviceMotion: CMDeviceMotion) -> ())? ) { + open func getDeviceMotionObject (_ interval: TimeInterval = 0.1, values: ((_ deviceMotion: CMDeviceMotion) -> ())? ) { - if manager.deviceMotionAvailable{ + if manager.isDeviceMotionAvailable{ manager.deviceMotionUpdateInterval = interval - manager.startDeviceMotionUpdatesToQueue(NSOperationQueue()){ - (data, error) in - + manager.startDeviceMotionUpdates(to: OperationQueue()){ + (objectData, error) in + if let isError = error{ - NSLog("Error: %@", isError) + NSLog("Error: \(isError)") } - if values != nil{ - values!(deviceMotion: data!) + + guard let data = objectData else { + return } - self.delegate?.retrieveDeviceMotionObject!(data!) + + values?(data) + + self.delegate?.retrieveDeviceMotionObject?(data) } } else { @@ -191,28 +199,24 @@ import CoreMotion * getAccelerationFromDeviceMotion:interval:values: * You can retrieve the processed user accelaration data from the device motion from this method. */ - public func getAccelerationFromDeviceMotion (interval: NSTimeInterval = 0.1, values: ((x:Double, y:Double, z:Double) -> ())? ) { - - var valX: Double! - var valY: Double! - var valZ: Double! - if manager.deviceMotionAvailable{ + open func getAccelerationFromDeviceMotion (_ interval: TimeInterval = 0.1, values: ((_ x:Double, _ y:Double, _ z:Double) -> ())? ) { + + if manager.isDeviceMotionAvailable{ manager.deviceMotionUpdateInterval = interval - manager.startDeviceMotionUpdatesToQueue(NSOperationQueue()){ + manager.startDeviceMotionUpdates(to: OperationQueue()){ (data, error) in if let isError = error{ - NSLog("Error: %@", isError) + NSLog("Error: \(isError)") } - valX = data!.userAcceleration.x - valY = data!.userAcceleration.y - valZ = data!.userAcceleration.z + + let valX = data!.userAcceleration.x + let valY = data!.userAcceleration.y + let valZ = data!.userAcceleration.z - if values != nil{ - values!(x: valX, y: valY, z: valZ) - } + values?(valX, valY, valZ) - self.delegate?.getAccelerationValFromDeviceMotion!(valX, y: valY, z: valZ) + self.delegate?.getAccelerationValFromDeviceMotion?(valX, y: valY, z: valZ) } } else { @@ -225,29 +229,23 @@ import CoreMotion * You can retrieve the processed gravitational accelaration data from the device motion from this * method. */ - public func getGravityAccelerationFromDeviceMotion (interval: NSTimeInterval = 0.1, values: ((x:Double, y:Double, z:Double) -> ())? ) { - - var valX: Double! - var valY: Double! - var valZ: Double! - if manager.deviceMotionAvailable{ + open func getGravityAccelerationFromDeviceMotion (_ interval: TimeInterval = 0.1, values: ((_ x:Double, _ y:Double, _ z:Double) -> ())? ) { + + if manager.isDeviceMotionAvailable{ manager.deviceMotionUpdateInterval = interval - manager.startDeviceMotionUpdatesToQueue(NSOperationQueue()){ + manager.startDeviceMotionUpdates(to: OperationQueue()){ (data, error) in if let isError = error{ - NSLog("Error: %@", isError) - } - valX = data!.gravity.x - valY = data!.gravity.y - valZ = data!.gravity.z - - if values != nil{ - values!(x: valX, y: valY, z: valZ) + NSLog("Error: \(isError)") } + let valX = data!.gravity.x + let valY = data!.gravity.y + let valZ = data!.gravity.z - let absoluteVal = sqrt(valX * valX + valY * valY + valZ * valZ) - self.delegate?.getGravityAccelerationValFromDeviceMotion!(valX, y: valY, z: valZ) + values?(valX, valY, valZ) + + self.delegate?.getGravityAccelerationValFromDeviceMotion?(valX, y: valY, z: valZ) } } else { @@ -261,21 +259,20 @@ import CoreMotion * You can retrieve the processed attitude data from the device motion from this * method. */ - public func getAttitudeFromDeviceMotion (interval: NSTimeInterval = 0.1, values: ((attitude: CMAttitude) -> ())? ) { + open func getAttitudeFromDeviceMotion (_ interval: TimeInterval = 0.1, values: ((_ attitude: CMAttitude) -> ())? ) { - if manager.deviceMotionAvailable{ + if manager.isDeviceMotionAvailable{ manager.deviceMotionUpdateInterval = interval - manager.startDeviceMotionUpdatesToQueue(NSOperationQueue()){ + manager.startDeviceMotionUpdates(to: OperationQueue()){ (data, error) in if let isError = error{ - NSLog("Error: %@", isError) - } - if values != nil{ - values!(attitude: data!.attitude) + NSLog("Error: \(isError)") } + + values?(data!.attitude) - self.delegate?.getAttitudeFromDeviceMotion!(data!.attitude) + self.delegate?.getAttitudeFromDeviceMotion?(data!.attitude) } } else { @@ -288,29 +285,28 @@ import CoreMotion * You can retrieve the processed rotation data from the device motion from this * method. */ - public func getRotationRateFromDeviceMotion (interval: NSTimeInterval = 0.1, values: ((x:Double, y:Double, z:Double) -> ())? ) { - - var valX: Double! - var valY: Double! - var valZ: Double! - if manager.deviceMotionAvailable{ + open func getRotationRateFromDeviceMotion (_ interval: TimeInterval = 0.1, values: ((_ x:Double, _ y:Double, _ z:Double) -> ())? ) { + + if manager.isDeviceMotionAvailable{ manager.deviceMotionUpdateInterval = interval - manager.startDeviceMotionUpdatesToQueue(NSOperationQueue()){ - (data, error) in + manager.startDeviceMotionUpdates(to: OperationQueue()){ + (rotationRateData, error) in if let isError = error{ - NSLog("Error: %@", isError) + NSLog("Error: \(isError)") } - valX = data!.rotationRate.x - valY = data!.rotationRate.y - valZ = data!.rotationRate.z - if values != nil{ - values!(x: valX, y: valY, z: valZ) + guard let data = rotationRateData else { + return } - let absoluteVal = sqrt(valX * valX + valY * valY + valZ * valZ) - self.delegate?.getRotationRateFromDeviceMotion!(valX, y: valY, z: valZ) + let valX = data.rotationRate.x + let valY = data.rotationRate.y + let valZ = data.rotationRate.z + + values?(valX, valY, valZ) + + self.delegate?.getRotationRateFromDeviceMotion?(valX, y: valY, z: valZ) } } else { @@ -324,30 +320,29 @@ import CoreMotion * You can retrieve the processed magnetic field data from the device motion from this * method. */ - public func getMagneticFieldFromDeviceMotion (interval: NSTimeInterval = 0.1, values: ((x:Double, y:Double, z:Double, accuracy: Int32) -> ())? ) { + open func getMagneticFieldFromDeviceMotion (_ interval: TimeInterval = 0.1, values: ((_ x:Double, _ y:Double, _ z:Double, _ accuracy: Int32) -> ())? ) { - var valX: Double! - var valY: Double! - var valZ: Double! - var valAccuracy: Int32! - if manager.deviceMotionAvailable{ + if manager.isDeviceMotionAvailable{ manager.deviceMotionUpdateInterval = interval - manager.startDeviceMotionUpdatesToQueue(NSOperationQueue()){ - (data, error) in + manager.startDeviceMotionUpdates(to: OperationQueue()){ + (magneticFieldData, error) in if let isError = error{ - NSLog("Error: %@", isError) + NSLog("Error: \(isError)") } - valX = data!.magneticField.field.x - valY = data!.magneticField.field.y - valZ = data!.magneticField.field.z - valAccuracy = data!.magneticField.accuracy.rawValue - if values != nil{ - values!(x: valX, y: valY, z: valZ, accuracy: valAccuracy) + guard let data = magneticFieldData else { + return } - self.delegate?.getMagneticFieldFromDeviceMotion!(valX, y: valY, z: valZ) + let valX = data.magneticField.field.x + let valY = data.magneticField.field.y + let valZ = data.magneticField.field.z + let valAccuracy = data.magneticField.accuracy.rawValue + + values?(valX, valY, valZ, valAccuracy) + + self.delegate?.getMagneticFieldFromDeviceMotion?(valX, y: valY, z: valZ) } } else { @@ -365,38 +360,38 @@ import CoreMotion /* MARK :- INSTANTANIOUS METHODS START HERE */ - public func getAccelerationAtCurrentInstant (values: (x:Double, y:Double, z:Double) -> ()){ + open func getAccelerationAtCurrentInstant (_ values: @escaping (_ x:Double, _ y:Double, _ z:Double) -> ()){ self.getAccelerationFromDeviceMotion(0.5) { (x, y, z) -> () in - values(x: x,y: y,z: z) + values(x,y,z) self.stopDeviceMotionUpdates() } } - public func getGravitationalAccelerationAtCurrentInstant (values: (x:Double, y:Double, z:Double) -> ()){ + open func getGravitationalAccelerationAtCurrentInstant (_ values: @escaping (_ x:Double, _ y:Double, _ z:Double) -> ()){ self.getGravityAccelerationFromDeviceMotion(0.5) { (x, y, z) -> () in - values(x: x,y: y,z: z) + values(x,y,z) self.stopDeviceMotionUpdates() } } - public func getAttitudeAtCurrentInstant (values: (attitude: CMAttitude) -> ()){ + open func getAttitudeAtCurrentInstant (_ values: @escaping (_ attitude: CMAttitude) -> ()){ self.getAttitudeFromDeviceMotion(0.5) { (attitude) -> () in - values(attitude: attitude) + values(attitude) self.stopDeviceMotionUpdates() } } - public func getMageticFieldAtCurrentInstant (values: (x:Double, y:Double, z:Double) -> ()){ + open func getMageticFieldAtCurrentInstant (_ values: @escaping (_ x:Double, _ y:Double, _ z:Double) -> ()){ self.getMagneticFieldFromDeviceMotion(0.5) { (x, y, z, accuracy) -> () in - values(x: x,y: y,z: z) + values(x,y,z) self.stopDeviceMotionUpdates() } } - public func getGyroValuesAtCurrentInstant (values: (x:Double, y:Double, z:Double) -> ()){ + open func getGyroValuesAtCurrentInstant (_ values: @escaping (_ x:Double, _ y:Double, _ z:Double) -> ()){ self.getRotationRateFromDeviceMotion(0.5) { (x, y, z) -> () in - values(x: x,y: y,z: z) + values(x,y,z) self.stopDeviceMotionUpdates() } } @@ -411,7 +406,7 @@ import CoreMotion * Discussion: * Stop accelerometer updates. */ - public func stopAccelerometerUpdates(){ + open func stopAccelerometerUpdates(){ self.manager.stopAccelerometerUpdates() NSLog("Accelaration Updates Status - Stopped") } @@ -422,7 +417,7 @@ import CoreMotion * Discussion: * Stops gyro updates. */ - public func stopGyroUpdates(){ + open func stopGyroUpdates(){ self.manager.stopGyroUpdates() NSLog("Gyroscope Updates Status - Stopped") } @@ -433,7 +428,7 @@ import CoreMotion * Discussion: * Stops device motion updates. */ - public func stopDeviceMotionUpdates() { + open func stopDeviceMotionUpdates() { self.manager.stopDeviceMotionUpdates() NSLog("Device Motion Updates Status - Stopped") } @@ -444,8 +439,8 @@ import CoreMotion * Discussion: * Stops magnetometer updates. */ - @available(iOS, introduced=5.0) - public func stopmagnetometerUpdates() { + @available(iOS, introduced: 5.0) + open func stopmagnetometerUpdates() { self.manager.stopMagnetometerUpdates() NSLog("Magnetometer Updates Status - Stopped") } diff --git a/MotionKitTests/MotionKitTests.swift b/MotionKitTests/MotionKitTests.swift index d78e1e7..0d36021 100644 --- a/MotionKitTests/MotionKitTests.swift +++ b/MotionKitTests/MotionKitTests.swift @@ -28,7 +28,7 @@ class MotionKitTests: XCTestCase { func testPerformanceExample() { // This is an example of a performance test case. - self.measureBlock() { + self.measure() { // Put the code you want to measure the time of here. } }