VLCAboutViewController.m 5.29 KB
Newer Older
Felix Paul Kühne's avatar
Felix Paul Kühne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*****************************************************************************
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCAboutViewController.h"

@interface VLCAboutViewController ()
{
16 17
    CADisplayLink *displayLink;
    NSTimer *startAnimationTimer;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31
}
@end

@implementation VLCAboutViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSBundle *mainBundle = [NSBundle mainBundle];
    self.versionLabel.text = [[NSString stringWithFormat:NSLocalizedString(@"VERSION_FORMAT", nil), [mainBundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]] stringByAppendingFormat:@" (%@)", [mainBundle objectForInfoDictionaryKey:@"CFBundleVersion"]];
    self.basedOnLabel.text = [[NSString stringWithFormat:NSLocalizedString(@"BASED_ON_FORMAT", nil),[[VLCLibrary sharedLibrary] version]] stringByReplacingOccurrencesOfString:@"<br />" withString:@" "];
    self.titleLabel.text = self.title;
    self.titleLabel.textColor = [UIColor colorWithWhite:0.5 alpha:1.];

32 33 34 35 36 37 38 39 40 41 42 43
    NSMutableAttributedString *aboutContents = [[NSMutableAttributedString alloc] initWithData:[[NSString stringWithContentsOfFile:[[NSBundle mainBundle]
                                                                                                                                    pathForResource:@"About Contents" ofType:@"html"]
                                                                                                                          encoding:NSUTF8StringEncoding
                                                                                                                             error:nil]
                                                                                                dataUsingEncoding:NSUTF8StringEncoding]
                                                                                       options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                                                 NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                                                            documentAttributes:nil error:nil];
    if ([UIScreen mainScreen].traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
        [aboutContents addAttribute:NSForegroundColorAttributeName value:[UIColor VLCLightTextColor] range:NSMakeRange(0., aboutContents.length)];
    }

44
    UITextView *textView = self.blablaTextView;
45
    textView.attributedText = aboutContents;
46 47 48 49 50 51 52 53 54
    textView.scrollEnabled = YES;
    textView.panGestureRecognizer.allowedTouchTypes = @[ @(UITouchTypeIndirect) ];
    [textView.panGestureRecognizer addTarget:self action:@selector(scrollViewPan:)];
    textView.userInteractionEnabled = YES;
    textView.showsVerticalScrollIndicator = YES;

    UITapGestureRecognizer *tapUpArrowRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollToTop)];
    tapUpArrowRecognizer.allowedPressTypes = @[@(UIPressTypeUpArrow)];
    [textView addGestureRecognizer:tapUpArrowRecognizer];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
55 56
}

57
- (void)viewDidAppear:(BOOL)animated
Felix Paul Kühne's avatar
Felix Paul Kühne committed
58
{
59 60 61 62 63 64 65
    [super viewDidAppear:animated];
    [self startAnimationTimer];
}
- (void) viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self stopAnimation];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
66 67
}

68
- (UIView *)preferredFocusedView
Felix Paul Kühne's avatar
Felix Paul Kühne committed
69
{
70 71 72 73 74 75 76 77 78
    return self.blablaTextView;
}

- (void)scrollToTop
{
    [self stopAnimation];
    [self.blablaTextView setContentOffset:CGPointZero animated:YES];
    [self startAnimationTimer];
}
Felix Paul Kühne's avatar
Felix Paul Kühne committed
79

80 81 82 83 84 85 86 87 88 89 90
- (void)scrollViewPan:(UIPanGestureRecognizer *)recognizer
{
    switch(recognizer.state) {
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
        case UIGestureRecognizerStateEnded:
            [self startAnimationTimer];
            break;
        default:
            [self stopAnimation];
            break;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
91 92 93
    }
}

94
- (void)startAnimationTimer
Felix Paul Kühne's avatar
Felix Paul Kühne committed
95
{
96
    [startAnimationTimer invalidate];
97
    startAnimationTimer = [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(startAnimation) userInfo:nil repeats:NO];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
98 99
}

100 101 102 103 104 105 106 107
- (void)stopAnimation
{
    [startAnimationTimer invalidate];
    startAnimationTimer = nil;
    [displayLink invalidate];
    displayLink = nil;
}
- (void)startAnimation
Felix Paul Kühne's avatar
Felix Paul Kühne committed
108
{
109 110 111 112
    [displayLink invalidate];
    displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTriggered:)];
    [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
Felix Paul Kühne's avatar
Felix Paul Kühne committed
113

114 115 116 117 118 119
- (void)displayLinkTriggered:(CADisplayLink*)link
{
    UIScrollView *scrollView = self.blablaTextView;
    CGFloat viewHeight = CGRectGetHeight(scrollView.frame);
    CGFloat maxOffsetY = scrollView.contentSize.height - viewHeight;

120
    CFTimeInterval secondsPerPage = 5.0;
121
    CGFloat offset = link.duration/secondsPerPage * viewHeight;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
122

123
    CGFloat newYOffset = scrollView.contentOffset.y + offset;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
124

125 126 127 128
    if (newYOffset > maxOffsetY+viewHeight) {
        scrollView.contentOffset = CGPointMake(0, -viewHeight);
    } else {
        scrollView.contentOffset = CGPointMake(0, newYOffset);
Felix Paul Kühne's avatar
Felix Paul Kühne committed
129 130 131 132
    }
}

@end