Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions Animated Banner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Animated Banner
Great-looking animated banner for your preferences

## Preview
<figure>
<img src="images/showcase.gif" alt="Showcase" width=500 />
<figcaption>Showcase of the banner</figcaption>
</figure>

## Features
- Simple but nice
- Looks good in both light and dark mode
- Endlessly customizable with animations and gradient
- Stays always vertically centered to fit the space
- Dynamically enabled/disabled by a specifier

## Usage
### Basic implementation
```objective-c
- (void)viewDidLoad {
[super viewDidLoad];

NSArray *myUIColorArray = @[
[UIColor redColor],
[UIColor blueColor]
];
CGFloat navbarOffset = 64; // default navigation bar height for non-notched devices. Required for scroll animation. Can be obtain with:
// self.view.window.windowScene.statusBarManager.statusBarFrame.size.height + self.realNavigationController.navigationBar.frame.size.height

self.table.tableHeaderView = [[XXXAnimatedBanner alloc] initWithTitle:@"Title" subtitle:@"v1.0" colors:myUIColorArray defaultOffset:-navbarOffset];
}
```

### Animate on scroll
```objective-c
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ([self.table.tableHeaderView isKindOfClass:[SPDAnimatedBanner class]]) {
[(SPDAnimatedBanner *)self.table.tableHeaderView adjustStackPositionToScrollOffset:scrollView.contentOffset.y];
}
}
```

### Tip 1: dynamically enable/disable
```objective-c
- (void)setPreferenceValue:(id)value specifier:(PSSpecifier *)specifier {
[super setPreferenceValue:value specifier:specifier];

if ([specifier.properties[@"key"] isEqualToString:@"enabled"] && [self.table.tableHeaderView isKindOfClass:[SPDAnimatedBanner class]]) {
((SPDAnimatedBanner *)self.table.tableHeaderView).activateGradient = [value boolValue];
}
}
```

### Tip 2: relaunch animations when they get stopped by iOS
```objective-c
- (void)viewDidLoad {
[super viewDidLoad];

self.table.tableHeaderView = [[XXXAnimatedBanner alloc] initWithTitle:...];
[self refreshBanner];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshBanner) name:UIApplicationDidBecomeActiveNotification object:nil]; // revive after app closed and reopened
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

if (!self.isMovingToParentViewController) [self refreshBanner]; // revive after going in sub pages
}

- (void)refreshBanner {
if ([self.table.tableHeaderView isKindOfClass:[SPDAnimatedBanner class]]) {
((SPDAnimatedBanner *)self.table.tableHeaderView).activateGradient = yourSwitchValue;
}
}
```

## Gallery
<figure>
<img src="images/animated.gif" alt="Animatable" width=500 />
<figcaption>Animatable</figcaption>
</figure>
<figure>
<img src="images/light_and_dark.gif" alt="Light and Dark modes" width=500 />
<figcaption>Gorgeous in light or dark</figcaption>
</figure>
<figure>
<img src="images/togglable.gif" alt="Togglable" width=500 />
<figcaption>Can be controlled from a specifier</figcaption>
</figure>
15 changes: 15 additions & 0 deletions Animated Banner/XXXAnimatedBanner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#import <UIKit/UIKit.h>

@interface XXXAnimatedBanner : UIView
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UIView *gradientView;
@property(nonatomic, strong) UILabel *subtitleLabel;
@property(nonatomic, strong) CAGradientLayer *gradient;
@property(nonatomic, strong) NSMutableArray *animations;
@property(nonatomic, strong) NSMutableArray *cgcolors;
@property(nonatomic) CGFloat defaultOffset;
@property(nonatomic, strong) NSLayoutConstraint *stackCenter;
@property(nonatomic, assign, getter=isGradientActivated) BOOL activateGradient;
- (instancetype)initWithTitle:(NSString *)title subtitle:(NSString *)subtitle colors:(NSArray<__kindof UIColor *> *)colors defaultOffset:(CGFloat)offset;
- (void)adjustStackPositionToScrollOffset:(CGFloat)offset;
@end
127 changes: 127 additions & 0 deletions Animated Banner/XXXAnimatedBanner.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#import "XXXAnimatedBanner.h"

@implementation XXXAnimatedBanner

- (instancetype)initWithTitle:(NSString *)title subtitle:(NSString *)subtitle colors:(NSArray<__kindof UIColor *> *)colors defaultOffset:(CGFloat)defaultOffset {
if (self = [super initWithFrame:CGRectMake(0, 0, self.superview.frame.size.width, 200)]) {
// Labels
self.titleLabel = [[UILabel alloc] init];
self.titleLabel.text = title;
self.titleLabel.font = [UIFont boldSystemFontOfSize:42.f];
self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
[self.titleLabel sizeToFit];
// CGPoint titleFrameAnchor = CGPointMake(CGRectGetMinX(self.titleLabel.frame), CGRectGetMaxY(self.titleLabel.frame));
// self.titleLabel.layer.anchorPoint = CGPointMake(0, 1);
// self.titleLabel.layer.position = titleFrameAnchor; // ~ top center

self.subtitleLabel = [[UILabel alloc] init];
self.subtitleLabel.text = subtitle;
self.subtitleLabel.font = [UIFont systemFontOfSize:24.f weight:UIFontWeightMedium];
if (@available(iOS 13.0, *)) {
self.subtitleLabel.textColor = [UIColor secondaryLabelColor];
} else {
self.subtitleLabel.textColor = [UIColor systemGrayColor];
}
self.subtitleLabel.lineBreakMode = NSLineBreakByWordWrapping;
[self.subtitleLabel sizeToFit];
// CGPoint subtitleFrameAnchor = CGPointMake(CGRectGetMinX(self.subtitleLabel.frame), CGRectGetMinY(self.subtitleLabel.frame));
// self.subtitleLabel.layer.anchorPoint = CGPointMake(0, 0);
// self.subtitleLabel.layer.position = subtitleFrameAnchor; // bottom center

// Create gradient
self.gradient = [CAGradientLayer layer];
self.gradient.frame = self.titleLabel.frame;
self.cgcolors = [NSMutableArray new];
for (int i = 0; i < colors.count; i++) {
self.cgcolors[i] = (id)colors[i].CGColor;
}
self.gradient.colors = self.cgcolors;
self.gradient.locations = @[@0, @1];

self.gradientView = [[UIView alloc] init];
[self.gradientView.layer addSublayer:self.gradient];
self.gradientView.maskView = self.titleLabel;
// CGPoint gradientFrameAnchor = CGPointMake(CGRectGetMinX(self.gradientView.frame), CGRectGetMaxY(self.gradientView.frame));
// self.gradientView.layer.anchorPoint = CGPointMake(0, 1);
// self.gradientView.layer.position = gradientFrameAnchor;

// Animations
self.animations = [NSMutableArray new];

CABasicAnimation *locations = [CABasicAnimation animationWithKeyPath:@"locations"];
locations.fromValue = @[@0, @0];
locations.toValue = @[@0, @1];
locations.autoreverses = YES;
locations.repeatCount = FLT_MAX;
locations.duration = 5.0;
[self.animations addObject:locations];

CABasicAnimation *opacity = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacity.fromValue = @.75;
opacity.toValue = @1;
opacity.autoreverses = YES;
opacity.repeatCount = FLT_MAX;
opacity.duration = 3.0;
[self.animations addObject:opacity];

// Stack
UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:@[self.gradientView, self.subtitleLabel]];
stackView.alignment = UIStackViewAlignmentLeading;
stackView.axis = UILayoutConstraintAxisVertical;
stackView.distribution = UIStackViewDistributionEqualCentering;
stackView.spacing = 0;
stackView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:stackView];

[NSLayoutConstraint activateConstraints:@[
Copy link
Owner

@LacertosusRepo LacertosusRepo Oct 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some ideas here about the constraints:

The stackview wont be centered on the X axis with the current constraints. All edges should be anchored or it could be centered with both centerX/centerY anchors.

Using edge anchors would also allow you to get rid of the heightAnchor by constraining the top anchor of the stackview to the title label's gradient's top anchor and the bottom anchor to the subtitle's bottom anchor:

[self.stackView.topAnchor constraintEqualToAnchor:self.gradientView.topAnchor],
[self.stackView.bottomAnchor constraintEqualToAnchor:self.titleLabel.bottomAnchor],

The gradient's height/width anchors should just be equal to the title label's anchors: I see that the label is used as the mask.

EDIT: Fixed some incorrect suggestions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the first part, the stack view isn't aimed to be centered on X, its left padding with a small indent of 25 (to leave some space and not stick it to the left border) looks great. In my opinion if the end user/developer wants it centered it's easily doable, but it's not the aim of my implementation.

For your edge anchors suggestion, it should work too, but I'm not sure if it'd break the gradient or the scrolling effect I made. Also I may add that it works great as-is, thus I don't get the point of changing that.

If you're really sure that it won't break the current look & feel of the banner, and that a change might be better in terms of performance or code practices, I'm open to any change. But honestly I don't see why anything should be changed on constraints' side with the current implementation.

[stackView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:25],
self.stackCenter = [stackView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor constant:0],
[stackView.heightAnchor constraintEqualToConstant:self.titleLabel.frame.size.height + self.subtitleLabel.frame.size.height],

[self.gradientView.widthAnchor constraintEqualToConstant:self.titleLabel.frame.size.width],
[self.gradientView.heightAnchor constraintEqualToConstant:self.titleLabel.frame.size.height]
]];

self.defaultOffset = defaultOffset;
}

return self;
}

- (void)setActivateGradient:(BOOL)activated {
_activateGradient = activated;

// I would like to animate transition between activated and not but nvm
if (activated) {
self.gradient.colors = self.cgcolors;
for (CAAnimation *animation in self.animations) {
[self.gradient addAnimation:animation forKey:nil];
}
} else {
if (@available(iOS 13.0, *)) {
self.gradient.colors = @[(id)[UIColor labelColor].CGColor, (id)[UIColor labelColor].CGColor];
} else {
self.gradient.colors = @[(id)[UIColor blackColor].CGColor, (id)[UIColor blackColor].CGColor];
}
[self.gradient removeAllAnimations];
}
}

- (void)adjustStackPositionToScrollOffset:(CGFloat)offset {
// Smooth scroll effect
if (offset < self.defaultOffset) {
CGFloat space = self.defaultOffset - offset;
self.stackCenter.constant = -space / 2;
// This + comments above are to make the text a bit bigger on scrolling
// pretty much like the stock iOS behavior with big titles but I can't
// manage to make it look good so I gave up
/* if (space < 100) {
CGFloat titleScale = 1 + (space / 750);
CGFloat subtitleScale = 1 + (space / 1000);
self.titleLabel.transform = CGAffineTransformMakeScale(titleScale, titleScale);
self.subtitleLabel.transform = CGAffineTransformMakeScale(subtitleScale, subtitleScale);
} */
}
}

@end
Binary file added Animated Banner/images/animated.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Animated Banner/images/light_and_dark.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Animated Banner/images/showcase.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Animated Banner/images/togglable.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 10 additions & 3 deletions Animated Title View/XXXAnimatedTitleView.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#import <UIKit/UIKit.h>

@interface LBMAnimatedTitleView : UIView
-(instancetype)initWithTitle:(NSString *)title minimumScrollOffsetRequired:(CGFloat)minimumOffset;
-(void)adjustLabelPositionToScrollOffset:(CGFloat)offset;
@interface XXXAnimatedTitleView : UIView {
NSLayoutConstraint *labelYConstraint;
NSLayoutConstraint *iconYConstraint;
CGFloat minimumOffsetRequired;
}
@property (nonatomic, strong, readonly) UIImageView *iconImageView;
@property (nonatomic, strong, readonly) UILabel *titleLabel;
- (instancetype)initWithTitle:(NSString *)title minimumScrollOffsetRequired:(CGFloat)minimumOffset;
- (instancetype)initWithTitle:(NSString *)title image:(_Nullable UIImage *)image minimumScrollOffsetRequired:(CGFloat)minimumOffset;
- (void)adjustItemsPositionToScrollOffset:(CGFloat)offset;
@end
100 changes: 55 additions & 45 deletions Animated Title View/XXXAnimatedTitleView.m
Original file line number Diff line number Diff line change
@@ -1,50 +1,60 @@
#import "XXXAnimatedTitleView.h"

@implementation XXXAnimatedTitleView {
UILabel *_titleLabel;
NSLayoutConstraint *_yConstraint;
CGFloat _minimumOffsetRequired;
@implementation XXXAnimatedTitleView

- (instancetype)initWithTitle:(NSString *)title minimumScrollOffsetRequired:(CGFloat)minimumOffset {
return [self initWithTitle:title image:nil minimumScrollOffsetRequired:minimumOffset];
}

- (instancetype)initWithTitle:(NSString *)title image:(_Nullable UIImage *)image minimumScrollOffsetRequired:(CGFloat)minimumOffset {
if (self = [super init]) {
self.superview.clipsToBounds = YES;

_titleLabel = [[UILabel alloc] init];
_titleLabel.text = title;
_titleLabel.textAlignment = NSTextAlignmentCenter;
if (@available(iOS 13.0, *)) {
_titleLabel.textColor = [UIColor labelColor];
} else {
_titleLabel.textColor = [UIColor blackColor];
}
_titleLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize] weight:UIFontWeightSemibold];
_titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
[_titleLabel sizeToFit];

_iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_iconImageView.image = image;
_iconImageView.contentMode = UIViewContentModeScaleAspectFit;
_iconImageView.translatesAutoresizingMaskIntoConstraints = NO;

[self addSubview:_titleLabel];
[self addSubview:_iconImageView];

[NSLayoutConstraint activateConstraints:@[
[self.widthAnchor constraintEqualToAnchor:_titleLabel.widthAnchor],
[self.heightAnchor constraintEqualToAnchor:_titleLabel.heightAnchor],
[_titleLabel.centerXAnchor constraintEqualToAnchor:self.centerXAnchor],
labelYConstraint = [_titleLabel.centerYAnchor constraintEqualToAnchor:self.centerYAnchor constant:50],

[_iconImageView.centerXAnchor constraintEqualToAnchor:self.centerXAnchor],
iconYConstraint = [_iconImageView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor constant:0]
]];

minimumOffsetRequired = minimumOffset;
}

return self;
}

-(instancetype)initWithTitle:(NSString *)title minimumScrollOffsetRequired:(CGFloat)minimumOffset {
if([super init]) {
self.superview.clipsToBounds = YES;

_titleLabel = [[UILabel alloc] init];
_titleLabel.text = title;
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightHeavy];
_titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
[_titleLabel sizeToFit];

[self addSubview:_titleLabel];

[NSLayoutConstraint activateConstraints:@[
[self.widthAnchor constraintEqualToAnchor:_titleLabel.widthAnchor],
[self.heightAnchor constraintEqualToAnchor:_titleLabel.heightAnchor],

[_titleLabel.centerXAnchor constraintEqualToAnchor:self.centerXAnchor],
_yConstraint = [_titleLabel.centerYAnchor constraintEqualToAnchor:self.centerYAnchor constant:100],
]];

_minimumOffsetRequired = minimumOffset;
}

return self;
}

-(void)adjustLabelPositionToScrollOffset:(CGFloat)offset {
CGFloat adjustment = 100 - (offset - _minimumOffsetRequired);
if(offset >= _minimumOffsetRequired) {
if(adjustment <= 0) {
_yConstraint.constant = 0;
} else {
_yConstraint.constant = adjustment;
}

} else {
_yConstraint.constant = -100;
}
}
- (void)adjustItemsPositionToScrollOffset:(CGFloat)offset {
CGFloat adjustment = 50 - (offset - minimumOffsetRequired);
if (offset >= minimumOffsetRequired) {
labelYConstraint.constant = adjustment <= 0 ? 0 : adjustment;
iconYConstraint.constant = adjustment <= 40 ? adjustment <= -10 ? -50 : adjustment - 40 : 0; // don't ask me i don't know
} else {
labelYConstraint.constant = 50;
iconYConstraint.constant = 0;
}
_iconImageView.alpha = labelYConstraint.constant / 50;
}
@end
15 changes: 7 additions & 8 deletions Animated Title View/XXXRootListController.m
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
-(void)viewDidAppear:(BOOL)animated {
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

//Create view and set as titleView of your navigation bar
//Set the title and the minimum scroll offset before starting the animation
if(!self.navigationItem.titleView) {
// Create view and set as titleView of your navigation bar
// Set the title and the minimum scroll offset before starting the animation
if (!self.navigationItem.titleView) {
LBMAnimatedTitleView *titleView = [[LBMAnimatedTitleView alloc] initWithTitle:@"Title" minimumScrollOffsetRequired:100];
self.navigationItem.titleView = titleView;
}
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

//Send scroll offset updates to view
if([self.navigationItem.titleView respondsToSelector:@selector(adjustLabelPositionToScrollOffset:)]) {
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Send scroll offset updates to view
if ([self.navigationItem.titleView respondsToSelector:@selector(adjustLabelPositionToScrollOffset:)]) {
[(XXXAnimatedTitleView *)self.navigationItem.titleView adjustLabelPositionToScrollOffset:scrollView.contentOffset.y];
}
}
Binary file added Animated Title View/with_icon.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading