I am trying to build a feature so that when you shake the iPhone, the particles on the screen will fly around.
I have hooks to tie into when the shaking begins and ends.
I will create a UIPushBehavior and add it to the animator scene when shaking begins and when it ends I will remove the UIPushBehavior from the animator.
But to get a good shaking experience I want to vary the push angle while the device is in motion.
How can I recreate this pseudo code in Swift?
override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent!)
{
if (motion == UIEventSubtype.MotionShake)
{
self.push = UIPushBehavior()
self.push!.angle = -self.angle!
self.animator.addBehavior(self.push)
// BEGIN Pseudo Code
self.sequence = Sequence({self.push.angle = self.angle!}, {self.push.angle = -self.angle},
delay: 100ms)
// END Pseudo Code
}
}
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent!)
{
if (motion == UIEventSubtype.MotionShake)
{
self.animator.removeBehavior(self.push)
// BEGIN Pseudo Code
self.sequence.stopSequence()
// END Pseudo Code
}
}
So as you can see I want to be able to define 2 functions (which change the direction of push angle by 180 degrees) to be looped every 100ms, until I call stopSequence()
How can I actually do this with Swift?
Are you looking for something like this:
class MySequence {
var functions : Array<() -> ()>
func runSequenceOneTime () -> Void {
for function in functions {
function ()
}
}
func stopSequence () -> Void {
// uninstall alarm
}
func startSequence (delay: Double) -> Void {
// install alarm w/ runSequenceOneTime
}
init (delay: Double, _ functions: (() -> ())...) {
self.functions = functions
self.startSequence (delay)
}
}
and then you'd create an instance of MySequence
as:
var mySeq = MySequence (delay: 0.1, func1, func2, func3)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.