Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MWMAnimator.mm « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f191ff8ff3dab3759aeacca576f266dc9cd8644e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#import "MWMAnimator.h"
#import <objc/runtime.h>

@interface MWMAnimator ()

@property (nonatomic) CADisplayLink * displayLink;
@property (nonatomic) NSMutableSet * animations;

@end

@implementation MWMAnimator

+ (instancetype)animatorWithScreen:(UIScreen *)screen
{
  if (!screen)
    screen = [UIScreen mainScreen];

  MWMAnimator * animator = objc_getAssociatedObject(screen, _cmd);
  if (!animator)
  {
    animator = [[self alloc] initWithScreen:screen];
    objc_setAssociatedObject(screen, _cmd, animator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  }
  return animator;
}

- (instancetype)initWithScreen:(UIScreen *)screen
{
  self = [super init];
  if (self)
  {
    self.displayLink = [screen displayLinkWithTarget:self selector:@selector(animationTick:)];
    self.displayLink.paused = YES;
    [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    self.animations = [NSMutableSet set];
  }
  return self;
}

- (void)animationTick:(CADisplayLink *)displayLink
{
  CFTimeInterval const dt = displayLink.duration;
  for (id<Animation> a in self.animations.copy)
  {
    BOOL finished = NO;
    [a animationTick:dt finished:&finished];

    if (finished)
      [self.animations removeObject:a];

  }

  if (self.animations.count == 0)
    self.displayLink.paused = YES;
}

- (void)addAnimation:(id<Animation>)animation
{
  [self.animations addObject:animation];

  if (self.animations.count == 1)
    self.displayLink.paused = NO;
}

- (void)removeAnimation:(id <Animation>)animatable
{
  if (animatable == nil)
    return;

  [self.animations removeObject:animatable];

  if (self.animations.count == 0)
    self.displayLink.paused = YES;
}

@end

@implementation UIView (AnimatorAdditions)

- (MWMAnimator *)animator
{
  return [MWMAnimator animatorWithScreen:self.window.screen];
}

@end