Aiutolayout for notifier. Needs animations fixed.

This commit is contained in:
Samuel Clay 2017-10-30 11:30:12 -07:00
parent bbd61a65c3
commit 644759d752
5 changed files with 103 additions and 145 deletions

View file

@ -146,8 +146,12 @@
[[ThemeManager themeManager] addThemeGestureRecognizerToView:self.storyTitlesTable];
self.notifier = [[NBNotifier alloc] initWithTitle:@"Fetching stories..." inView:self.view];
self.notifier = [[NBNotifier alloc] initWithTitle:@"Fetching stories..."];
[self.view addSubview:self.notifier];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.notifier attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.notifier attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.notifier attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:NOTIFIER_HEIGHT]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.notifier attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20]];
}

View file

@ -8,12 +8,16 @@
#import <UIKit/UIKit.h>
#define NOTIFIER_HEIGHT 32
@interface NBNotifier : UIView {
UIView *progressBar;
@protected
UILabel *_txtLabel;
NSLayoutConstraint *progressBarWidthConstraint;
NSLayoutConstraint *txtLabelLeadingConstraint;
}
typedef enum {
@ -32,17 +36,15 @@ typedef enum {
@property (nonatomic, strong) NSString *title;
@property (nonatomic, assign) BOOL showing;
@property (nonatomic, retain) UIView *progressBar;
@property (nonatomic) NSLayoutConstraint *topOffsetConstraint;
- (id)initWithTitle:(NSString *)title;
- (id)initWithTitle:(NSString *)title inView:(UIView *)view;
- (id)initWithTitle:(NSString *)title inView:(UIView *)view withOffset:(CGPoint)offset;
- (id)initWithTitle:(NSString *)title inView:(UIView *)view style:(NBNotifierStyle)style;
- (id)initWithTitle:(NSString *)title inView:(UIView *)view style:(NBNotifierStyle)style withOffset:(CGPoint)offset;
- (id)initWithTitle:(NSString *)title withOffset:(CGPoint)offset;
- (id)initWithTitle:(NSString *)title style:(NBNotifierStyle)style;
- (id)initWithTitle:(NSString *)title style:(NBNotifierStyle)style withOffset:(CGPoint)offset;
- (void) didChangedOrientation:(NSNotification *)sender;
- (void)setAccessoryView:(UIView *)view animated:(BOOL)animated;
- (void)setProgress:(float)value;
- (void)setTitle:(id)title animated:(BOOL)animated;
- (void)show;
- (void)showIn:(float)time;

View file

@ -29,7 +29,6 @@
#include <QuartzCore/QuartzCore.h>
#define _displaytime 4.f
#define NOTIFIER_HEIGHT 32
#define PROGRESS_BAR_SIZE 40
@implementation NBNotifier
@ -38,6 +37,7 @@
@synthesize showing;
@synthesize progressBar;
@synthesize offset = _offset;
@synthesize topOffsetConstraint;
+ (void)initialize {
if (self == [NBNotifier class]) {
@ -51,36 +51,35 @@
if (self) {
// Initialization code
showing = NO;
self.translatesAutoresizingMaskIntoConstraints = NO;
}
return self;
}
- (id)initWithTitle:(NSString *)title {
return [self initWithTitle:title inView:[[[UIApplication sharedApplication] delegate] window]];
return [self initWithTitle:title style:NBLoadingStyle withOffset:CGPointZero];
}
- (id)initWithTitle:(NSString *)title inView:(UIView *)view {
return [self initWithTitle:title inView:view style:NBLoadingStyle withOffset:CGPointZero];
- (id)initWithTitle:(NSString *)title withOffset:(CGPoint)offset {
return [self initWithTitle:title style:NBLoadingStyle withOffset:offset];
}
- (id)initWithTitle:(NSString *)title inView:(UIView *)view withOffset:(CGPoint)offset {
return [self initWithTitle:title inView:view style:NBLoadingStyle withOffset:offset];
- (id)initWithTitle:(NSString *)title style:(NBNotifierStyle)style {
return [self initWithTitle:title style:NBLoadingStyle withOffset:CGPointZero];
}
- (id)initWithTitle:(NSString *)title inView:(UIView *)view style:(NBNotifierStyle)style {
return [self initWithTitle:title inView:view style:NBLoadingStyle withOffset:CGPointZero];
}
- (id)initWithTitle:(NSString *)title inView:(UIView *)view style:(NBNotifierStyle)style withOffset:(CGPoint)offset{
- (id)initWithTitle:(NSString *)title style:(NBNotifierStyle)style withOffset:(CGPoint)offset{
if (self = [super initWithFrame:CGRectMake(0, view.bounds.size.height - offset.y, view.bounds.size.width, NOTIFIER_HEIGHT)]){
// if (self = [super initWithFrame:CGRectMake(0, view.bounds.size.height - offset.y, view.bounds.size.width, NOTIFIER_HEIGHT)]){
if (self = [super init]) {
self.backgroundColor = [UIColor clearColor];
self.style = style;
self.offset = offset;
_txtLabel = [[UILabel alloc]initWithFrame:CGRectMake(32, 12, self.frame.size.width - 32, 20)];
// _txtLabel = [[UILabel alloc] initWithFrame:CGRectMake(32, 12, self.frame.size.width - 32, 20)];
_txtLabel = [[UILabel alloc] init];
_txtLabel.translatesAutoresizingMaskIntoConstraints = NO;
[_txtLabel setFont:[UIFont fontWithName: @"Helvetica" size: 16]];
[_txtLabel setBackgroundColor:[UIColor clearColor]];
@ -95,20 +94,32 @@
[self addSubview:_txtLabel];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_txtLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_txtLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20]];
txtLabelLeadingConstraint = [NSLayoutConstraint constraintWithItem:_txtLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:32];
[self addConstraint:txtLabelLeadingConstraint];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_txtLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
self.title = title;
self.view = view;
self.progressBar = [[UIView alloc] initWithFrame:CGRectMake(0, 4, 0, 1)];
// self.progressBar = [[UIView alloc] initWithFrame:CGRectMake(0, 4, 0, 1)];
self.progressBar = [[UIView alloc] init];
self.progressBar.translatesAutoresizingMaskIntoConstraints = NO;
self.progressBar.backgroundColor = UIColorFromRGB(0xD05046);
self.progressBar.alpha = 0.6f;
self.progressBar.hidden = YES;
[self addSubview:self.progressBar];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.progressBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:4]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.progressBar attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.progressBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:1.0 constant:1]];
progressBarWidthConstraint = [NSLayoutConstraint constraintWithItem:self.progressBar attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0];
[self addConstraint:progressBarWidthConstraint];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didChangedOrientation:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
// [[NSNotificationCenter defaultCenter] addObserver:self
// selector:@selector(didChangedOrientation:)
// name:UIDeviceOrientationDidChangeNotification
// object:nil];
}
return self;
@ -122,33 +133,47 @@
- (void) didChangedOrientation:(NSNotification *)sender {
// UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
// NSLog(@"Notifier changed orieintation to: %ld (%@/%@)", (long)orientation, NSStringFromCGRect(self.frame), NSStringFromCGRect(self.view.frame));
[self setView:self.view];
self.progressBar.frame = CGRectMake(0, 4, 0, 1);
// [self setView:self.view];
progressBarWidthConstraint.constant = 0;
// self.progressBar.frame = CGRectMake(0, 4, 0, 1);
}
- (void)setAccessoryView:(UIView *)accessoryView{
- (void)setAccessoryView:(UIView *)accessoryView {
if (_accessoryView) {
[_accessoryView removeFromSuperview];
}
_accessoryView = accessoryView;
accessoryView.translatesAutoresizingMaskIntoConstraints = NO;
int offset = 0;
if (self.style == NBSyncingStyle || self.style == NBSyncingProgressStyle) {
offset = 1;
}
// NSInteger leadingOffset = 30;
txtLabelLeadingConstraint.constant = 30;
if (self.style == NBSyncingStyle || self.style == NBSyncingProgressStyle) {
// leadingOffset = 34;
txtLabelLeadingConstraint.constant = 34;
// [_txtLabel setFrame:CGRectMake(34, (NOTIFIER_HEIGHT / 2) - 8, self.frame.size.width - 32, 20)];
// } else {
// [_txtLabel setFrame:CGRectMake(30, (NOTIFIER_HEIGHT / 2) - 8, self.frame.size.width - 32, 20)];
}
accessoryView.tag = 1;
[accessoryView setFrame:CGRectMake((32 - accessoryView.frame.size.width) / 2 + offset, ((self.frame.size.height -accessoryView.frame.size.height)/2)+2, accessoryView.frame.size.width, accessoryView.frame.size.height)];
// [accessoryView setFrame:CGRectMake((32 - accessoryView.frame.size.width) / 2 + offset, ((self.frame.size.height -accessoryView.frame.size.height)/2)+2, accessoryView.frame.size.width, accessoryView.frame.size.height)];
[self addSubview:accessoryView];
if (self.style == NBSyncingStyle || self.style == NBSyncingProgressStyle) {
[_txtLabel setFrame:CGRectMake(34, (NOTIFIER_HEIGHT / 2) - 8, self.frame.size.width - 32, 20)];
} else {
[_txtLabel setFrame:CGRectMake(30, (NOTIFIER_HEIGHT / 2) - 8, self.frame.size.width - 32, 20)];
}
[self addConstraint:[NSLayoutConstraint constraintWithItem:accessoryView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:32]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:accessoryView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:accessoryView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:accessoryView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:offset]];
}
- (void)setProgress:(float)value {
self.progressBar.frame = CGRectMake(0, 4, value * self.frame.size.width, 1);
progressBarWidthConstraint.constant = value * CGRectGetWidth(self.frame);
// self.progressBar.frame = CGRectMake(0, 4, value * self.frame.size.width, 1);
}
- (void)setTitle:(NSString *)title {
@ -168,10 +193,14 @@
self.accessoryView = activityIndicator;
} else if (style == NBOfflineStyle) {
UIImage *offlineImage = [UIImage imageNamed:@"g_icn_offline.png"];
self.accessoryView = [[UIImageView alloc] initWithImage:offlineImage];
UIImageView *imageView = [[UIImageView alloc] initWithImage:offlineImage];
imageView.contentMode = UIViewContentModeScaleAspectFit;
self.accessoryView = imageView;
} else if (style == NBSyncingProgressStyle) {
UIImage *offlineImage = [UIImage imageNamed:@"g_icn_offline.png"];
self.accessoryView = [[UIImageView alloc] initWithImage:offlineImage];
UIImageView *imageView = [[UIImageView alloc] initWithImage:offlineImage];
imageView.contentMode = UIViewContentModeScaleAspectFit;
self.accessoryView = imageView;
self.progressBar.hidden = NO;
} else if (style == NBSyncingStyle) {
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
@ -179,38 +208,32 @@
self.accessoryView = activityIndicator;
} else if (style == NBDoneStyle) {
UIImage *doneImage = [UIImage imageNamed:@"checkmark.png"];
self.accessoryView = [[UIImageView alloc] initWithImage:doneImage];
UIImageView *imageView = [[UIImageView alloc] initWithImage:doneImage];
imageView.contentMode = UIViewContentModeScaleAspectFit;
self.accessoryView = imageView;
}
[self setNeedsDisplay];
}
- (void)setView:(UIView *)view {
_view = view;
if (self.showing) {
self.frame = CGRectMake(self.offset.x, view.bounds.size.height - self.offset.y - self.frame.size.height, view.bounds.size.width, NOTIFIER_HEIGHT);
} else {
self.frame = CGRectMake(self.offset.x, view.bounds.size.height - self.offset.y, view.bounds.size.width, NOTIFIER_HEIGHT);
}
}
- (void)show {
[self showIn:(float)0.3f];
}
- (void)showIn:(float)time {
showing = YES;
CGRect frame = self.frame;
frame.size.width = self.view.frame.size.width;
self.frame = frame;
// CGRect frame = self.frame;
// frame.size.width = self.view.frame.size.width;
// self.frame = frame;
self.hidden = NO;
[UIView animateWithDuration:time animations:^{
CGRect move = self.frame;
move.origin.x = self.view.frame.origin.x + self.offset.x;
move.origin.y = self.view.frame.size.height - NOTIFIER_HEIGHT - self.offset.y;
self.frame = move;
// CGRect move = self.frame;
// move.origin.x = self.view.frame.origin.x + self.offset.x;
// move.origin.y = self.view.frame.size.height - NOTIFIER_HEIGHT - self.offset.y;
// self.frame = move;
topOffsetConstraint.constant = -1 * NOTIFIER_HEIGHT * 2;
[self layoutIfNeeded];
} completion:nil];
}
@ -227,97 +250,18 @@
// if (!showing) return;
[UIView animateWithDuration:seconds animations:^{
CGRect move = self.frame;
move.origin.y = self.view.bounds.size.height - self.offset.y;
self.frame = move;
// CGRect move = self.frame;
// move.origin.y = self.view.bounds.size.height - self.offset.y;
// self.frame = move;
topOffsetConstraint.constant = - 14;
[self layoutIfNeeded];
} completion:^(BOOL finished) {
self.hidden = YES;
// self.hidden = YES;
}];
showing = NO;
}
- (void)setAccessoryView:(UIView *)view animated:(BOOL)animated{
if (!animated){
[[self viewWithTag:1]removeFromSuperview];
view.tag = 1;
}
[view setFrame:CGRectMake(12, ((self.frame.size.height -view.frame.size.height)/2)+1, view.frame.size.width, view.frame.size.height)];
[self addSubview:view];
if (animated) {
view.alpha = 0.0;
if ([self viewWithTag:1])
view.tag = 0;
else
view.tag = 2;
[UIView animateWithDuration:0.5
animations:^{
if ([self viewWithTag:1])
[self viewWithTag:1].alpha = 0.0;
else
view.alpha = 1.0;
}
completion:^(BOOL finished){
[[self viewWithTag:1]removeFromSuperview];
[UIView animateWithDuration:0.5
animations:^{
view.alpha = 1.0;
}
completion:^(BOOL finished){
view.tag = 1;
}];
}];
}
if (self.style == NBSyncingStyle || self.style == NBSyncingProgressStyle) {
[_txtLabel setFrame:CGRectMake(34, (NOTIFIER_HEIGHT / 2) - 8, self.frame.size.width - 32, 20)];
} else {
[_txtLabel setFrame:CGRectMake(30, (NOTIFIER_HEIGHT / 2) - 8, self.frame.size.width - 32, 20)];
}
}
- (void)setTitle:(id)title animated:(BOOL)animated{
float duration = 0.0;
if (animated)
duration = 0.5;
[UIView animateWithDuration:duration
animations:^{
_txtLabel.alpha = 0.0f;
}
completion:^(BOOL finished){
_txtLabel.text = title;
[UIView animateWithDuration:duration
animations:^{
_txtLabel.alpha = 1.0f;
}
completion:^(BOOL finished){
}];
}];
}
- (void)drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
@ -440,3 +384,4 @@
}
@end

View file

@ -152,9 +152,13 @@ static UIFont *userLabelFont;
[[ThemeManager themeManager] addThemeGestureRecognizerToView:self.feedTitlesTable];
self.notifier = [[NBNotifier alloc] initWithTitle:@"Fetching stories..."
inView:self.innerView
withOffset:CGPointMake(0, 0)];
[self.view insertSubview:self.notifier belowSubview:self.feedViewToolbar];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.notifier attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.notifier attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.notifier attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:NOTIFIER_HEIGHT]];
self.notifier.topOffsetConstraint = [NSLayoutConstraint constraintWithItem:self.notifier attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.feedViewToolbar attribute:NSLayoutAttributeTop multiplier:1.0 constant:-20];
[self.view addConstraint:self.notifier.topOffsetConstraint];
// self.feedTitlesTable.backgroundColor = UIColorFromRGB(0xf4f4f4);
self.feedTitlesTable.separatorColor = [UIColor clearColor];

View file

@ -181,9 +181,12 @@
self.buttonBack = backButton;
self.notifier = [[NBNotifier alloc] initWithTitle:@"Fetching text..."
inView:self.view
withOffset:CGPointMake(0.0, 0.0 /*self.bottomSize.frame.size.height*/)];
[self.view addSubview:self.notifier];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.notifier attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.notifier attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.notifier attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:NOTIFIER_HEIGHT]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.notifier attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
[self.notifier hideNow];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {