开发者

Obj-C: Constantly observe changes to a property(double value) to trigger if's

I want to use the property currentPlaybackTime, which returns a double value to trigger a series of if statements at certain points throughout the movie's playback to pause it. Because of the approximate behavior of doubles, it never syncs up with开发者_如何学C the value in my if statement, and the movie doesn't stop. I can't use a range though because when I resume playing, it might still be within the range an immediately pause again.

SO I think what I need to do is round this double value to 2 decimal places.

But how do I observe each change to the currentPlaybackTime property? Right now I have a timer set to call the method checking my if statements against the playback time every .0333 seconds(approx 30 times a second(my movies framerate)). However this is inexact and I feel almost certainly wrong.


Perfect time to check out Key-Value Observing.


Just an FYI for others since this is an old thread:

unfortunately currentPlaybackTime doesn't seem to be KVO-compliant, so Hippocrates method of polling with a timer is probably the best alternative


I'm not sure to what the property you're attempting to observe belongs, but it must be KVO compliant for this approach to work. That isn't a given for every property.

Internally, the code providing you currentPlaybackTime must call willChangeValueForKey: and didChangeValueForKey: (if it uses KVC to change the property, it gets these calls for free). I'd check the documentation (or the code if it's yours) to ensure this is observable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜