How to set up a progressView for an AVPlayer? - ios

I am thinking about using KVO for the Player's currentTime. But the currentTime is a method ([myPlayer currentTime]) instead of a property, would the KVO work in this case? If not, how could I track the progress of the video playing and set up a float number for the progressView?

Call the AVPlayer's addPeriodicTimeObserverForInterval:queue:usingBlock: to get periodic callbacks based on the current time. The current time is passed to your block as its parameter.

Related

UIViewPropertyAnimator - simply stop all animations, rather than a specific one?

In UIViewPropertyAnimator, is there a way to just stop all UIViewPropertyAnimator animations?
Or perhaps simply get all current animations - then of course you could stop them all.
Can this be done?
Or do you really have to
(a) do only one per UIViewPropertyAnimator,
and,
(b) keep a reference to each of those?
Every animation has to have atleast one UIViewPropertyAnimator instance. In order to stop the animation, we have to explicitly call stopAnimation(_:) on the animator instance.
A way would be to make a factory class to fetch UIViewPropertyAnimator instance and keep track of it in a set or array. And then use this factory class to stop or start all the animations at once. Or use one UIViewPropertyAnimator to perform all your animations and stop it.

Objective C: Check how long animation runs for

This is probably an easy answer, but I cannot find any information on it anywhere. I have an animation that transforms the x value of a label. If a certain task completes, the animation stops early and the completion action occurs. With this in mind, is there a way I can use the animation duration to determine if the timer ran out first or if the task completed?
I have a boolean that I was using to do this called taskComplete, but when I reset the view for the next level the completion sees the boolean as false and runs the code. For example, is there a way in xcode to do something like:
completion:^(BOOL finished) {
//boolean is false and animation has lasted the amount asked or greater
if (!taskComplete && animationDuration > animationTimer) {
//do this
}
}
All help is appreciated thank you!
There is way how to retrieve current animation status, though I have to say you are not supposed to do it (but if you need, what can you do :)
CALayer has method called .presentationLayer (docs):
The layer object returned by this method provides a close
approximation of the layer that is currently being displayed onscreen.
While an animation is in progress, you can retrieve this object and
use it to get the current values for those animations.
So on that layer, you can choose whatever attribute and run your condition against it. You can also add KVO to track any changes (and access it through view.layer.presentationLayer.attribute)
Other than that, you would have to use Core Animation or POPFramework from Facebook to track changes in greater detail.
Hope it helps!
Edit: I forgot to mention, if you need to know time, you can always calculate it from current value and start / end value as ((currentValue - startValue) / (endValue - startValue)) * animationTime, so there is no need to track it differently.
As #Sulthan stated, you should never base calculations off the duration of an animation. With this in mind, I opened another question on the same subject matter here:
https://stackoverflow.com/questions/31791748/objective-c-animation-not-stopping

How to pause game for a certain time and resume again

I want to pause my game for 2 seconds and continue again. I tried the following but it didn't work.
var duration = NSTimeInterval(2)
var wait=SKAction.waitForDuration(duration)
self.runAction(wait)
self is SKScene. Is there a different method to achieve this?
You can set the SKNode.isPaused property. This is a Boolean value that determines whether actions on the node and its descendants are processed.
This means that if you set self.paused = true in the SKScene, all of the child nodes of the scene will be paused. You can also do it to individual nodes like sprites.
For more information
https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKNode_Ref/index.html

how to pause DynamicAnimator

i keep seeing these methods,
– dynamicAnimatorDidPause: required method
– dynamicAnimatorWillResume: required method
But i've not found a way to call them. Ive set up an animator and called <UIDynamicAnimatorDelegate> in the .h, but I cannot seem to call pause on the self.animator for some reason.
Anyone have any tips for me?
Thanks!
Those are delegate methods, which means that you don't call them, they get called for you. You implement a delegate to get informed about certain events
The dynamic animator pauses by itself when all movement stops. Try adding a gravity effect with no collision boundaries. The animator should pause when all dynamic items fall offscreen.

iOS >> How to schedule timing based events that are not user action related

In my app I wish to have in a certain view a list of images that change on a UIImageView; something like an automated "presentation".
I'm looking for a way to perform this "automation". The idea is that I will have a list of images, each one will also have a timing property >> something like "Image_1" + "00:00", "Image_2" + "00:27", "Image_3" + "01:04" etc...
I guess I should probably use an NSDictionary to hold the file names and timing.
The only thing I'm not clear about, is which method I should use to "catch" the timing property from the NSDictionary and relate to them as "events" that run my code that will deal with the image change.
Anyone?
You can set up a NSTimer to fire after the required time, or you could use – performSelector:withObject:afterDelay:. NSTimer is probably the best option because you can cancel them if the view goes away.
Create a timer to trigger after the first delay.
When the timer fires, change the image (using a nice transition if you like) and set up the timer for the next delay.
If the view is removed, cancel the current timer.
I would use NSTimer you can set it to fire at a set time or at intervals, by using the current date. Id have NSTimer call a method which changes the images.
- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
This declaration method should do the trick
NSTimer Developer Doc
Hope this helps

Resources