Skip to content

Commit

Permalink
• Fixed PMTweenUnit and PMTweenPhysicsUnit failing to tween object pr…
Browse files Browse the repository at this point in the history
…operties specified by a single-element keyPath.

• Fixed NSNumber object properties with a nil value not tweening. Such properties are now initialized with the specified starting value during the init method.
• Changed all init methods to return instancetype instead of id.
• More tests.
  • Loading branch information
poetmountain committed Jun 3, 2014
1 parent 7fa4a04 commit 7d5410d
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Classes/PMTweenBeat.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
* @return A new instance of this class
*/
- (id)initWithTempo:(PMTweenTempo *)tempo;
- (instancetype)initWithTempo:(PMTweenTempo *)tempo;


///-------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Classes/PMTweenGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @warning A NSInternalInconsistencyException will be raised if the provided array contains an object which does not adopt the `PMTweening` protocol.
*
*/
- (id)initWithTweens:(NSArray *)tweens options:(PMTweenOptions)options;
- (instancetype)initWithTweens:(NSArray *)tweens options:(PMTweenOptions)options;


///-------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions Classes/PMTweenPhysicsUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
*
* @see initWithObject:propertyKeyPath:startingValue:endingValue:duration:options:easingBlock:
*/
- (id)initWithProperty:(NSValue *)property
- (instancetype)initWithProperty:(NSValue *)property
startingValue:(double)startingValue
velocity:(double)velocity
friction:(double)friction
Expand All @@ -66,7 +66,7 @@
*
* @see initWithProperty:startingValue:endingValue:duration:options:easingBlock:, structValueUpdater
*/
- (id)initWithObject:(NSObject *)object
- (instancetype)initWithObject:(NSObject *)object
propertyKeyPath:(NSString *)propertyKeyPath
startingValue:(double)startingValue
velocity:(double)velocity
Expand Down
16 changes: 12 additions & 4 deletions Classes/PMTweenPhysicsUnit.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,19 @@ - (id)initWithObject:(NSObject *)object propertyKeyPath:(NSString *)propertyKeyP
}

} else {
// this is a top-level property, so let's see if this property is updatable
BOOL is_value_supported = NO;
if (self.targetObject && [_structValueUpdater replaceObject:self.targetObject newPropertyValue:1 propertyKeyPath:propertyKeyPath]) {
id prop_value = [object valueForKeyPath:propertyKeyPath];
if (prop_value && [_structValueUpdater replaceObject:prop_value newPropertyValue:1 propertyKeyPath:propertyKeyPath]) {
is_value_supported = YES;
}

if (self.targetObject && is_value_supported) {
_targetProperty = self.targetObject;
if (prop_value && is_value_supported) {
_targetProperty = prop_value;
} else {
// target property's value could be nil if it's a NSNumber, so set it to the starting value
self.targetProperty = @(_startingValue);
}

}


Expand Down Expand Up @@ -210,6 +215,9 @@ - (void)setupTweenForProperty:(NSObject *)property startingValue:(double)startin

self.tempo = [PMTweenCATempo tempo];

// set initial value
[self updatePropertyValue];

}


Expand Down
2 changes: 1 addition & 1 deletion Classes/PMTweenSequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @warning A NSInternalInconsistencyException will be raised if the provided array contains an object which does not adopt the `PMTweening` protocol.
*
*/
- (id)initWithSequenceSteps:(NSArray *)sequenceSteps options:(PMTweenOptions)options;
- (instancetype)initWithSequenceSteps:(NSArray *)sequenceSteps options:(PMTweenOptions)options;


///-------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions Classes/PMTweenUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
*
* @see initWithObject:propertyKeyPath:startingValue:endingValue:duration:options:easingBlock:
*/
- (id)initWithProperty:(NSValue *)property
- (instancetype)initWithProperty:(NSValue *)property
startingValue:(double)startingValue
endingValue:(double)endingValue
duration:(NSTimeInterval)duration
Expand All @@ -69,7 +69,7 @@
*
* @see initWithProperty:startingValue:endingValue:duration:options:easingBlock:, structValueUpdater
*/
- (id)initWithObject:(NSObject *)object
- (instancetype)initWithObject:(NSObject *)object
propertyKeyPath:(NSString *)propertyKeyPath
startingValue:(double)startingValue
endingValue:(double)endingValue
Expand Down
20 changes: 13 additions & 7 deletions Classes/PMTweenUnit.m
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,21 @@ - (id)initWithObject:(NSObject *)object propertyKeyPath:(NSString *)propertyKeyP
}

} else {
// this is a top-level property, so let's see if this property is updatable
BOOL is_value_supported = NO;
if (self.targetObject && [_structValueUpdater replaceObject:self.targetObject newPropertyValue:1 propertyKeyPath:propertyKeyPath]) {
id prop_value = [object valueForKeyPath:propertyKeyPath];
if (prop_value && [_structValueUpdater replaceObject:prop_value newPropertyValue:1 propertyKeyPath:propertyKeyPath]) {
is_value_supported = YES;
}

if (self.targetObject && is_value_supported) {
_targetProperty = self.targetObject;
if (prop_value && is_value_supported) {
_targetProperty = prop_value;
} else {
// target property's value could be nil if it's a NSNumber, so set it to the starting value
self.targetProperty = @(_startingValue);
}

}



[self setupTweenForProperty:_targetProperty startingValue:startingValue endingValue:endingValue duration:duration options:options easingBlock:easingBlock];

}
Expand Down Expand Up @@ -213,14 +216,17 @@ - (void)setupTweenForProperty:(NSObject *)property startingValue:(double)startin
_currentTime = 0;

self.tempo = [PMTweenCATempo tempo];

// set initial value
[self updatePropertyValue];

}


#pragma mark - Tween methods

- (void)updatePropertyValue {

if ([_targetProperty isKindOfClass:[NSNumber class]]) {
self.targetProperty = @(_currentValue);

Expand Down
2 changes: 1 addition & 1 deletion PMTween.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'PMTween'
s.version = '1.1.0'
s.version = '1.1.1'
s.license = { :type => 'MIT' }
s.summary = 'An elegant and flexible tweening library for iOS.'
s.platform = :ios
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ PMTweenSequence *sequence = [[PMTweenSequence alloc] initWithSequenceSteps:@[twe
</tr>
<tr>
<td><a href="https://poetmountain.github.io/PMTween/Classes/PMTweenPhysicsUnit.html">PMTweenPhysicsUnit</a></td>
<td>PMTweenUnit handles a single tween operation on an NSValue, using a physics system to update a value with decaying velocity.</td>
<td>PMTweenPhysicsUnit handles a single physics-based tween operation on an NSValue, using a physics system to update a value with decaying velocity.</td>
</tr>
<tr>
<td><a href="https://poetmountain.github.io/PMTween/Classes/PMTweenGroup.html">PMTweenGroup</a></td>
Expand Down
6 changes: 6 additions & 0 deletions Tests/PMTweenTests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
4EE309368C8C419195482A1D /* libPods-PMTweenTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FAD88554FF8E4015A15B4FA4 /* libPods-PMTweenTests.a */; };
8B74931B193D4EB500358D62 /* TestObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B74931A193D4EB500358D62 /* TestObject.m */; };
8B8901DF18FE17C4000507D9 /* PMTweenCATempo.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B8901DE18FE17C4000507D9 /* PMTweenCATempo.m */; };
8B9E609618F52A07002B553F /* PMTweenGroupSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B9E609518F52A07002B553F /* PMTweenGroupSpec.m */; };
8B9E609918F52AE8002B553F /* PMTweenGroup.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B9E609818F52AE8002B553F /* PMTweenGroup.m */; };
Expand Down Expand Up @@ -44,6 +45,8 @@

/* Begin PBXFileReference section */
2D2FBFF7633B42D68F134D3D /* Pods-PMTweenTests.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PMTweenTests.xcconfig"; path = "Pods/Pods-PMTweenTests.xcconfig"; sourceTree = "<group>"; };
8B749319193D4EB500358D62 /* TestObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TestObject.h; path = Classes/TestObject.h; sourceTree = "<group>"; };
8B74931A193D4EB500358D62 /* TestObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TestObject.m; path = Classes/TestObject.m; sourceTree = "<group>"; };
8B8901DD18FE17C4000507D9 /* PMTweenCATempo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PMTweenCATempo.h; path = ../../Classes/PMTweenCATempo.h; sourceTree = "<group>"; };
8B8901DE18FE17C4000507D9 /* PMTweenCATempo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PMTweenCATempo.m; path = ../../Classes/PMTweenCATempo.m; sourceTree = "<group>"; };
8B9E609518F52A07002B553F /* PMTweenGroupSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PMTweenGroupSpec.m; path = PMTweenTests/Specs/PMTweenGroupSpec.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -166,6 +169,8 @@
8BCE194518EE3EAC0062B01A /* PMTweenTests */ = {
isa = PBXGroup;
children = (
8B749319193D4EB500358D62 /* TestObject.h */,
8B74931A193D4EB500358D62 /* TestObject.m */,
8BCE196818EE42070062B01A /* Classes */,
8BCE195118EE3EE40062B01A /* Specs */,
8BCE194618EE3EAC0062B01A /* Supporting Files */,
Expand Down Expand Up @@ -354,6 +359,7 @@
8B8901DF18FE17C4000507D9 /* PMTweenCATempo.m in Sources */,
8BCB5EFD190A580A006D50EF /* PMTweenObjectUpdater.m in Sources */,
8BF8F7991916FBB50028261E /* PMTweenPhysicsUnit.m in Sources */,
8B74931B193D4EB500358D62 /* TestObject.m in Sources */,
8BCE196418EE3FCA0062B01A /* PMTweenUnitSpec.m in Sources */,
8BF8F7931916FB860028261E /* PMTweenPhysicsUnitSpec.m in Sources */,
8BEFDF3018FBCACB004ACD85 /* PMTweenEasingBounce.m in Sources */,
Expand Down
15 changes: 15 additions & 0 deletions Tests/PMTweenTests/Classes/TestObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// TestObject.h
// PMTween
//
// Created by Brett Walker on 6/2/14.
// Copyright (c) 2014 Poet & Mountain, LLC. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface TestObject : NSObject

@property (nonatomic, strong) NSNumber *testProp;

@end
13 changes: 13 additions & 0 deletions Tests/PMTweenTests/Classes/TestObject.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// TestObject.m
// PMTween
//
// Created by Brett Walker on 6/2/14.
// Copyright (c) 2014 Poet & Mountain, LLC. All rights reserved.
//

#import "TestObject.h"

@implementation TestObject

@end
14 changes: 14 additions & 0 deletions Tests/PMTweenTests/Specs/PMTweenObjectUpdaterSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
__block NSObject *old_value;
__block NSObject *new_value;

describe(@", with a CGFloat", ^{
before(^{
PMTweenObjectUpdater *updater = [PMTweenObjectUpdater updater];
old_value = @0.0;
new_value = [updater replaceObject:old_value newPropertyValue:0.5 propertyKeyPath:@"alpha"];
});

it(@", should update", ^{
CGFloat new_float = (CGFloat)[(NSNumber *)new_value floatValue];
expect(new_float).to.equal(0.5);
});

});

describe(@", with a CGPoint", ^{
before(^{
PMTweenObjectUpdater *updater = [PMTweenObjectUpdater updater];
Expand Down
49 changes: 37 additions & 12 deletions Tests/PMTweenTests/Specs/PMTweenPhysicsUnitSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,47 @@
describe(@"using initWithObject:...", ^{
__block UIView *view;

before(^{
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
unit = [[PMTweenPhysicsUnit alloc] initWithObject:view propertyKeyPath:@"frame.origin.x" startingValue:0 velocity:1 friction:0.998 options:PMTweenOptionNone];

describe(@"single-level property", ^{
before(^{
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
unit = [[PMTweenPhysicsUnit alloc] initWithObject:view propertyKeyPath:@"alpha" startingValue:0 velocity:1 friction:0.998 options:PMTweenOptionNone];
});

it(@"should end on specified ending value", ^AsyncBlock{
unit.completeBlock = ^void(NSObject<PMTweening> *tween) {
__strong PMTweenPhysicsUnit *physics_unit = (PMTweenPhysicsUnit *)tween;
expect(physics_unit.velocity).to.beCloseToWithin(0.0, 0.1);
expect(physics_unit.tweenProgress).to.equal(1.0);
expect(view.alpha).to.equal(physics_unit.currentValue);
done();
};
[unit startTween];

});
});

it(@"should end on specified ending value", ^AsyncBlock{
unit.completeBlock = ^void(NSObject<PMTweening> *tween) {
__strong PMTweenPhysicsUnit *physics_unit = (PMTweenPhysicsUnit *)tween;
expect(physics_unit.velocity).to.beCloseToWithin(0.0, 0.1);
expect(physics_unit.tweenProgress).to.equal(1.0);
expect(view.frame.origin.x).to.equal(physics_unit.currentValue);
done();
};
[unit startTween];

describe(@"nested struct", ^{
before(^{
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
unit = [[PMTweenPhysicsUnit alloc] initWithObject:view propertyKeyPath:@"frame.origin.x" startingValue:0 velocity:1 friction:0.998 options:PMTweenOptionNone];
});

it(@"should end on specified ending value", ^AsyncBlock{
unit.completeBlock = ^void(NSObject<PMTweening> *tween) {
__strong PMTweenPhysicsUnit *physics_unit = (PMTweenPhysicsUnit *)tween;
expect(physics_unit.velocity).to.beCloseToWithin(0.0, 0.1);
expect(physics_unit.tweenProgress).to.equal(1.0);
expect(view.frame.origin.x).to.equal(physics_unit.currentValue);
done();
};
[unit startTween];

});
});


});

});
Expand Down
Loading

0 comments on commit 7d5410d

Please sign in to comment.