0

再帰的なアニメーションを処理する Animations Manager クラスを作成しようとしています。それは機能していますが、信頼性が低いです。たとえば、時々、それを使用すると、アニメーションは「連続して」発生し、その間に遅延はありません。それを使用すると、アニメーション間に遅延が残ります(これは、他のView Controllerにある他の要素で発生しています)

これが私のコードです:

@implementation customAnimationTimer

-(id) initWithTimeInterval:(NSTimeInterval)timeInterval target:(UIView*)target animationType:(NSInteger)animationType
              fromValue:(CGFloat)fromValue toValue:(CGFloat)toValue withDelegate:(id <customAnimationTimerDelegate>)delegate {

    if (self = [super init]) {
        self.delegate = delegate;
        _timeInterval = timeInterval;
        _target = target;
        _animationState = NO;
        _animationType = animationType;
        _fromValue = fromValue;
        _toValue = toValue;

        _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkFire)];
        [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    }
    return self;
}

-(void) displayLinkFire {if (self.delegate) [self.delegate displayLinkUpdate:self];}

@end


@implementation animationManager


static animationManager* sharedHelper = nil;

+ (animationManager*) sharedInstance {
    if (!sharedHelper) sharedHelper = [[animationManager alloc] init];
    return sharedHelper;
}


-(id) init { if (self = [super init]) {[self initArrays];} return self;}

-(void) initArrays {
    _activeTimers = [NSMutableArray arrayWithObjects: nil];
}

-(void) displayLinkUpdate:(customAnimationTimer*)timer {
    if (timer.displayLink.frameInterval != 1) [self animateWithTimer:timer];
    timer.displayLink.frameInterval = (timer.timeInterval/timer.displayLink.duration);
}

-(void) animateWithTimer:(customAnimationTimer*)timer {
    if (!timer.animationState) {

        timer.animationState = true;
        [UIView animateWithDuration:timer.timeInterval animations:^{
           if (timer.animationType == 0) timer.target.alpha = timer.toValue;
        }];

    } else {

        timer.animationState = false;
        [UIView animateWithDuration:timer.timeInterval animations:^{
            if (timer.animationType == 0) timer.target.alpha = timer.fromValue;
        }];

    }
}

-(void) addAnimationToView:(UIView*)view withType:(int)animationType fromValue:(CGFloat)fromValue toValue:(CGFloat)toValue withTime:(CGFloat)time  {
    [_activeTimers addObject: [[customAnimationTimer alloc] initWithTimeInterval:time target:view animationType:animationType fromValue:fromValue toValue:toValue withDelegate:self]];
}

-(void) removeAnimationFromView:(UIView*)view {

    NSInteger index = 900000000;
    for (customAnimationTimer* timer in _activeTimers) {
        if (timer.target == view) {
            index = [_activeTimers indexOfObject:timer];
            [timer.displayLink invalidate];
        }
    }

    if (index != 900000000) [_activeTimers removeObjectAtIndex:index];

}

-(void) removeAnimations {
    for (customAnimationTimer* timer in _activeTimers) [timer.displayLink invalidate];
    [self initArrays];
}


@end
4

2 に答える 2

0

さて、それを録音して、2 つの録音を並べて再生したところ、実際に私の目がいたずらをしていました。そのうちの 1 つは青色の背景に、もう 1 つは赤色の背景にありました。これは何か関係があると思います。混乱させてしまった方々、申し訳ありません!

于 2014-04-02T20:19:56.957 に答える