VLCStatusLabel.m 1.99 KB
Newer Older
1 2 3 4 5 6 7
//
//  VLCStatusLabel.m
//  AspenProject
//
//  Created by Felix Paul Kühne on 17.05.13.
//  Copyright (c) 2013 VideoLAN. All rights reserved.
//
8 9
//  Refer to the COPYING file of the official project for license.
//
10 11 12 13 14 15 16 17

#import "VLCStatusLabel.h"

@implementation VLCStatusLabel

- (void)showStatusMessage:(NSString *)message
{
    self.text = message;
18 19 20 21

    /* layout and horizontal center in super view */
    [self sizeToFit];
    CGRect selfFrame = self.frame;
22
    CGRect parentFrame = [self superview].bounds;
23 24 25 26
    selfFrame.size.width += 15.; // take extra width into account for our custom drawing
    selfFrame.origin.x = (parentFrame.size.width - selfFrame.size.width) / 2.;
    [self setFrame:selfFrame];

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
    [self setNeedsDisplay];
    [self _toggleVisibility:NO];

    _displayTimer = [NSTimer scheduledTimerWithTimeInterval:1.5
                                                     target:self
                                                   selector:@selector(_hideAgain)
                                                   userInfo:nil
                                                    repeats:NO];
}

- (void)_hideAgain
{
    [self _toggleVisibility:YES];
    _displayTimer = nil;
}

- (void)_toggleVisibility:(BOOL)hidden
{
    CGFloat alpha = hidden? 0.0f: 1.0f;

    if (!hidden) {
        self.alpha = 0.0f;
        self.hidden = NO;
    }

    void (^animationBlock)() = ^() {
        self.alpha = alpha;
    };

    void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
        self.hidden = hidden;
    };

    [UIView animateWithDuration:0.3f animations:animationBlock completion:completionBlock];
}

- (void)drawRect:(CGRect)rect
{
    self.backgroundColor = [UIColor clearColor];
    CGContextClearRect(UIGraphicsGetCurrentContext(), rect);

    UIColor *drawingColor = [UIColor colorWithWhite:.20 alpha:.7];
    [drawingColor setFill];

    UIBezierPath* bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:rect.size.height / 2];
    [bezierPath fill];

    [super drawRect:rect];
}

@end