Skip to content

Commit

Permalink
Merge branch '0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
TomKnig committed Nov 23, 2014
2 parents 3bb1a66 + 4a7edc1 commit 6ee9891
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 17 deletions.
91 changes: 76 additions & 15 deletions Classes/TOMSMorphingLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@

#define kTOMSKernFactorAttributeName @"kTOMSKernFactorAttributeName"

/** A helper class which is used to break the strong reference cycle
* between CADisplayLink and TOMSMorphingLabel. Does nothing significant,
* just forwards all messages to its morphingLabel property.
*/
@interface TOMSMorphingLabelWeakWrapper : NSObject
@property (nonatomic, weak, readonly) TOMSMorphingLabel *morphingLabel;

- (instancetype)initWithLabel:(TOMSMorphingLabel *)label;

@end

@interface TOMSMorphingLabel (){
void (^_setTextCompletionBlock)(void);
}
Expand Down Expand Up @@ -78,16 +89,20 @@ - (void)designatedInitialization
_morphingEnabled = YES;
self.animating = NO;

self.displayLink = [CADisplayLink displayLinkWithTarget:self
selector:@selector(tickInitial)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop]
forMode:NSRunLoopCommonModes];
[self setupDisplayLinkWithSelector:@selector(tickInitial)];

self.animationDuration = 0.37;
self.characterAnimationOffset = 0.25;
self.characterShrinkFactor = 4;
}


- (void)dealloc
{
[_displayLink invalidate];
_displayLink = nil;
}

#pragma mark - Setters

- (void)numberOfAttributionStagesShouldChange
Expand Down Expand Up @@ -144,18 +159,34 @@ - (void)tickInitial
self.displayLink.paused = YES;
CFTimeInterval duration = self.displayLink.duration;

self.displayLink = [CADisplayLink displayLinkWithTarget:self
selector:@selector(tickMorphing)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop]
forMode:NSRunLoopCommonModes];
[self setupDisplayLinkWithSelector: @selector(tickMorphing)];
self.displayLink.paused = YES;

self.displayLinkDuration = duration;
}
}


- (void)setupDisplayLinkWithSelector:(SEL)selector
{
TOMSMorphingLabelWeakWrapper *displayLinkTarget = [[TOMSMorphingLabelWeakWrapper alloc] initWithLabel:self];
CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:displayLinkTarget
selector:selector];

[displayLink addToRunLoop:[NSRunLoop currentRunLoop]
forMode:NSRunLoopCommonModes];

[self.displayLink invalidate];
self.displayLink = displayLink;
}

#pragma mark - Getters

- (BOOL)shouldAnimateSettingText
{
return self.isMorphingEnabled && [UIView areAnimationsEnabled];
}

- (CGFloat)easedValue:(CGFloat)p
{
if (p < 0.5f) {
Expand Down Expand Up @@ -258,13 +289,14 @@ - (void)setText:(NSString *)text

- (void)setText:(NSString*) text withCompletionBlock:(void (^)(void))block{
_setTextCompletionBlock = block;
if (self.isMorphingEnabled) {
if ([self shouldAnimateSettingText]) {
self.nextText = text ? text : @"";
if (self.displayLinkDuration > 0) {
[self beginMorphing];
}
} else {
super.text = text;
[self textDidChange];
}
}

Expand Down Expand Up @@ -405,18 +437,22 @@ - (void)endMorphing
self.animating = NO;
if (self.nextText) {
[self beginMorphing];
}
else{
if(_setTextCompletionBlock != nil){
_setTextCompletionBlock();
_setTextCompletionBlock = nil;
}
} else {
[self textDidChange];
}
}
});
});
}

- (void)textDidChange
{
if(_setTextCompletionBlock != nil){
_setTextCompletionBlock();
_setTextCompletionBlock = nil;
}
}

- (void)tickMorphing
{
@synchronized (self) {
Expand All @@ -431,3 +467,28 @@ - (void)tickMorphing
}

@end

@implementation TOMSMorphingLabelWeakWrapper

- (instancetype)initWithLabel:(TOMSMorphingLabel *)label
{
self = [super init];

if (self) {
_morphingLabel = label;
}

return self;
}

- (void)tickInitial
{
[self.morphingLabel tickInitial];
}

- (void)tickMorphing
{
[self.morphingLabel tickMorphing];
}

@end
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ it, simply add the following line to your Podfile:

```ruby
platform :ios, '7.0'
pod "TOMSMorphingLabel", "~> 0.2.5"
pod "TOMSMorphingLabel", "~> 0.5"
```

## Usage
Expand Down Expand Up @@ -70,6 +70,11 @@ The configureable properties are defined as follows:
## Changelog
#### 0.5.0
* broke a strong reference cycle between `CADisplayLink` and `TOMSMorphingLabel`
* added invokation of the completion block when setting text without animations
* respect the global `[UIView areAnimationsEnabled]` state
#### 0.2.5
* fixed a textColor glitch
* introduced `setText:withCompletionBlock:`
Expand Down Expand Up @@ -99,6 +104,9 @@ The best way to contribute is by submitting a pull request or a [new Github issu
* @itouch2 fixed a bug that caused a crash when setting text to nil in version 0.2.3
* @waynehartman fixed text color-change glitch in version 0.2.5
* @cyril94440 added `setText:withCompletionBlock:` in version 0.2.5
* @wanderwaltz broke a strong reference cycle between `CADisplayLink` and `TOMSMorphingLabel` in version 0.5.0
* @wanderwaltz added invokation of the completion block when setting text without animations in version 0.5.0
* @wanderwaltz made `TOMSMorphingLabel` respect the global `[UIView areAnimationsEnabled]` state in version 0.5.0
## Author
Expand Down
2 changes: 1 addition & 1 deletion TOMSMorphingLabel.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "TOMSMorphingLabel"
s.version = "0.2.5"
s.version = "0.5.0"
s.summary = "Configurable morphing transitions between text values of a label."
s.homepage = "https://github.com/TomKnig/TOMSMorphingLabel"
s.license = 'MIT'
Expand Down

0 comments on commit 6ee9891

Please sign in to comment.