Why is this animation not working as intended?

70 Views Asked by At

Hi there all the past couple days now I’ve been trying to modify the iOS tweak Daisy. I have successfully added more fonts, make the Hello animation stay on the screen and merged a closed pull request that the original dev didn’t want In. Anyways I’m up to trying to fix this bug that the original version has and that is that the “playHelloStartAnimation” will bug out sometimes when waking the phone. Sometimes it will fully replay the animation from the beginning or a couple frames from the middle of the animation, other times it will finish and show nothing for a few millisecond and then continue to the next animation or will play as intended. Tried adding a blank animation to play first so when it dose bug out you’ll won’t see but didn’t help, change times, tried other hooks still happens. Can’t figure out why any help would be appreciated.

%group DiaryHello

%hook CSCoverSheetView

%property(nonatomic, retain)UIView* diaryHelloIconView;
%property(nonatomic, retain)UILabel* diaryHelloLabel;

- (void)didMoveToWindow { // add iphone hello

    %orig;


    // hello label

    if (enableHelloSwitch && showHelloGreetingSwitch && ![self diaryHelloLabel]) {
        self.diaryHelloLabel = [UILabel new];
        [[self diaryHelloLabel] setTextColor:[UIColor whiteColor]];
        if ([fontFamilyValue intValue] == 0) [[self diaryHelloLabel] setFont:[UIFont fontWithName:@"Selawik-Regular" size:24]];
        else if ([fontFamilyValue intValue] == 1) [[self diaryHelloLabel] setFont:[UIFont systemFontOfSize:24 weight:UIFontWeightRegular]];
        [[self diaryHelloLabel] setText:greetingValue];
        [[self diaryHelloLabel] setTextAlignment:NSTextAlignmentCenter];
        [[self diaryHelloLabel] setAlpha:0];
        [[self diaryHelloLabel] setHidden:YES];
        [self addSubview:[self diaryHelloLabel]];

        [[self diaryHelloLabel] setTranslatesAutoresizingMaskIntoConstraints:NO];
        [NSLayoutConstraint activateConstraints:@[
            [self.diaryHelloLabel.topAnchor constraintEqualToAnchor:self.topAnchor constant:150],
            [self.diaryHelloLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
            [self.diaryHelloLabel.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
        ]];
    }

}

%new
- (void)initHelloViewWithAnimation:(int)animation { // set hello view up

    if (!enableHelloSwitch) return;
    if (enableMediaPlayerSwitch && ![[self diaryPlayerView] isHidden]) return;
    [[self diaryHelloIconView] stopAnimating];
    [[self diaryHelloIconView] removeFromSuperview];
    self.diaryHelloIconView = nil;

    if (animation == 0) {
        helloStartArray = [NSMutableArray new];
        for (int i = 0; i < 24; i++) [helloStartArray addObject:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"/Library/PreferenceBundles/DiaryPreferences.bundle/hello/start/%i.png", i]]];
        helloStartImage = [UIImage animatedImageWithImages:helloStartArray duration:0.6];
        self.diaryHelloIconView = [[UIImageView alloc] initWithImage:helloStartImage];

        helloSearchingArray = nil;
        helloSearchingImage = nil;
        helloAuthenticatedArray = nil;
        helloAuthenticatedImage = nil;
    } else if (animation == 1) {
        helloSearchingArray = [NSMutableArray new];
        for (int i = 0; i < 116; i++) [helloSearchingArray addObject:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"/Library/PreferenceBundles/DiaryPreferences.bundle/hello/searching/%i.png", i]]];
        helloSearchingImage = [UIImage animatedImageWithImages:helloSearchingArray duration:4.28];
        self.diaryHelloIconView = [[UIImageView alloc] initWithImage:helloSearchingImage];

        helloStartArray = nil;
        helloStartImage = nil;
        helloAuthenticatedArray = nil;
        helloAuthenticatedImage = nil;
    } else if (animation == 2) {
        helloAuthenticatedArray = [NSMutableArray new];
        for (int i = 0; i < 51; i++) [helloAuthenticatedArray addObject:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"/Library/PreferenceBundles/DiaryPreferences.bundle/hello/authenticated/%i.png", i]]];
        helloAuthenticatedImage = [UIImage animatedImageWithImages:helloAuthenticatedArray duration:1.12];
        self.diaryHelloIconView = [[UIImageView alloc] initWithImage:helloAuthenticatedImage];

        helloStartArray = nil;
        helloStartImage = nil;
        helloSearchingArray = nil;
        helloSearchingImage = nil;
    }

    [[self diaryHelloIconView] setContentMode:UIViewContentModeScaleAspectFit];
    [[self diaryHelloIconView] setClipsToBounds:YES];
    [[self diaryHelloIconView] setHidden:NO];
    if (![[self diaryHelloIconView] isDescendantOfView:self]) [self addSubview:[self diaryHelloIconView]];

    [[self diaryHelloIconView] setTranslatesAutoresizingMaskIntoConstraints:NO];
    [NSLayoutConstraint activateConstraints:@[
        [self.diaryHelloIconView.topAnchor constraintEqualToAnchor:self.topAnchor constant:50],
        [self.diaryHelloIconView.centerXAnchor constraintEqualToAnchor:self.centerXAnchor],
        [self.diaryHelloIconView.heightAnchor constraintEqualToConstant:80],
        [self.diaryHelloIconView.widthAnchor constraintEqualToConstant:80],
    ]];

}

%new
- (void)playHelloStartAnimation { // play hello start animation

    if (!enableHelloSwitch) return;
    if (enableMediaPlayerSwitch && ![[self diaryPlayerView] isHidden]) return;
    shouldPlaySearchAnimation = YES;
    [self initHelloViewWithAnimation:0];
    [[self diaryHelloIconView] setAnimationRepeatCount:1];
    [[self diaryHelloIconView] startAnimating];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [self playHelloSearchingAnimation];
    });

}

%new
- (void)playHelloSearchingAnimation { // play hello searching animation

    if (!enableHelloSwitch) return;
    if (enableMediaPlayerSwitch && ![[self diaryPlayerView] isHidden]) return;
    if (!shouldPlaySearchAnimation) return;
    [self initHelloViewWithAnimation:1];
    [[self diaryHelloIconView] setAnimationRepeatCount:0];
    [[self diaryHelloIconView] startAnimating];

    if (showHelloGreetingSwitch) [[self diaryHelloLabel] setAlpha:0];

}

%new
- (void)playHelloAuthenticatedAnimation { // play hello authenticated animation

    if (!enableHelloSwitch) return;
    if (enableMediaPlayerSwitch && ![[self diaryPlayerView] isHidden]) return;
    shouldPlaySearchAnimation = NO;
    [self initHelloViewWithAnimation:2];
    [[self diaryHelloIconView] setAnimationRepeatCount:1];
    [[self diaryHelloIconView] startAnimating];

    if (showHelloGreetingSwitch) {
        NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
        [UIView animateWithDuration:0.25 delay:0.4 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            [[self diaryHelloLabel] setHidden:NO];
            [[self diaryHelloLabel] setAlpha:1];
            [notificationCenter postNotificationName:@"diaryUpdateNotificationList" object:nil];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                [[self diaryHelloLabel] setAlpha:0];
            } completion:^(BOOL finished) {
                [[self diaryHelloIconView] setHidden:YES];
                [[self diaryHelloLabel] setHidden:YES];
                [[self diaryHelloIconView] removeFromSuperview];
                [notificationCenter postNotificationName:@"diaryUpdateNotificationList" object:nil];
            }];
        }];
    } else {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.8 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            [[self diaryHelloIconView] setHidden:YES];
            [[self diaryHelloIconView] removeFromSuperview];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"diaryUpdateNotificationList" object:nil];
        });
    }

}

%end

%hook CSCoverSheetViewController

- (void)viewDidDisappear:(BOOL)animated { // remove hello view when lock screen disappeared

    %orig;

    // free up memory when hello is not visible
    [[coverSheetView diaryHelloIconView] stopAnimating];
    [[coverSheetView diaryHelloIconView] removeFromSuperview];
    helloStartArray = nil;
    helloStartImage = nil;
    helloSearchingArray = nil;
    helloSearchingImage = nil;
    helloAuthenticatedArray = nil;
    helloAuthenticatedImage = nil;

}

%end

%hook SBLockScreenManager

- (void)lockUIFromSource:(int)arg1 withOptions:(id)arg2 { // remove hello view when device was locked

    %orig;

    // free up memory when hello is not visible
    isLockedHello = YES;
    isScreenOnHello = NO;
    [[coverSheetView diaryHelloIconView] stopAnimating];
    [[coverSheetView diaryHelloIconView] removeFromSuperview];
    helloStartArray = nil;
    helloStartImage = nil;
    helloSearchingArray = nil;
    helloSearchingImage = nil;
    helloAuthenticatedArray = nil;
    helloAuthenticatedImage = nil;

}

%end

%hook SBBacklightController

- (void)turnOnScreenFullyWithBacklightSource:(long long)arg1 { // update diary when screen turns on

    %orig;

    if (![[%c(SBLockScreenManager) sharedInstance] isLockScreenVisible]) return; // this method gets called not only when the screen gets turned on, so i verify that it was turned on by checking if the lock screen is visible
    if (!isScreenOnHello) [coverSheetView playHelloStartAnimation];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"diaryUpdateNotificationList" object:nil];
    isScreenOnHello = YES;

}

%end

%hook SBDashBoardBiometricUnlockController

- (void)setAuthenticated:(BOOL)arg1 { // play authenticated animation when unlocked with biometrics

    %orig;

    if (arg1 && isLockedHello) {
        isLockedHello = NO;
        [coverSheetView playHelloAuthenticatedAnimation];
    }

}

%end

%end

Original tweaks source code: https://github.com/schneelittchen/Diary

0

There are 0 best solutions below