Last active
October 30, 2020 20:35
-
-
Save aheze/0eda6fb77c54eccce39d9126dd3b3358 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func updateHighlightOrientations(attitude: CMAttitude) { | |
/// initialAttitude is an optional that points to the reference frame that the device started at | |
/// we set this when the device lays out it's subviews on the first launch | |
if let initAttitude = initialAttitude { | |
/// We can now translate the current attitude to the reference frame | |
attitude.multiply(byInverseOf: initAttitude) | |
/// Roll is the movement of the phone left and right, Pitch is forwards and backwards | |
let rollValue = attitude.roll.radiansToDegrees | |
let pitchValue = attitude.pitch.radiansToDegrees | |
/// This is a magic number, but for simplicity, we won't do any advanced trigonometry -- also, 3 works pretty well | |
let conversion = Double(3) | |
/// Here, we figure out how much the values changed by comparing against the previous values (motionX and motionY) | |
let differenceInX = (rollValue - motionX) * conversion | |
let differenceInY = (pitchValue - motionY) * conversion | |
/// Now we adjust every highlight's potision | |
for highlight in previousHighlightComponents { | |
highlight.frame.origin.x += CGFloat(differenceInX) | |
highlight.frame.origin.y += CGFloat(differenceInY) | |
} | |
/// finally, we put the new attitude values into motionX and motionY so we can compare against them in 0.03 seconds (the next time this function is called) | |
motionX = rollValue | |
motionY = pitchValue | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment