Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make animated image can resume from next frame after switch back to specified runloop mode #521

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions Source/Classes/AnimatedImages/PINAnimatedImageView.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ @interface PINAnimatedImageView ()
@property (nonatomic, assign) CGImageRef frameImage;
@property (nonatomic, strong) PINDisplayLink *displayLink;

@property (nonatomic, assign) CFTimeInterval lastDisplayLinkFire;

@end

@implementation PINAnimatedImageView
Expand Down Expand Up @@ -239,7 +237,6 @@ - (void)stopAnimating
PINAssertMain();

_displayLink.paused = YES;
_lastDisplayLinkFire = 0;

[_animatedImage clearAnimatedImageCache];
}
Expand Down Expand Up @@ -352,14 +349,11 @@ - (void)updateAnimationForPossibleVisibility
- (void)displayLinkFired:(PINDisplayLink *)displayLink
{
PINAssertMain();
CFTimeInterval timeBetweenLastFire;
if (_lastDisplayLinkFire == 0) {
timeBetweenLastFire = 0;
} else {
timeBetweenLastFire = CACurrentMediaTime() - self.lastDisplayLinkFire;
}

self.lastDisplayLinkFire = CACurrentMediaTime();
#if PIN_TARGET_MAC
CFTimeInterval timeBetweenLastFire = displayLink.duration;
#else
CFTimeInterval timeBetweenLastFire = displayLink.duration * displayLink.frameInterval;
#endif

_playHead += timeBetweenLastFire;

Expand All @@ -383,7 +377,7 @@ - (void)displayLinkFired:(PINDisplayLink *)displayLink
if (frameImage == nil) {
//Pause the display link until we get a file ready notification
displayLink.paused = YES;
self.lastDisplayLinkFire = 0;
_playHead = MAX(_playHead - timeBetweenLastFire, 0);
} else {
if (_frameImage) {
CGImageRelease(_frameImage);
Expand Down
1 change: 1 addition & 0 deletions Source/Classes/PINDisplayLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

@property(getter=isPaused, nonatomic) BOOL paused;
@property(nonatomic) NSInteger frameInterval;
@property(nonatomic, readonly) CFTimeInterval duration;

@end
#endif
21 changes: 18 additions & 3 deletions Source/Classes/PINDisplayLink.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ @interface PINDisplayLink ()
@property (nonatomic, readonly) NSRunLoop *runloop;
@property (nonatomic, readonly) NSRunLoopMode mode;

- (void)displayLinkFired;
- (void)displayLinkFiredWithDuration:(CFTimeInterval)duration;

@end

Expand All @@ -27,8 +27,9 @@ static CVReturn displayLinkFired (CVDisplayLinkRef displayLink,
CVOptionFlags *flagsOut,
void *displayLinkContext)
{
CFTimeInterval duration = inOutputTime->videoRefreshPeriod / (inOutputTime->videoTimeScale * inOutputTime->rateScalar);
PINDisplayLink *link = (__bridge PINDisplayLink *)displayLinkContext;
[link displayLinkFired];
[link displayLinkFiredWithDuration:duration];
return kCVReturnSuccess;
}

Expand All @@ -38,6 +39,7 @@ @implementation PINDisplayLink

BOOL _paused;
NSInteger _frameInterval;
CFTimeInterval _duration;
}

+ (PINDisplayLink *)displayLinkWithTarget:(id)target selector:(SEL)sel
Expand All @@ -63,9 +65,10 @@ - (void)dealloc
}
}

- (void)displayLinkFired
- (void)displayLinkFiredWithDuration:(CFTimeInterval)duration
{
dispatch_async(dispatch_get_main_queue(), ^{
self.duration = duration;
[self.runloop performSelector:self.selector target:self.target argument:self order:NSUIntegerMax modes:@[self.mode]];
});
}
Expand Down Expand Up @@ -111,6 +114,18 @@ - (void)setPaused:(BOOL)paused
}
}

- (CFTimeInterval)duration
{
PINAssertMain();
return _duration;
}

- (void)setDuration:(CFTimeInterval)duration
{
PINAssertMain();
_duration = duration;
}

- (NSInteger)frameInterval
{
PINAssertMain();
Expand Down