├── TestPop ├── Untitled1.gif ├── Untitled2.gif ├── Untitled3.gif ├── TestPop │ ├── en.lproj │ │ └── InfoPlist.strings │ ├── pop │ │ ├── en.lproj │ │ │ └── InfoPlist.strings │ │ ├── pop-Prefix.pch │ │ ├── POPDefines.h │ │ ├── POPAnimationPrivate.h │ │ ├── POP.h │ │ ├── pop-Info.plist │ │ ├── POPAnimationEventInternal.h │ │ ├── POPGeometry.h │ │ ├── POPAnimator.h │ │ ├── POPCustomAnimation.mm │ │ ├── POPAnimationExtras.h │ │ ├── POPAction.h │ │ ├── POPGeometry.mm │ │ ├── POPAnimationTracer.h │ │ ├── POPAnimatorPrivate.h │ │ ├── POPDecayAnimation.h │ │ ├── POPCustomAnimation.h │ │ ├── POPMath.h │ │ ├── POPAnimationTracerInternal.h │ │ ├── POPAnimationEvent.h │ │ ├── POPCGUtils.h │ │ ├── POPCGUtils.mm │ │ ├── WebCore │ │ │ ├── FloatConversion.h │ │ │ ├── UnitBezier.h │ │ │ └── TransformationMatrix.h │ │ ├── POPBasicAnimation.h │ │ ├── POPMath.mm │ │ ├── POPPropertyAnimation.h │ │ ├── POPSpringAnimation.h │ │ ├── POPBasicAnimation.mm │ │ ├── POPBasicAnimationInternal.h │ │ ├── POPAnimationEvent.mm │ │ ├── POPAnimationRuntime.h │ │ ├── POPDecayAnimation.mm │ │ ├── POPPropertyAnimation.mm │ │ ├── POPSpringAnimationInternal.h │ │ ├── POPDecayAnimationInternal.h │ │ ├── POPAnimation.h │ │ ├── POPSpringAnimation.mm │ │ ├── POPAnimationExtras.mm │ │ ├── POPAnimatableProperty.h │ │ ├── POPAnimationTracer.mm │ │ ├── POPSpringSolver.h │ │ ├── POPLayerExtras.h │ │ ├── POPAnimation.mm │ │ ├── POPLayerExtras.mm │ │ ├── POPVector.mm │ │ ├── POPAnimationRuntime.mm │ │ └── POPPropertyAnimationInternal.h │ ├── AppDelegate.h │ ├── main.m │ ├── TestPop-Prefix.pch │ ├── Images.xcassets │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── LaunchImage.launchimage │ │ │ └── Contents.json │ ├── TestPop-Info.plist │ └── AppDelegate.m ├── Resource │ ├── menu.png │ ├── rexueriji.png │ └── chengxuyuan.png ├── TestPop.xcodeproj │ ├── xcuserdata │ │ └── xudongjin.xcuserdatad │ │ │ ├── xcdebugger │ │ │ └── Breakpoints_v2.xcbkptlist │ │ │ └── xcschemes │ │ │ ├── xcschememanagement.plist │ │ │ └── TestPop.xcscheme │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ ├── xcuserdata │ │ └── xudongjin.xcuserdatad │ │ │ ├── UserInterfaceState.xcuserstate │ │ │ └── WorkspaceSettings.xcsettings │ │ └── xcshareddata │ │ └── TestPop.xccheckout └── Controllers │ ├── POPBasicPOPViewAlpha.h │ ├── POPSpringPOPLayerSize.h │ ├── POPSpringPOPViewFrame.h │ ├── POPSpringPOPLayerPosition.h │ ├── ViewController.h │ ├── CombinePOPbox.h │ ├── CombinePOPbox.xib │ ├── POPBasicPOPViewAlpha.m │ ├── POPSpringPOPViewFrame.m │ ├── POPSpringPOPLayerPosition.m │ ├── POPSpringPOPLayerSize.m │ ├── POPSpringPOPLayerPosition.xib │ ├── POPBasicPOPViewAlpha.xib │ ├── POPSpringPOPLayerSize.xib │ ├── ViewController.xib │ ├── CombinePOPbox.m │ ├── POPSpringPOPViewFrame.xib │ └── ViewController.m ├── .gitignore └── README.md /TestPop/Untitled1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxd001/POPdemo/HEAD/TestPop/Untitled1.gif -------------------------------------------------------------------------------- /TestPop/Untitled2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxd001/POPdemo/HEAD/TestPop/Untitled2.gif -------------------------------------------------------------------------------- /TestPop/Untitled3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxd001/POPdemo/HEAD/TestPop/Untitled3.gif -------------------------------------------------------------------------------- /TestPop/TestPop/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /TestPop/Resource/menu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxd001/POPdemo/HEAD/TestPop/Resource/menu.png -------------------------------------------------------------------------------- /TestPop/TestPop/pop/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /TestPop/Resource/rexueriji.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxd001/POPdemo/HEAD/TestPop/Resource/rexueriji.png -------------------------------------------------------------------------------- /TestPop/Resource/chengxuyuan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxd001/POPdemo/HEAD/TestPop/Resource/chengxuyuan.png -------------------------------------------------------------------------------- /TestPop/TestPop.xcodeproj/xcuserdata/xudongjin.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/pop-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header for all source files of the 'POPAnimation' target in the 'POPAnimation' project 3 | // 4 | 5 | #ifdef __OBJC__ 6 | #import 7 | #endif 8 | -------------------------------------------------------------------------------- /TestPop/TestPop.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /TestPop/TestPop.xcodeproj/project.xcworkspace/xcuserdata/xudongjin.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxd001/POPdemo/HEAD/TestPop/TestPop.xcodeproj/project.xcworkspace/xcuserdata/xudongjin.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # CocoaPods 2 | # 3 | # We recommend against adding the Pods directory to your .gitignore. However 4 | # you should judge for yourself, the pros and cons are mentioned at: 5 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control? 6 | # 7 | # Pods/ 8 | 9 | -------------------------------------------------------------------------------- /TestPop/Controllers/POPBasicPOPViewAlpha.h: -------------------------------------------------------------------------------- 1 | // 2 | // POPBasicPOPViewAlpha.h 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-5-1. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface POPBasicPOPViewAlpha : UIViewController 12 | @property (weak, nonatomic) IBOutlet UIImageView *basicView; 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /TestPop/Controllers/POPSpringPOPLayerSize.h: -------------------------------------------------------------------------------- 1 | // 2 | // SpringAnimateCtrl.h 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-5-1. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface POPSpringPOPLayerSize : UIViewController 12 | 13 | @property (weak, nonatomic) IBOutlet UIImageView *springView; 14 | @end 15 | -------------------------------------------------------------------------------- /TestPop/Controllers/POPSpringPOPViewFrame.h: -------------------------------------------------------------------------------- 1 | // 2 | // POPSpringPOPViewFrame.h 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-5-1. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface POPSpringPOPViewFrame : UIViewController 12 | @property (weak, nonatomic) IBOutlet UIImageView *springView; 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /TestPop/Controllers/POPSpringPOPLayerPosition.h: -------------------------------------------------------------------------------- 1 | // 2 | // POPSpringPOPLayerPosition.h 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-5-1. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface POPSpringPOPLayerPosition : UIViewController 12 | @property (weak, nonatomic) IBOutlet UIView *springView; 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /TestPop/TestPop/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-4-29. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "ViewController.h" 11 | 12 | @interface AppDelegate : UIResponder 13 | 14 | @property (strong, nonatomic) UIWindow *window; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /TestPop/TestPop/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-4-29. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | #import "AppDelegate.h" 12 | 13 | int main(int argc, char * argv[]) 14 | { 15 | @autoreleasepool { 16 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /TestPop/TestPop/TestPop-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header 3 | // 4 | // The contents of this file are implicitly included at the beginning of every source file. 5 | // 6 | 7 | #import 8 | 9 | #ifndef __IPHONE_3_0 10 | #warning "This project uses features only available in iOS SDK 3.0 and later." 11 | #endif 12 | 13 | #ifdef __OBJC__ 14 | #import 15 | #import 16 | #import "POP.h" 17 | #endif 18 | -------------------------------------------------------------------------------- /TestPop/TestPop.xcodeproj/project.xcworkspace/xcuserdata/xudongjin.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges 6 | 7 | SnapshotAutomaticallyBeforeSignificantChanges 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /TestPop/TestPop/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "40x40", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "60x60", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /TestPop/Controllers/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-5-1. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "POP.h" 11 | #import "POPSpringPOPLayerSize.h" 12 | #import "POPSpringPOPLayerPosition.h" 13 | #import "POPSpringPOPViewFrame.h" 14 | 15 | #import "POPBasicPOPViewAlpha.h" 16 | 17 | @interface ViewController : UIViewController 18 | 19 | @property (weak, nonatomic) IBOutlet UITableView *tableView; 20 | 21 | @end 22 | -------------------------------------------------------------------------------- /TestPop/Controllers/CombinePOPbox.h: -------------------------------------------------------------------------------- 1 | // 2 | // CombinePOPbox.h 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-5-2. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface CombinePOPbox : UIViewController 12 | 13 | @property (strong, nonatomic) UIImageView *popView; 14 | 15 | @property (assign, nonatomic) CGRect showPosition; 16 | 17 | @property (assign, nonatomic) CGRect hidePosition; 18 | 19 | @property (assign, nonatomic) BOOL isOpened; 20 | 21 | 22 | - (void)showPop; 23 | 24 | - (void)hidePop; 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /TestPop/TestPop/Images.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "iphone", 6 | "extent" : "full-screen", 7 | "minimum-system-version" : "7.0", 8 | "scale" : "2x" 9 | }, 10 | { 11 | "orientation" : "portrait", 12 | "idiom" : "iphone", 13 | "subtype" : "retina4", 14 | "extent" : "full-screen", 15 | "minimum-system-version" : "7.0", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /TestPop/TestPop.xcodeproj/xcuserdata/xudongjin.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | TestPop.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 4D233A95190FEB86004B35AC 16 | 17 | primary 18 | 19 | 20 | 4D233AB0190FEB86004B35AC 21 | 22 | primary 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPDefines.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #ifndef POP_POPDefines_h 11 | #define POP_POPDefines_h 12 | 13 | #ifdef __cplusplus 14 | # define POP_EXTERN_C_BEGIN extern "C" { 15 | # define POP_EXTERN_C_END } 16 | #else 17 | # define POP_EXTERN_C_BEGIN 18 | # define POP_EXTERN_C_END 19 | #endif 20 | 21 | #define POP_ARRAY_COUNT(x) sizeof(x) / sizeof(x[0]) 22 | 23 | #if defined (__cplusplus) && defined (__GNUC__) 24 | # define POP_NOTHROW __attribute__ ((nothrow)) 25 | #else 26 | # define POP_NOTHROW 27 | #endif 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimationPrivate.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPAnimation.h" 11 | 12 | #define POP_ANIMATION_FRICTION_FOR_QC_FRICTION(qcFriction) (25.0 + (((qcFriction - 8.0) / 2.0) * (25.0 - 19.0))) 13 | #define POP_ANIMATION_TENSION_FOR_QC_TENSION(qcTension) (194.0 + (((qcTension - 30.0) / 50.0) * (375.0 - 194.0))) 14 | 15 | #define QC_FRICTION_FOR_POP_ANIMATION_FRICTION(fbFriction) (8.0 + 2.0 * ((fbFriction - 25.0)/(25.0 - 19.0))) 16 | #define QC_TENSION_FOR_POP_ANIMATION_TENSION(fbTension) (30.0 + 50.0 * ((fbTension - 194.0)/(375.0 - 194.0))) 17 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POP.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #ifndef POP_POP_H 11 | #define POP_POP_H 12 | 13 | #import "POPAnimatableProperty.h" 14 | #import "POPAnimation.h" 15 | #import "POPAnimationEvent.h" 16 | #import "POPAnimationExtras.h" 17 | #import "POPAnimationTracer.h" 18 | #import "POPAnimator.h" 19 | #import "POPBasicAnimation.h" 20 | #import "POPCustomAnimation.h" 21 | #import "POPDecayAnimation.h" 22 | #import "POPDefines.h" 23 | #import "POPGeometry.h" 24 | #import "POPLayerExtras.h" 25 | #import "POPPropertyAnimation.h" 26 | #import "POPSpringAnimation.h" 27 | 28 | 29 | #endif /* POP_POP_H */ 30 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/pop-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | com.facebook.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | FMWK 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | NSHumanReadableCopyright 26 | Copyright © 2014 Facebook. All rights reserved. 27 | NSPrincipalClass 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimationEventInternal.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import 11 | 12 | #import "POPAnimationEvent.h" 13 | 14 | @interface POPAnimationEvent () 15 | 16 | /** 17 | @abstract Default initializer. 18 | */ 19 | - (instancetype)initWithType:(POPAnimationEventType)type time:(CFTimeInterval)time; 20 | 21 | /** 22 | @abstract Readwrite redefinition of public property. 23 | */ 24 | @property (readwrite, nonatomic, copy) NSString *animationDescription; 25 | 26 | @end 27 | 28 | @interface POPAnimationValueEvent () 29 | 30 | /** 31 | @abstract Default initializer. 32 | */ 33 | - (instancetype)initWithType:(POPAnimationEventType)type time:(CFTimeInterval)time value:(id)value; 34 | 35 | /** 36 | @abstract Readwrite redefinition of public property. 37 | */ 38 | @property (readwrite, nonatomic, strong) id velocity; 39 | 40 | @end 41 | 42 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPGeometry.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import 11 | 12 | #if TARGET_OS_IPHONE 13 | #import 14 | #endif 15 | 16 | #if !TARGET_OS_IPHONE 17 | 18 | /** NSValue extensions to support animatable types. */ 19 | @interface NSValue (POP) 20 | 21 | /** 22 | @abstract Creates an NSValue given a CGPoint. 23 | */ 24 | + (NSValue *)valueWithCGPoint:(CGPoint)point; 25 | 26 | /** 27 | @abstract Creates an NSValue given a CGSize. 28 | */ 29 | + (NSValue *)valueWithCGSize:(CGSize)size; 30 | 31 | /** 32 | @abstract Creates an NSValue given a CGRect. 33 | */ 34 | + (NSValue *)valueWithCGRect:(CGRect)rect; 35 | 36 | /** 37 | @abstract Returns the underlying CGPoint value. 38 | */ 39 | - (CGPoint)CGPointValue; 40 | 41 | /** 42 | @abstract Returns the underlying CGSize value. 43 | */ 44 | - (CGSize)CGSizeValue; 45 | 46 | /** 47 | @abstract Returns the underlying CGRect value. 48 | */ 49 | - (CGRect)CGRectValue; 50 | 51 | @end 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimator.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import 11 | 12 | @protocol POPAnimatorDelegate; 13 | 14 | /** 15 | @abstract The animator class renders animations. 16 | */ 17 | @interface POPAnimator : NSObject 18 | 19 | /** 20 | @abstract The shared animator instance. 21 | @discussion Consumers should generally use the shared instance in lieu of creating new instances. 22 | */ 23 | + (instancetype)sharedAnimator; 24 | 25 | /** 26 | @abstract The optional animator delegate. 27 | */ 28 | @property (weak, nonatomic) id delegate; 29 | 30 | @end 31 | 32 | /** 33 | @abstract The animator delegate. 34 | */ 35 | @protocol POPAnimatorDelegate 36 | 37 | /** 38 | @abstract Called on each frame before animation application. 39 | */ 40 | - (void)animatorWillAnimate:(POPAnimator *)animator; 41 | 42 | /** 43 | @abstract Called on each frame after animation application. 44 | */ 45 | - (void)animatorDidAnimate:(POPAnimator *)animator; 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /TestPop/TestPop/TestPop-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIdentifier 12 | com.zipingfang.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1.0 25 | LSRequiresIPhoneOS 26 | 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /TestPop/Controllers/CombinePOPbox.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPCustomAnimation.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPCustomAnimation.h" 11 | #import "POPAnimationInternal.h" 12 | 13 | @interface POPCustomAnimation () 14 | @property (nonatomic, copy) POPCustomAnimationBlock animate; 15 | @end 16 | 17 | @implementation POPCustomAnimation 18 | 19 | + (instancetype)animationWithBlock:(BOOL(^)(id target, POPCustomAnimation *))block 20 | { 21 | POPCustomAnimation *b = [[self alloc] _init]; 22 | b.animate = block; 23 | return b; 24 | } 25 | 26 | - (id)_init 27 | { 28 | self = [super _init]; 29 | if (nil != self) { 30 | _state->type = kPOPAnimationCustom; 31 | } 32 | return self; 33 | } 34 | 35 | - (CFTimeInterval)beginTime 36 | { 37 | POPAnimationState *s = POPAnimationGetState(self); 38 | return s->startTime > 0 ? s->startTime : s->beginTime; 39 | } 40 | 41 | - (BOOL)_advance:(id)object currentTime:(CFTimeInterval)currentTime elapsedTime:(CFTimeInterval)elapsedTime 42 | { 43 | _currentTime = currentTime; 44 | _elapsedTime = elapsedTime; 45 | return _animate(object, self); 46 | } 47 | 48 | - (void)_appendDescription:(NSMutableString *)s debug:(BOOL)debug 49 | { 50 | [s appendFormat:@"; elapsedTime = %f; currentTime = %f;", _elapsedTime, _currentTime]; 51 | } 52 | 53 | @end 54 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimationExtras.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import 11 | 12 | #import "POPDefines.h" 13 | #import "POPSpringAnimation.h" 14 | 15 | /** 16 | @abstract The current drag coefficient. 17 | @discussion A value greater than 1.0 indicates Simulator slow-motion animations are enabled. Defaults to 1.0. 18 | */ 19 | extern CGFloat POPAnimationDragCoefficient(); 20 | 21 | @interface CAAnimation (POPAnimationExtras) 22 | 23 | /** 24 | @abstract Apply the current drag coefficient to animation speed. 25 | @discussion Convenience utility to respect Simulator slow-motion animation settings. 26 | */ 27 | - (void)pop_applyDragCoefficient; 28 | 29 | @end 30 | 31 | @interface POPSpringAnimation (POPAnimationExtras) 32 | 33 | /** 34 | @abstract Converts from spring bounciness and speed to tension, friction and mass dynamics values. 35 | */ 36 | + (void)convertBounciness:(CGFloat)bounciness speed:(CGFloat)speed toTension:(CGFloat *)outTension friction:(CGFloat *)outFriction mass:(CGFloat *)outMass; 37 | 38 | /** 39 | @abstract Converts from dynamics tension, friction and mass to spring bounciness and speed values. 40 | */ 41 | + (void)convertTension:(CGFloat)tension friction:(CGFloat)friction toBounciness:(CGFloat *)outBounciness speed:(CGFloat *)outSpeed; 42 | 43 | @end 44 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAction.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #ifndef POPACTION_H 11 | #define POPACTION_H 12 | 13 | #import 14 | #import "POPDefines.h" 15 | 16 | #ifdef __cplusplus 17 | 18 | namespace POP { 19 | 20 | /** 21 | @abstract Disables Core Animation actions using RAII. 22 | @discussion The disablement of actions is scoped to the current transaction. 23 | */ 24 | class ActionDisabler 25 | { 26 | BOOL state; 27 | 28 | public: 29 | ActionDisabler() POP_NOTHROW 30 | { 31 | state = [CATransaction disableActions]; 32 | [CATransaction setDisableActions:YES]; 33 | } 34 | 35 | ~ActionDisabler() 36 | { 37 | [CATransaction setDisableActions:state]; 38 | } 39 | }; 40 | 41 | /** 42 | @abstract Enables Core Animation actions using RAII. 43 | @discussion The enablement of actions is scoped to the current transaction. 44 | */ 45 | class ActionEnabler 46 | { 47 | BOOL state; 48 | 49 | public: 50 | ActionEnabler() POP_NOTHROW 51 | { 52 | state = [CATransaction disableActions]; 53 | [CATransaction setDisableActions:NO]; 54 | } 55 | 56 | ~ActionEnabler() 57 | { 58 | [CATransaction setDisableActions:state]; 59 | } 60 | }; 61 | 62 | } 63 | 64 | #endif /* __cplusplus */ 65 | 66 | #endif /* POPACTION_H */ 67 | -------------------------------------------------------------------------------- /TestPop/TestPop.xcodeproj/project.xcworkspace/xcshareddata/TestPop.xccheckout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDESourceControlProjectFavoriteDictionaryKey 6 | 7 | IDESourceControlProjectIdentifier 8 | FD643AEA-B47B-48EB-835B-FBA7978C0AFD 9 | IDESourceControlProjectName 10 | TestPop 11 | IDESourceControlProjectOriginsDictionary 12 | 13 | 5250F991-C932-47F1-926A-7FE971E67122 14 | https://github.com/jxd001/POPdemo.git 15 | 16 | IDESourceControlProjectPath 17 | TestPop/TestPop.xcodeproj/project.xcworkspace 18 | IDESourceControlProjectRelativeInstallPathDictionary 19 | 20 | 5250F991-C932-47F1-926A-7FE971E67122 21 | ../../.. 22 | 23 | IDESourceControlProjectURL 24 | https://github.com/jxd001/POPdemo.git 25 | IDESourceControlProjectVersion 26 | 110 27 | IDESourceControlProjectWCCIdentifier 28 | 5250F991-C932-47F1-926A-7FE971E67122 29 | IDESourceControlProjectWCConfigurations 30 | 31 | 32 | IDESourceControlRepositoryExtensionIdentifierKey 33 | public.vcs.git 34 | IDESourceControlWCCIdentifierKey 35 | 5250F991-C932-47F1-926A-7FE971E67122 36 | IDESourceControlWCCName 37 | POPdemo 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /TestPop/Controllers/POPBasicPOPViewAlpha.m: -------------------------------------------------------------------------------- 1 | // 2 | // POPBasicPOPViewAlpha.m 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-5-1. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import "POPBasicPOPViewAlpha.h" 10 | 11 | @interface POPBasicPOPViewAlpha () 12 | 13 | @end 14 | 15 | @implementation POPBasicPOPViewAlpha 16 | 17 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 18 | { 19 | self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 20 | if (self) { 21 | // Custom initialization 22 | } 23 | return self; 24 | } 25 | 26 | - (void)viewDidLoad 27 | { 28 | [super viewDidLoad]; 29 | // Do any additional setup after loading the view from its nib. 30 | 31 | //添加手势 32 | UITapGestureRecognizer *gestureForSpring = [[UITapGestureRecognizer alloc] init]; 33 | [gestureForSpring addTarget:self action:@selector(changeAlpha:)]; 34 | [self.view addGestureRecognizer:gestureForSpring]; 35 | 36 | } 37 | 38 | - (void)changeAlpha:(UITapGestureRecognizer*)tap{ 39 | 40 | POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha]; 41 | 42 | anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 43 | 44 | float alpha = _basicView.alpha; 45 | if (alpha==1.0) { 46 | anim.toValue = @(0.0); 47 | } 48 | else{ 49 | anim.toValue = @(1.0); 50 | } 51 | 52 | [_basicView pop_addAnimation:anim forKey:@"alpha"]; 53 | 54 | } 55 | 56 | 57 | - (void)didReceiveMemoryWarning 58 | { 59 | [super didReceiveMemoryWarning]; 60 | // Dispose of any resources that can be recreated. 61 | } 62 | 63 | @end 64 | -------------------------------------------------------------------------------- /TestPop/Controllers/POPSpringPOPViewFrame.m: -------------------------------------------------------------------------------- 1 | // 2 | // POPSpringPOPViewFrame.m 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-5-1. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import "POPSpringPOPViewFrame.h" 10 | 11 | @interface POPSpringPOPViewFrame () 12 | 13 | @end 14 | 15 | @implementation POPSpringPOPViewFrame 16 | 17 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 18 | { 19 | self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 20 | if (self) { 21 | // Custom initialization 22 | } 23 | return self; 24 | } 25 | 26 | - (void)viewDidLoad 27 | { 28 | [super viewDidLoad]; 29 | // Do any additional setup after loading the view from its nib. 30 | //添加手势 31 | UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] init]; 32 | [gesture addTarget:self action:@selector(changeSize:)]; 33 | [self.view addGestureRecognizer:gesture]; 34 | 35 | } 36 | 37 | - (void)changeSize:(UIPanGestureRecognizer*)tap{ 38 | 39 | POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame]; 40 | 41 | CGPoint point = [tap locationInView:self.view]; 42 | 43 | springAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, point.x, point.y)]; 44 | 45 | 46 | //弹性值 47 | springAnimation.springBounciness = 20.0; 48 | //弹性速度 49 | springAnimation.springSpeed = 20.0; 50 | 51 | [_springView pop_addAnimation:springAnimation forKey:@"changeframe"]; 52 | 53 | } 54 | 55 | - (void)didReceiveMemoryWarning 56 | { 57 | [super didReceiveMemoryWarning]; 58 | // Dispose of any resources that can be recreated. 59 | } 60 | 61 | @end 62 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPGeometry.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPGeometry.h" 11 | 12 | #if !TARGET_OS_IPHONE 13 | @implementation NSValue (POP) 14 | 15 | + (NSValue *)valueWithCGPoint:(CGPoint)point { 16 | return [NSValue valueWithBytes:&point objCType:@encode(CGPoint)]; 17 | } 18 | 19 | + (NSValue *)valueWithCGSize:(CGSize)size { 20 | return [NSValue valueWithBytes:&size objCType:@encode(CGSize)]; 21 | } 22 | 23 | + (NSValue *)valueWithCGRect:(CGRect)rect { 24 | return [NSValue valueWithBytes:&rect objCType:@encode(CGRect)]; 25 | } 26 | 27 | + (NSValue *)valueWithCFRange:(CFRange)range { 28 | return [NSValue valueWithBytes:&range objCType:@encode(CFRange)]; 29 | } 30 | 31 | + (NSValue *)valueWithCGAffineTransform:(CGAffineTransform)transform 32 | { 33 | return [NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)]; 34 | } 35 | 36 | - (CGPoint)CGPointValue { 37 | CGPoint result; 38 | [self getValue:&result]; 39 | return result; 40 | } 41 | 42 | - (CGSize)CGSizeValue { 43 | CGSize result; 44 | [self getValue:&result]; 45 | return result; 46 | } 47 | 48 | - (CGRect)CGRectValue { 49 | CGRect result; 50 | [self getValue:&result]; 51 | return result; 52 | } 53 | 54 | - (CFRange)CFRangeValue { 55 | CFRange result; 56 | [self getValue:&result]; 57 | return result; 58 | } 59 | 60 | - (CGAffineTransform)CGAffineTransformValue { 61 | CGAffineTransform result; 62 | [self getValue:&result]; 63 | return result; 64 | } 65 | @end 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimationTracer.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import 11 | 12 | #import "POPAnimationEvent.h" 13 | 14 | @class POPAnimation; 15 | 16 | /** 17 | @abstract Tracer of animation events to fasciliate unit testing & debugging. 18 | */ 19 | @interface POPAnimationTracer : NSObject 20 | 21 | /** 22 | @abstract Start recording events. 23 | */ 24 | - (void)start; 25 | 26 | /** 27 | @abstract Stop recording events. 28 | */ 29 | - (void)stop; 30 | 31 | /** 32 | @abstract Resets any recoded events. Continues recording events if already started. 33 | */ 34 | - (void)reset; 35 | 36 | /** 37 | @abstract Property representing all recorded events. 38 | @discussion Events are returned in order of occurence. 39 | */ 40 | @property (nonatomic, assign, readonly) NSArray *allEvents; 41 | 42 | /** 43 | @abstract Property representing all recorded write events for convenience. 44 | @discussion Events are returned in order of occurence. 45 | */ 46 | @property (nonatomic, assign, readonly) NSArray *writeEvents; 47 | 48 | /** 49 | @abstract Queries for events of specified type. 50 | @param type The type of event to return. 51 | @returns An array of events of specified type in order of occurence. 52 | */ 53 | - (NSArray *)eventsWithType:(POPAnimationEventType)type; 54 | 55 | /** 56 | @abstract Property indicating whether tracer should automatically log events and reset collection on animation completion. 57 | */ 58 | @property (nonatomic, assign) BOOL shouldLogAndResetOnCompletion; 59 | 60 | @end 61 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimatorPrivate.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPAnimator.h" 11 | 12 | @class POPAnimation; 13 | 14 | @protocol POPAnimatorObserving 15 | @required 16 | 17 | /** 18 | @abstract Called on each observer after animator has advanced. Core Animation actions are disabled by default. 19 | */ 20 | - (void)animatorDidAnimate:(POPAnimator *)animator; 21 | 22 | @end 23 | 24 | @interface POPAnimator () 25 | 26 | #if !TARGET_OS_PHONE 27 | /** 28 | Determines whether or not to use a high priority background thread for animation updates. Using a background thread can result in faster, more responsive updates, but may be less compatible. Defaults to YES. 29 | */ 30 | + (BOOL)disableBackgroundThread; 31 | + (void)setDisableBackgroundThread:(BOOL)flag; 32 | #endif 33 | 34 | /** 35 | Exposed for unit testing. 36 | */ 37 | - (void)renderTime:(CFTimeInterval)time; 38 | 39 | /** 40 | Funnel methods for category additions. 41 | */ 42 | - (void)addAnimation:(POPAnimation *)anim forObject:(id)obj key:(NSString *)key; 43 | - (void)removeAllAnimationsForObject:(id)obj; 44 | - (void)removeAnimationForObject:(id)obj key:(NSString *)key; 45 | - (NSArray *)animationKeysForObject:(id)obj; 46 | - (POPAnimation *)animationForObject:(id)obj key:(NSString *)key; 47 | 48 | /** 49 | @abstract Add an animator observer. Observer will be notified of each subsequent animator advance until removal. 50 | */ 51 | - (void)addObserver:(id)observer; 52 | 53 | /** 54 | @abstract Remove an animator observer. 55 | */ 56 | - (void)removeObserver:(id)observer; 57 | 58 | @end 59 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPDecayAnimation.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPPropertyAnimation.h" 11 | 12 | /** 13 | @abstract A concrete decay animation class. 14 | @discussion Animation is achieved through gradual decay of animation value. 15 | */ 16 | @interface POPDecayAnimation : POPPropertyAnimation 17 | 18 | /** 19 | @abstract The designated initializer. 20 | @returns An instance of a decay animation. 21 | */ 22 | + (instancetype)animation; 23 | 24 | /** 25 | @abstract Convenience initializer that returns an animation with animatable property of name. 26 | @param name The name of the animatable property. 27 | @returns An instance of a decay animation configured with specified animatable property. 28 | */ 29 | + (instancetype)animationWithPropertyNamed:(NSString *)name; 30 | 31 | /** 32 | @abstract The current velocity value. 33 | @discussion Set before animation start to account for initial velocity. Expressed in change of value units per second. 34 | */ 35 | @property (copy, nonatomic) id velocity; 36 | 37 | /** 38 | @abstract The deceleration factor. 39 | @discussion Values specifies should be in the range [0, 1]. Lower values results in faster deceleration. Defaults to 0.998. 40 | */ 41 | @property (assign, nonatomic) CGFloat deceleration; 42 | 43 | /** 44 | @abstract The expected duration. 45 | @discussion Derived based on input velocity and deceleration values. 46 | */ 47 | @property (readonly, assign, nonatomic) CFTimeInterval duration; 48 | 49 | /** 50 | The to value is derived based on input velocity and deceleration. 51 | */ 52 | - (void)setToValue:(id)toValue NS_UNAVAILABLE; 53 | 54 | @end 55 | -------------------------------------------------------------------------------- /TestPop/Controllers/POPSpringPOPLayerPosition.m: -------------------------------------------------------------------------------- 1 | // 2 | // POPSpringPOPLayerPosition.m 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-5-1. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import "POPSpringPOPLayerPosition.h" 10 | 11 | @interface POPSpringPOPLayerPosition () 12 | 13 | @end 14 | 15 | @implementation POPSpringPOPLayerPosition 16 | 17 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 18 | { 19 | self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 20 | if (self) { 21 | // Custom initialization 22 | } 23 | return self; 24 | } 25 | 26 | - (void)viewDidLoad 27 | { 28 | [super viewDidLoad]; 29 | // Do any additional setup after loading the view from its nib. 30 | 31 | //添加手势 32 | UITapGestureRecognizer *gestureForSpring = [[UITapGestureRecognizer alloc] init]; 33 | [gestureForSpring addTarget:self action:@selector(changePosition:)]; 34 | [self.view addGestureRecognizer:gestureForSpring]; 35 | } 36 | 37 | - (void)changePosition:(UITapGestureRecognizer*)tap{ 38 | 39 | POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPosition]; 40 | 41 | CGPoint point = _springView.center; 42 | 43 | if (point.y==240) { 44 | springAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(point.x, -230)]; 45 | } 46 | else{ 47 | springAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(point.x, 240)]; 48 | } 49 | 50 | //弹性值 51 | springAnimation.springBounciness = 20.0; 52 | //弹性速度 53 | springAnimation.springSpeed = 20.0; 54 | 55 | [_springView pop_addAnimation:springAnimation forKey:@"changeposition"]; 56 | 57 | } 58 | 59 | 60 | - (void)didReceiveMemoryWarning 61 | { 62 | [super didReceiveMemoryWarning]; 63 | // Dispose of any resources that can be recreated. 64 | } 65 | 66 | @end 67 | -------------------------------------------------------------------------------- /TestPop/Controllers/POPSpringPOPLayerSize.m: -------------------------------------------------------------------------------- 1 | // 2 | // SpringAnimateCtrl.m 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-5-1. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import "POPSpringPOPLayerSize.h" 10 | 11 | @interface POPSpringPOPLayerSize () 12 | 13 | @end 14 | 15 | @implementation POPSpringPOPLayerSize 16 | 17 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 18 | { 19 | self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 20 | if (self) { 21 | // Custom initialization 22 | } 23 | return self; 24 | } 25 | 26 | - (void)viewDidLoad 27 | { 28 | [super viewDidLoad]; 29 | // Do any additional setup after loading the view from its nib. 30 | 31 | //添加手势 32 | UITapGestureRecognizer *gestureForSpring = [[UITapGestureRecognizer alloc] init]; 33 | [gestureForSpring addTarget:self action:@selector(changeSize:)]; 34 | [_springView addGestureRecognizer:gestureForSpring]; 35 | 36 | } 37 | 38 | - (void)changeSize:(UITapGestureRecognizer*)tap{ 39 | //用POPSpringAnimation 让viewBlue实现弹性放大缩小的效果 40 | POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerSize]; 41 | 42 | CGRect rect = _springView.frame; 43 | if (rect.size.width==100) { 44 | springAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(300, 300)]; 45 | } 46 | else{ 47 | springAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(100, 100)]; 48 | } 49 | 50 | 51 | //弹性值 52 | springAnimation.springBounciness = 20.0; 53 | //弹性速度 54 | springAnimation.springSpeed = 20.0; 55 | 56 | [_springView.layer pop_addAnimation:springAnimation forKey:@"changesize"]; 57 | 58 | } 59 | 60 | 61 | - (void)didReceiveMemoryWarning 62 | { 63 | [super didReceiveMemoryWarning]; 64 | // Dispose of any resources that can be recreated. 65 | } 66 | 67 | @end 68 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPCustomAnimation.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPAnimation.h" 11 | 12 | @class POPCustomAnimation; 13 | 14 | /** 15 | @abstract POPCustomAnimationBlock is the callback block of a custom animation. 16 | @discussion This block will be executed for each animation frame and should update the property or properties being animated based on current timing. 17 | @param target The object being animated. Reference the passed in target to help avoid retain loops. 18 | @param target The custom animation instance. Use to determine the current and elapsed time since last callback. Reference the passed in animation to help avoid retain loops. 19 | @return Flag indicating whether the animation should continue animating. Return NO to indicate animation is done. 20 | */ 21 | typedef BOOL (^POPCustomAnimationBlock)(id target, POPCustomAnimation *animation); 22 | 23 | /** 24 | @abstract POPCustomAnimation is a concrete animation subclass for custom animations. 25 | */ 26 | @interface POPCustomAnimation : POPAnimation 27 | 28 | /** 29 | @abstract Creates and returns an initialized custom animation instance. 30 | @discussion This is the designated initializer. 31 | @param block The custom animation callback block. See {@ref POPCustomAnimationBlock}. 32 | @return The initialized custom animation instance. 33 | */ 34 | + (instancetype)animationWithBlock:(POPCustomAnimationBlock)block; 35 | 36 | /** 37 | @abstract The current animation time at time of callback. 38 | */ 39 | @property (readonly, nonatomic) CFTimeInterval currentTime; 40 | 41 | /** 42 | @abstract The elapsed animation time since last callback. 43 | */ 44 | @property (readonly, nonatomic) CFTimeInterval elapsedTime; 45 | 46 | @end 47 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPMath.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import 11 | #import 12 | 13 | #import "POPDefines.h" 14 | 15 | #import "POPVector.h" 16 | 17 | NS_INLINE CGFloat sqrtr(CGFloat f) 18 | { 19 | #if CGFLOAT_IS_DOUBLE 20 | return sqrt(f); 21 | #else 22 | return sqrtf(f); 23 | #endif 24 | } 25 | 26 | // round to nearest sub; pass 2.0 to round to every 0.5 (eg: retina pixels) 27 | NS_INLINE CGFloat POPSubRound(CGFloat f, CGFloat sub) 28 | { 29 | return round(f * sub) / sub; 30 | } 31 | 32 | #define MIX(a, b, f) ((a) + (f) * ((b) - (a))) 33 | 34 | // the longer the duration, the higher the necessary precision 35 | #define SOLVE_EPS(dur) (1. / (1000. * (dur))) 36 | 37 | #define _EQLF_(x, y, epsilon) (fabsf ((x) - (y)) < epsilon) 38 | 39 | extern void interpolate_vector(NSUInteger count, CGFloat *dst, const CGFloat *from, const CGFloat *to, double f); 40 | 41 | extern double timing_function_solve(const double vec[4], double t, double eps); 42 | 43 | // quadratic mapping of t [0, 1] to [start, end] 44 | extern double quadratic_out_interpolation(double t, double start, double end); 45 | 46 | // normalize value to [0, 1] based on its range [startValue, endValue] 47 | extern double normalize(double value, double startValue, double endValue); 48 | 49 | // project a normalized value [0, 1] to a given range [start, end] 50 | extern double project_normal(double n, double start, double end); 51 | 52 | // solve a quadratic equation of the form a * x^2 + b * x + c = 0 53 | extern void quadratic_solve(CGFloat a, CGFloat b, CGFloat c, CGFloat &x1, CGFloat &x2); 54 | 55 | // for a given tension return the bouncy 3 friction that produces no bounce 56 | extern double b3_nobounce(double tension); 57 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimationTracerInternal.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import 11 | 12 | #import "POPAnimationTracer.h" 13 | 14 | @interface POPAnimationTracer (Internal) 15 | 16 | /** 17 | @abstract Designated initalizer. Pass the animation being traced. 18 | */ 19 | - (instancetype)initWithAnimation:(POPAnimation *)anAnim; 20 | 21 | /** 22 | @abstract Records read value. 23 | */ 24 | - (void)readPropertyValue:(id)aValue; 25 | 26 | /** 27 | @abstract Records write value. 28 | */ 29 | - (void)writePropertyValue:(id)aValue; 30 | 31 | /** 32 | Records to value update. 33 | */ 34 | - (void)updateToValue:(id)aValue; 35 | 36 | /** 37 | @abstract Records from value update. 38 | */ 39 | - (void)updateFromValue:(id)aValue; 40 | 41 | /** 42 | @abstract Records from value update. 43 | */ 44 | - (void)updateVelocity:(id)aValue; 45 | 46 | /** 47 | @abstract Records bounciness update. 48 | */ 49 | - (void)updateBounciness:(float)aFloat; 50 | 51 | /** 52 | @abstract Records speed update. 53 | */ 54 | - (void)updateSpeed:(float)aFloat; 55 | 56 | /** 57 | @abstract Records friction update. 58 | */ 59 | - (void)updateFriction:(float)aFloat; 60 | 61 | /** 62 | @abstract Records mass update. 63 | */ 64 | - (void)updateMass:(float)aFloat; 65 | 66 | /** 67 | @abstract Records tension update. 68 | */ 69 | - (void)updateTension:(float)aFloat; 70 | 71 | /** 72 | @abstract Records did add. 73 | */ 74 | - (void)didAdd; 75 | 76 | /** 77 | @abstract Records did start. 78 | */ 79 | - (void)didStart; 80 | 81 | /** 82 | @abstract Records did stop. 83 | */ 84 | - (void)didStop:(BOOL)finished; 85 | 86 | /** 87 | @abstract Records did reach to value. 88 | */ 89 | - (void)didReachToValue:(id)aValue; 90 | 91 | @end 92 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimationEvent.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import 11 | 12 | /** 13 | @abstract Enumeraton of animation event types. 14 | */ 15 | typedef NS_ENUM(NSUInteger, POPAnimationEventType) { 16 | kPOPAnimationEventPropertyRead = 0, 17 | kPOPAnimationEventPropertyWrite, 18 | kPOPAnimationEventToValueUpdate, 19 | kPOPAnimationEventFromValueUpdate, 20 | kPOPAnimationEventVelocityUpdate, 21 | kPOPAnimationEventBouncinessUpdate, 22 | kPOPAnimationEventSpeedUpdate, 23 | kPOPAnimationEventFrictionUpdate, 24 | kPOPAnimationEventMassUpdate, 25 | kPOPAnimationEventTensionUpdate, 26 | kPOPAnimationEventDidStart, 27 | kPOPAnimationEventDidStop, 28 | kPOPAnimationEventDidReachToValue, 29 | }; 30 | 31 | /** 32 | @abstract The base animation event class. 33 | */ 34 | @interface POPAnimationEvent : NSObject 35 | 36 | /** 37 | @abstract The event type. See {@ref POPAnimationEventType} for possible values. 38 | */ 39 | @property (readonly, nonatomic, assign) POPAnimationEventType type; 40 | 41 | /** 42 | @abstract The time of event. 43 | */ 44 | @property (readonly, nonatomic, assign) CFTimeInterval time; 45 | 46 | /** 47 | @abstract Optional string describing the animation at time of event. 48 | */ 49 | @property (readonly, nonatomic, copy) NSString *animationDescription; 50 | 51 | @end 52 | 53 | /** 54 | @abstract An animation event subclass for recording value and velocity. 55 | */ 56 | @interface POPAnimationValueEvent : POPAnimationEvent 57 | 58 | /** 59 | @abstract The value recorded. 60 | */ 61 | @property (readonly, nonatomic, strong) id value; 62 | 63 | /** 64 | @abstract The velocity recorded, if any. 65 | */ 66 | @property (readonly, nonatomic, strong) id velocity; 67 | 68 | @end 69 | -------------------------------------------------------------------------------- /TestPop/Controllers/POPSpringPOPLayerPosition.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /TestPop/Controllers/POPBasicPOPViewAlpha.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /TestPop/Controllers/POPSpringPOPLayerSize.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPCGUtils.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import 11 | #import "POPDefines.h" 12 | 13 | #if TARGET_OS_IPHONE 14 | @class UIColor; 15 | #endif 16 | 17 | POP_EXTERN_C_BEGIN 18 | 19 | NS_INLINE CGPoint values_to_point(const CGFloat values[]) 20 | { 21 | return CGPointMake(values[0], values[1]); 22 | } 23 | 24 | NS_INLINE CGSize values_to_size(const CGFloat values[]) 25 | { 26 | return CGSizeMake(values[0], values[1]); 27 | } 28 | 29 | NS_INLINE CGRect values_to_rect(const CGFloat values[]) 30 | { 31 | return CGRectMake(values[0], values[1], values[2], values[3]); 32 | } 33 | 34 | NS_INLINE void values_from_point(CGFloat values[], CGPoint p) 35 | { 36 | values[0] = p.x; 37 | values[1] = p.y; 38 | } 39 | 40 | NS_INLINE void values_from_size(CGFloat values[], CGSize s) 41 | { 42 | values[0] = s.width; 43 | values[1] = s.height; 44 | } 45 | 46 | NS_INLINE void values_from_rect(CGFloat values[], CGRect r) 47 | { 48 | values[0] = r.origin.x; 49 | values[1] = r.origin.y; 50 | values[2] = r.size.width; 51 | values[3] = r.size.height; 52 | } 53 | 54 | /** 55 | Takes a CGColorRef and converts it into RGBA components, if necessary. 56 | */ 57 | extern void POPCGColorGetRGBAComponents(CGColorRef color, CGFloat components[]); 58 | 59 | /** 60 | Takes RGBA components and returns a CGColorRef. 61 | */ 62 | extern CGColorRef POPCGColorRGBACreate(const CGFloat components[]) CF_RETURNS_RETAINED; 63 | 64 | #if TARGET_OS_IPHONE 65 | 66 | /** 67 | Takes a UIColor and converts it into RGBA components, if necessary. 68 | */ 69 | extern void POPUIColorGetRGBAComponents(UIColor *color, CGFloat components[]); 70 | 71 | /** 72 | Takes RGBA components and returns a UIColor. 73 | */ 74 | extern UIColor *POPUIColorRGBACreate(const CGFloat components[]) NS_RETURNS_RETAINED; 75 | 76 | #endif 77 | 78 | POP_EXTERN_C_END 79 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPCGUtils.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPCGUtils.h" 11 | 12 | #if TARGET_OS_IPHONE 13 | #import 14 | #else 15 | #import 16 | #endif 17 | 18 | void POPCGColorGetRGBAComponents(CGColorRef color, CGFloat components[]) 19 | { 20 | if (!color) { 21 | #if TARGET_OS_IPHONE 22 | color = [UIColor clearColor].CGColor; 23 | #else 24 | color = [NSColor clearColor].CGColor; 25 | #endif 26 | } 27 | 28 | const CGFloat *colors = CGColorGetComponents(color); 29 | size_t count = CGColorGetNumberOfComponents(color); 30 | 31 | if (4 == count) { 32 | // RGB colorspace 33 | components[0] = colors[0]; 34 | components[1] = colors[1]; 35 | components[2] = colors[2]; 36 | components[3] = colors[3]; 37 | } else if (2 == count) { 38 | // Grey colorspace 39 | components[0] = components[1] = components[2] = colors[0]; 40 | components[3] = colors[1]; 41 | } else { 42 | // TODO HSV and CMYK conversion 43 | NSCAssert(NO, @"unsuported color space conversion, component count:%lu", count); 44 | } 45 | } 46 | 47 | CGColorRef POPCGColorRGBACreate(const CGFloat components[]) 48 | { 49 | #if TARGET_OS_IPHONE 50 | CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); 51 | CGColorRef color = CGColorCreate(space, components); 52 | CGColorSpaceRelease(space); 53 | return color; 54 | #else 55 | return CGColorCreateGenericRGB(components[0], components[1], components[2], components[3]); 56 | #endif 57 | } 58 | 59 | #if TARGET_OS_IPHONE 60 | 61 | void POPUIColorGetRGBAComponents(UIColor *color, CGFloat components[]) 62 | { 63 | return POPCGColorGetRGBAComponents(color.CGColor, components); 64 | } 65 | 66 | UIColor *POPUIColorRGBACreate(const CGFloat components[]) 67 | { 68 | CGColorRef colorRef = POPCGColorRGBACreate(components); 69 | UIColor *color = [[UIColor alloc] initWithCGColor:colorRef]; 70 | CGColorRelease(colorRef); 71 | return color; 72 | } 73 | 74 | #endif 75 | 76 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/WebCore/FloatConversion.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 Apple Inc. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 14 | * its contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | #ifndef FloatConversion_h 30 | #define FloatConversion_h 31 | 32 | #include 33 | 34 | namespace WebCore { 35 | 36 | template 37 | float narrowPrecisionToFloat(T); 38 | 39 | template<> 40 | inline float narrowPrecisionToFloat(double number) 41 | { 42 | return static_cast(number); 43 | } 44 | 45 | template 46 | CGFloat narrowPrecisionToCGFloat(T); 47 | 48 | template<> 49 | inline CGFloat narrowPrecisionToCGFloat(double number) 50 | { 51 | return static_cast(number); 52 | } 53 | 54 | } // namespace WebCore 55 | 56 | #endif // FloatConversion_h 57 | -------------------------------------------------------------------------------- /TestPop/Controllers/ViewController.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPBasicAnimation.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPPropertyAnimation.h" 11 | 12 | /** 13 | @abstract A concrete basic animation class. 14 | @discussion Animation is achieved through interpolation. 15 | */ 16 | @interface POPBasicAnimation : POPPropertyAnimation 17 | 18 | /** 19 | @abstract The designated initializer. 20 | @returns An instance of a basic animation. 21 | */ 22 | + (instancetype)animation; 23 | 24 | /** 25 | @abstract Convenience initializer that returns an animation with animatable property of name. 26 | @param name The name of the animatable property. 27 | @returns An instance of a basic animation configured with specified animatable property. 28 | */ 29 | + (instancetype)animationWithPropertyNamed:(NSString *)name; 30 | 31 | /** 32 | @abstract Convenience constructor. 33 | @returns Returns a basic animation with kCAMediaTimingFunctionDefault timing function. 34 | */ 35 | + (instancetype)defaultAnimation; 36 | 37 | /** 38 | @abstract Convenience constructor. 39 | @returns Returns a basic animation with kCAMediaTimingFunctionLinear timing function. 40 | */ 41 | + (instancetype)linearAnimation; 42 | 43 | /** 44 | @abstract Convenience constructor. 45 | @returns Returns a basic animation with kCAMediaTimingFunctionEaseIn timing function. 46 | */ 47 | + (instancetype)easeInAnimation; 48 | 49 | /** 50 | @abstract Convenience constructor. 51 | @returns Returns a basic animation with kCAMediaTimingFunctionEaseOut timing function. 52 | */ 53 | + (instancetype)easeOutAnimation; 54 | 55 | /** 56 | @abstract Convenience constructor. 57 | @returns Returns a basic animation with kCAMediaTimingFunctionEaseInEaseOut timing function. 58 | */ 59 | + (instancetype)easeInEaseOutAnimation; 60 | 61 | /** 62 | @abstract The duration in seconds. Defaults to 0.4. 63 | */ 64 | @property (assign, nonatomic) CFTimeInterval duration; 65 | 66 | /** 67 | @abstract A timing function defining the pacing of the animation. Defaults to nil indicating pacing according to kCAMediaTimingFunctionDefault. 68 | */ 69 | @property (strong, nonatomic) CAMediaTimingFunction *timingFunction; 70 | 71 | @end 72 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPMath.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPMath.h" 11 | #import "UnitBezier.h" 12 | #import "POPAnimationPrivate.h" 13 | 14 | void interpolate_vector(NSUInteger count, CGFloat *dst, const CGFloat *from, const CGFloat *to, double f) 15 | { 16 | for (NSUInteger idx = 0; idx < count; idx++) { 17 | dst[idx] = MIX(from[idx], to[idx], f); 18 | } 19 | } 20 | 21 | double timing_function_solve(const double vec[4], double t, double eps) 22 | { 23 | WebCore::UnitBezier bezier(vec[0], vec[1], vec[2], vec[3]); 24 | return bezier.solve(t, eps); 25 | } 26 | 27 | double normalize(double value, double startValue, double endValue) 28 | { 29 | return (value - startValue) / (endValue - startValue); 30 | } 31 | 32 | double project_normal(double n, double start, double end) 33 | { 34 | return start + (n * (end - start)); 35 | } 36 | 37 | static double linear_interpolation(double t, double start, double end) 38 | { 39 | return t * end + (1.f - t) * start; 40 | } 41 | 42 | double quadratic_out_interpolation(double t, double start, double end) 43 | { 44 | return linear_interpolation(2*t - t*t, start, end); 45 | } 46 | 47 | static double b3_friction1(double x) 48 | { 49 | return (0.0007 * pow(x, 3)) - (0.031 * pow(x, 2)) + 0.64 * x + 1.28; 50 | } 51 | 52 | static double b3_friction2(double x) 53 | { 54 | return (0.000044 * pow(x, 3)) - (0.006 * pow(x, 2)) + 0.36 * x + 2.; 55 | } 56 | 57 | static double b3_friction3(double x) 58 | { 59 | return (0.00000045 * pow(x, 3)) - (0.000332 * pow(x, 2)) + 0.1078 * x + 5.84; 60 | } 61 | 62 | double b3_nobounce(double tension) 63 | { 64 | double friction = 0; 65 | if (tension <= 18.) { 66 | friction = b3_friction1(tension); 67 | } else if (tension > 18 && tension <= 44) { 68 | friction = b3_friction2(tension); 69 | } else if (tension > 44) { 70 | friction = b3_friction3(tension); 71 | } else { 72 | assert(false); 73 | } 74 | return friction; 75 | } 76 | 77 | void quadratic_solve(CGFloat a, CGFloat b, CGFloat c, CGFloat &x1, CGFloat &x2) 78 | { 79 | CGFloat discriminant = sqrt(b * b - 4 * a * c); 80 | x1 = (-b + discriminant) / (2 * a); 81 | x2 = (-b - discriminant) / (2 * a); 82 | } 83 | -------------------------------------------------------------------------------- /TestPop/Controllers/CombinePOPbox.m: -------------------------------------------------------------------------------- 1 | // 2 | // CombinePOPbox.m 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-5-2. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import "CombinePOPbox.h" 10 | 11 | @interface CombinePOPbox () 12 | 13 | @end 14 | 15 | @implementation CombinePOPbox 16 | 17 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 18 | { 19 | self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 20 | if (self) { 21 | // Custom initialization 22 | } 23 | return self; 24 | } 25 | 26 | - (void)viewDidLoad 27 | { 28 | [super viewDidLoad]; 29 | // Do any additional setup after loading the view from its nib. 30 | 31 | _showPosition = CGRectMake(320-147, 5, 147, 160); 32 | _hidePosition = CGRectMake(320, 5, 0, 0); 33 | 34 | _popView = [[UIImageView alloc] initWithFrame:_hidePosition]; 35 | _popView.image = [UIImage imageNamed:@"menu.png"]; 36 | [self.view addSubview:_popView]; 37 | 38 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStyleDone target:self action:@selector(showPop)]; 39 | 40 | if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) { 41 | self.edgesForExtendedLayout = UIRectEdgeNone; 42 | } 43 | 44 | } 45 | 46 | - (void)showPop{ 47 | 48 | if (_isOpened) { 49 | [self hidePop]; 50 | return; 51 | } 52 | _isOpened = YES; 53 | 54 | POPSpringAnimation *positionAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame]; 55 | positionAnimation.fromValue = [NSValue valueWithCGRect:_hidePosition]; 56 | positionAnimation.toValue = [NSValue valueWithCGRect:_showPosition]; 57 | positionAnimation.springBounciness = 15.0f; 58 | positionAnimation.springSpeed = 20.0f; 59 | [_popView pop_addAnimation:positionAnimation forKey:@"frameAnimation"]; 60 | } 61 | 62 | - (void)hidePop{ 63 | 64 | POPBasicAnimation *positionAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewFrame]; 65 | positionAnimation.fromValue = [NSValue valueWithCGRect:_showPosition]; 66 | positionAnimation.toValue = [NSValue valueWithCGRect:_hidePosition]; 67 | [_popView pop_addAnimation:positionAnimation forKey:@"frameAnimation"]; 68 | 69 | _isOpened = NO; 70 | } 71 | 72 | - (void)didReceiveMemoryWarning 73 | { 74 | [super didReceiveMemoryWarning]; 75 | // Dispose of any resources that can be recreated. 76 | } 77 | 78 | @end 79 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPPropertyAnimation.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPAnimatableProperty.h" 11 | #import "POPAnimation.h" 12 | 13 | /** 14 | @abstract Flags for clamping animation values. 15 | @discussion Animation values can optionally be clamped to avoid overshoot. kPOPAnimationClampStart ensures values are more than fromValue and kPOPAnimationClampEnd ensures values are less than toValue. 16 | */ 17 | typedef NS_OPTIONS(NSUInteger, POPAnimationClampFlags) 18 | { 19 | kPOPAnimationClampNone = 0, 20 | kPOPAnimationClampStart = 1UL << 0, 21 | kPOPAnimationClampEnd = 1UL << 1, 22 | kPOPAnimationClampBoth = kPOPAnimationClampStart | kPOPAnimationClampEnd, 23 | }; 24 | 25 | /** 26 | @abstract The semi-concrete property animation subclass. 27 | */ 28 | @interface POPPropertyAnimation : POPAnimation 29 | 30 | /** 31 | @abstract The property to animate. 32 | */ 33 | @property (strong, nonatomic) POPAnimatableProperty *property; 34 | 35 | /** 36 | @abstract The value to animate from. 37 | @discussion The value type should match the property. If unspecified, the value is initialized to the object's current value on animation start. 38 | */ 39 | @property (copy, nonatomic) id fromValue; 40 | 41 | /** 42 | @abstract The value to animate to. 43 | @discussion The value type should match the property. If unspecified, the value is initialized to the object's current value on animation start. 44 | */ 45 | @property (copy, nonatomic) id toValue; 46 | 47 | /** 48 | @abstract The rounding factor applied to the current animated value. 49 | @discussion Specify 1.0 to animate between integral values. Defaults to 0 meaning no rounding. 50 | */ 51 | @property (assign, nonatomic) CGFloat roundingFactor; 52 | 53 | /** 54 | @abstract The clamp mode applied to the current animated value. 55 | @discussion See {@ref POPAnimationClampFlags} for possible values. Defaults to kPOPAnimationClampNone. 56 | */ 57 | @property (assign, nonatomic) NSUInteger clampMode; 58 | 59 | /** 60 | @abstract The flag indicating whether values should be "added" each frame, rather than set. 61 | @discussion Addition may be type dependent. Defaults to NO. 62 | */ 63 | @property (assign, nonatomic, getter = isAdditive) BOOL additive; 64 | 65 | @end 66 | -------------------------------------------------------------------------------- /TestPop/TestPop/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-4-29. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @implementation AppDelegate 12 | 13 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 14 | { 15 | self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 16 | // Override point for customization after application launch. 17 | 18 | ViewController *ctrl = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 19 | self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:ctrl]; 20 | 21 | self.window.backgroundColor = [UIColor whiteColor]; 22 | [self.window makeKeyAndVisible]; 23 | return YES; 24 | } 25 | 26 | - (void)applicationWillResignActive:(UIApplication *)application 27 | { 28 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 29 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 30 | } 31 | 32 | - (void)applicationDidEnterBackground:(UIApplication *)application 33 | { 34 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 35 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 36 | } 37 | 38 | - (void)applicationWillEnterForeground:(UIApplication *)application 39 | { 40 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 41 | } 42 | 43 | - (void)applicationDidBecomeActive:(UIApplication *)application 44 | { 45 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 46 | } 47 | 48 | - (void)applicationWillTerminate:(UIApplication *)application 49 | { 50 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 51 | } 52 | 53 | @end 54 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPSpringAnimation.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPPropertyAnimation.h" 11 | 12 | /** 13 | @abstract A concrete spring animation class. 14 | @discussion Animation is achieved through modeling spring dynamics. 15 | */ 16 | @interface POPSpringAnimation : POPPropertyAnimation 17 | 18 | /** 19 | @abstract The designated initializer. 20 | @returns An instance of a spring animation. 21 | */ 22 | + (instancetype)animation; 23 | 24 | /** 25 | @abstract Convenience initializer that returns an animation with animatable property of name. 26 | @param name The name of the animatable property. 27 | @returns An instance of a spring animation configured with specified animatable property. 28 | */ 29 | + (instancetype)animationWithPropertyNamed:(NSString *)name; 30 | 31 | /** 32 | @abstract The current velocity value. 33 | @discussion Set before animation start to account for initial velocity. Expressed in change of value units per second. 34 | */ 35 | @property (copy, nonatomic) id velocity; 36 | 37 | /** 38 | @abstract The effective bounciness. 39 | @discussion Use in conjunction with 'springSpeed' to change animation effect. Values are converted into corresponding dynamics constants. Defined as a value in the range [0, 20]. Defaults to 4. 40 | */ 41 | @property (assign, nonatomic) CGFloat springBounciness; 42 | 43 | /** 44 | @abstract The effective speed. 45 | @discussion Use in conjunction with 'springBounciness' to change animation effect. Values are converted into corresponding dynamics constants. Defined as a value in the range [0, 20]. Defaults to 12. 46 | */ 47 | @property (assign, nonatomic) CGFloat springSpeed; 48 | 49 | /** 50 | @abstract The tension used in the dynamics simulation. 51 | @discussion Can be used over bounciness and speed for finer grain tweaking of animation effect. 52 | */ 53 | @property (assign, nonatomic) CGFloat dynamicsTension; 54 | 55 | /** 56 | @abstract The friction used in the dynamics simulation. 57 | @discussion Can be used over bounciness and speed for finer grain tweaking of animation effect. 58 | */ 59 | @property (assign, nonatomic) CGFloat dynamicsFriction; 60 | 61 | /** 62 | @abstract The mass used in the dynamics simulation. 63 | @discussion Can be used over bounciness and speed for finer grain tweaking of animation effect. 64 | */ 65 | @property (assign, nonatomic) CGFloat dynamicsMass; 66 | 67 | @end 68 | -------------------------------------------------------------------------------- /TestPop/Controllers/POPSpringPOPViewFrame.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPBasicAnimation.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPBasicAnimationInternal.h" 11 | 12 | @implementation POPBasicAnimation 13 | 14 | #undef __state 15 | #define __state ((POPBasicAnimationState *)_state) 16 | 17 | #pragma mark - Lifecycle 18 | 19 | + (instancetype)animation 20 | { 21 | return [[self alloc] init]; 22 | } 23 | 24 | + (instancetype)animationWithPropertyNamed:(NSString *)aName 25 | { 26 | POPBasicAnimation *anim = [self animation]; 27 | anim.property = [POPAnimatableProperty propertyWithName:aName]; 28 | return anim; 29 | } 30 | 31 | - (void)_initState 32 | { 33 | _state = new POPBasicAnimationState(self); 34 | } 35 | 36 | + (instancetype)linearAnimation 37 | { 38 | POPBasicAnimation *anim = [self animation]; 39 | anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 40 | return anim; 41 | } 42 | 43 | + (instancetype)easeInAnimation 44 | { 45 | POPBasicAnimation *anim = [self animation]; 46 | anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 47 | return anim; 48 | } 49 | 50 | + (instancetype)easeOutAnimation 51 | { 52 | POPBasicAnimation *anim = [self animation]; 53 | anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 54 | return anim; 55 | } 56 | 57 | + (instancetype)easeInEaseOutAnimation 58 | { 59 | POPBasicAnimation *anim = [self animation]; 60 | anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 61 | return anim; 62 | } 63 | 64 | + (instancetype)defaultAnimation 65 | { 66 | POPBasicAnimation *anim = [self animation]; 67 | anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]; 68 | return anim; 69 | } 70 | 71 | - (id)init 72 | { 73 | return [self _init]; 74 | } 75 | 76 | #pragma mark - Properties 77 | 78 | DEFINE_RW_PROPERTY(POPBasicAnimationState, duration, setDuration:, CFTimeInterval); 79 | DEFINE_RW_PROPERTY_OBJ(POPBasicAnimationState, timingFunction, setTimingFunction:, CAMediaTimingFunction*, __state->updatedTimingFunction();); 80 | 81 | #pragma mark - Utility 82 | 83 | - (void)_appendDescription:(NSMutableString *)s debug:(BOOL)debug 84 | { 85 | [super _appendDescription:s debug:debug]; 86 | if (__state->duration) 87 | [s appendFormat:@"; duration = %f", __state->duration]; 88 | } 89 | 90 | @end 91 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPBasicAnimationInternal.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPBasicAnimation.h" 11 | #import "POPPropertyAnimationInternal.h" 12 | 13 | // default animation duration 14 | static CGFloat kPOPAnimationDurationDefault = 0.4; 15 | 16 | static void interpolate(POPValueType valueType, NSUInteger count, const CGFloat *fromVec, const CGFloat *toVec, CGFloat *outVec, double p) 17 | { 18 | switch (valueType) { 19 | case kPOPValueInteger: 20 | case kPOPValueFloat: 21 | case kPOPValuePoint: 22 | case kPOPValueSize: 23 | case kPOPValueRect: 24 | interpolate_vector(count, outVec, fromVec, toVec, p); 25 | break; 26 | default: 27 | NSCAssert(false, @"unhandled type %d", valueType); 28 | break; 29 | } 30 | } 31 | 32 | struct _POPBasicAnimationState : _POPPropertyAnimationState 33 | { 34 | CAMediaTimingFunction *timingFunction; 35 | double timingControlPoints[4]; 36 | CFTimeInterval duration; 37 | 38 | _POPBasicAnimationState(id __unsafe_unretained anim) : _POPPropertyAnimationState(anim), 39 | duration(kPOPAnimationDurationDefault), 40 | timingFunction(nil) 41 | { 42 | type = kPOPAnimationBasic; 43 | memset(timingControlPoints, 0, sizeof(timingControlPoints)); 44 | } 45 | 46 | bool isDone() { 47 | if (_POPPropertyAnimationState::isDone()) { 48 | return true; 49 | } 50 | return _EQLF_(progress, 1., 1e-2); 51 | } 52 | 53 | void updatedTimingFunction() 54 | { 55 | float vec[4] = {0., 0., 0., 0.}; 56 | [timingFunction getControlPointAtIndex:1 values:&vec[0]]; 57 | [timingFunction getControlPointAtIndex:2 values:&vec[2]]; 58 | for (NSUInteger idx = 0; idx < POP_ARRAY_COUNT(vec); idx++) { 59 | timingControlPoints[idx] = vec[idx]; 60 | } 61 | } 62 | 63 | bool advance(CFTimeInterval time, CFTimeInterval dt, id obj) { 64 | // default timing function 65 | if (!timingFunction) { 66 | ((POPBasicAnimation *)self).timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]; 67 | } 68 | 69 | // cap local time to duration 70 | CFTimeInterval t = MIN(time - startTime, duration) / duration; 71 | 72 | // solve for normalized time, aka progresss [0, 1] 73 | double p = timing_function_solve(timingControlPoints, t, SOLVE_EPS(duration)); 74 | 75 | // interpolate and advance 76 | if (p != progress) { 77 | interpolate(valueType, valueCount, fromVec->data(), toVec->data(), currentVec->data(), p); 78 | progress = p; 79 | return true; 80 | } 81 | 82 | clampCurrentValue(); 83 | 84 | return false; 85 | } 86 | }; 87 | 88 | typedef struct _POPBasicAnimationState POPBasicAnimationState; 89 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimationEvent.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPAnimationEvent.h" 11 | #import "POPAnimationEventInternal.h" 12 | 13 | static NSString *stringFromType(POPAnimationEventType aType) 14 | { 15 | switch (aType) { 16 | case kPOPAnimationEventPropertyRead: 17 | return @"read"; 18 | case kPOPAnimationEventPropertyWrite: 19 | return @"write"; 20 | case kPOPAnimationEventToValueUpdate: 21 | return @"toValue"; 22 | case kPOPAnimationEventFromValueUpdate: 23 | return @"fromValue"; 24 | case kPOPAnimationEventVelocityUpdate: 25 | return @"velocity"; 26 | case kPOPAnimationEventSpeedUpdate: 27 | return @"speed"; 28 | case kPOPAnimationEventBouncinessUpdate: 29 | return @"bounciness"; 30 | case kPOPAnimationEventFrictionUpdate: 31 | return @"friction"; 32 | case kPOPAnimationEventMassUpdate: 33 | return @"mass"; 34 | case kPOPAnimationEventTensionUpdate: 35 | return @"tension"; 36 | case kPOPAnimationEventDidStart: 37 | return @"didStart"; 38 | case kPOPAnimationEventDidStop: 39 | return @"didStop"; 40 | case kPOPAnimationEventDidReachToValue: 41 | return @"didReachToValue"; 42 | default: 43 | return nil; 44 | } 45 | } 46 | 47 | @implementation POPAnimationEvent 48 | 49 | - (instancetype)initWithType:(POPAnimationEventType)aType time:(CFTimeInterval)aTime 50 | { 51 | self = [super init]; 52 | if (nil != self) { 53 | _type = aType; 54 | _time = aTime; 55 | } 56 | return self; 57 | } 58 | 59 | - (NSString *)description 60 | { 61 | NSMutableString *s = [NSMutableString stringWithFormat:@""]; 64 | return s; 65 | } 66 | 67 | // subclass override 68 | - (void)_appendDescription:(NSMutableString *)s 69 | { 70 | if (0 != _animationDescription.length) { 71 | [s appendFormat:@"; animation = %@", _animationDescription]; 72 | } 73 | } 74 | 75 | @end 76 | 77 | @implementation POPAnimationValueEvent 78 | 79 | - (instancetype)initWithType:(POPAnimationEventType)aType time:(CFTimeInterval)aTime value:(id)aValue 80 | { 81 | self = [self initWithType:aType time:aTime]; 82 | if (nil != self) { 83 | _value = aValue; 84 | } 85 | return self; 86 | } 87 | 88 | - (void)_appendDescription:(NSMutableString *)s 89 | { 90 | [super _appendDescription:s]; 91 | 92 | if (nil != _value) { 93 | [s appendFormat:@"; value = %@", _value]; 94 | } 95 | 96 | if (nil != _velocity) { 97 | [s appendFormat:@"; velocity = %@", _velocity]; 98 | } 99 | } 100 | 101 | @end 102 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimationRuntime.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import 11 | #include 12 | 13 | #import 14 | 15 | #import "POPMath.h" 16 | #import "POPVector.h" 17 | 18 | enum POPValueType 19 | { 20 | kPOPValueUnknown = 0, 21 | kPOPValueInteger, 22 | kPOPValueFloat, 23 | kPOPValuePoint, 24 | kPOPValueSize, 25 | kPOPValueRect, 26 | kPOPValueAffineTransform, 27 | kPOPValueTransform, 28 | kPOPValueRange, 29 | kPOPValueColor, 30 | }; 31 | 32 | using namespace POP; 33 | 34 | /** 35 | Returns value type based on objc type description, given list of supported value types and length. 36 | */ 37 | extern POPValueType POPSelectValueType(const char *objctype, const POPValueType *types, size_t length); 38 | 39 | /** 40 | Returns value type based on objc object, given a list of supported value types and length. 41 | */ 42 | extern POPValueType POPSelectValueType(id obj, const POPValueType *types, size_t length); 43 | 44 | /** 45 | Array of all value types. 46 | */ 47 | extern const POPValueType kPOPAnimatableAllTypes[9]; 48 | 49 | /** 50 | Array of all value types supported for animation. 51 | */ 52 | extern const POPValueType kPOPAnimatableSupportTypes[7]; 53 | 54 | /** 55 | Returns a string description of a value type. 56 | */ 57 | extern NSString *POPValueTypeToString(POPValueType t); 58 | 59 | /** 60 | Returns a mutable dictionary of weak pointer keys to weak pointer values. 61 | */ 62 | extern CFMutableDictionaryRef POPDictionaryCreateMutableWeakPointerToWeakPointer(NSUInteger capacity) CF_RETURNS_RETAINED; 63 | 64 | /** 65 | Returns a mutable dictionary of weak pointer keys to weak pointer values. 66 | */ 67 | extern CFMutableDictionaryRef POPDictionaryCreateMutableWeakPointerToStrongObject(NSUInteger capacity) CF_RETURNS_RETAINED; 68 | 69 | /** 70 | Box a vector. 71 | */ 72 | extern id POPBox(VectorConstRef vec, POPValueType type, bool force = false); 73 | 74 | /** 75 | Unbox a vector. 76 | */ 77 | extern VectorRef POPUnbox(id value, POPValueType &type, NSUInteger &count, bool validate); 78 | 79 | /** 80 | Read/write block typedefs for convenience. 81 | */ 82 | typedef void(^pop_animatable_read_block)(id obj, CGFloat *value); 83 | typedef void(^pop_animatable_write_block)(id obj, const CGFloat *value); 84 | 85 | /** 86 | Read object value and return a Vector4r. 87 | */ 88 | NS_INLINE Vector4r read_values(pop_animatable_read_block read, id obj, size_t count) 89 | { 90 | Vector4r vec = Vector4r::Zero(); 91 | if (0 == count) 92 | return vec; 93 | 94 | read(obj, vec.data()); 95 | 96 | return vec; 97 | } 98 | 99 | NS_INLINE NSString *POPStringFromBOOL(BOOL value) 100 | { 101 | return value ? @"YES" : @"NO"; 102 | } 103 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPDecayAnimation.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPDecayAnimationInternal.h" 11 | 12 | @implementation POPDecayAnimation 13 | 14 | #pragma mark - Lifecycle 15 | 16 | #undef __state 17 | #define __state ((POPDecayAnimationState *)_state) 18 | 19 | + (instancetype)animation 20 | { 21 | return [[self alloc] init]; 22 | } 23 | 24 | + (instancetype)animationWithPropertyNamed:(NSString *)aName 25 | { 26 | POPDecayAnimation *anim = [self animation]; 27 | anim.property = [POPAnimatableProperty propertyWithName:aName]; 28 | return anim; 29 | } 30 | 31 | - (id)init 32 | { 33 | return [self _init]; 34 | } 35 | 36 | - (void)_initState 37 | { 38 | _state = new POPDecayAnimationState(self); 39 | } 40 | 41 | #pragma mark - Properties 42 | 43 | DEFINE_RW_PROPERTY(POPDecayAnimationState, deceleration, setDeceleration:, CGFloat, __state->toVec = NULL;); 44 | 45 | @dynamic velocity; 46 | 47 | - (id)toValue 48 | { 49 | [self _ensureComputedProperties]; 50 | return POPBox(__state->toVec, __state->valueType); 51 | } 52 | 53 | - (CFTimeInterval)duration 54 | { 55 | [self _ensureComputedProperties]; 56 | return __state->duration; 57 | } 58 | 59 | - (void)setFromValue:(id)fromValue 60 | { 61 | super.fromValue = fromValue; 62 | [self _invalidateComputedProperties]; 63 | } 64 | 65 | - (void)setToValue:(id)aValue 66 | { 67 | // no-op 68 | NSLog(@"ignoring to value on decay animation %@", self); 69 | } 70 | 71 | - (id)velocity 72 | { 73 | return POPBox(__state->velocityVec, __state->valueType); 74 | } 75 | 76 | - (void)setVelocity:(id)aValue 77 | { 78 | VectorRef vec = POPUnbox(aValue, __state->valueType, __state->valueCount, YES); 79 | 80 | if (!vec_equal(vec, __state->velocityVec)) { 81 | __state->velocityVec = vec; 82 | 83 | if (__state->tracing) { 84 | [__state->tracer updateVelocity:aValue]; 85 | } 86 | 87 | [self _invalidateComputedProperties]; 88 | 89 | // automatically unpause active animations 90 | if (__state->active && __state->paused) { 91 | __state->fromVec = NULL; 92 | __state->setPaused(false); 93 | } 94 | } 95 | } 96 | 97 | #pragma mark - Utility 98 | 99 | - (void)_ensureComputedProperties 100 | { 101 | if (NULL == __state->toVec) { 102 | __state->computeDestinationValues(); 103 | } 104 | } 105 | 106 | - (void)_invalidateComputedProperties 107 | { 108 | __state->toVec = NULL; 109 | __state->duration = 0; 110 | } 111 | 112 | - (void)_appendDescription:(NSMutableString *)s debug:(BOOL)debug 113 | { 114 | [super _appendDescription:s debug:debug]; 115 | 116 | if (NULL != __state->toVec) 117 | [s appendFormat:@"; duration = %f", self.duration]; 118 | 119 | if (__state->deceleration) 120 | [s appendFormat:@"; deceleration = %f", __state->deceleration]; 121 | } 122 | 123 | @end 124 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPPropertyAnimation.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPPropertyAnimationInternal.h" 11 | 12 | @implementation POPPropertyAnimation 13 | 14 | #pragma mark - Lifecycle 15 | 16 | #undef __state 17 | #define __state ((POPPropertyAnimationState *)_state) 18 | 19 | - (void)_initState 20 | { 21 | _state = new POPPropertyAnimationState(self); 22 | } 23 | 24 | #pragma mark - Properties 25 | 26 | DEFINE_RW_FLAG(POPPropertyAnimationState, additive, isAdditive, setAdditive:); 27 | DEFINE_RW_PROPERTY(POPPropertyAnimationState, roundingFactor, setRoundingFactor:, CGFloat); 28 | DEFINE_RW_PROPERTY(POPPropertyAnimationState, clampMode, setClampMode:, NSUInteger); 29 | DEFINE_RW_PROPERTY_OBJ(POPPropertyAnimationState, property, setProperty:, POPAnimatableProperty*, ((POPPropertyAnimationState*)_state)->updatedDynamicsThreshold();); 30 | DEFINE_RW_PROPERTY_OBJ_COPY(POPPropertyAnimationState, progressMarkers, setProgressMarkers:, NSArray*, ((POPPropertyAnimationState*)_state)->updatedProgressMarkers();); 31 | 32 | - (id)fromValue 33 | { 34 | return POPBox(__state->fromVec, __state->valueType); 35 | } 36 | 37 | - (void)setFromValue:(id)aValue 38 | { 39 | POPPropertyAnimationState *s = __state; 40 | VectorRef vec = POPUnbox(aValue, s->valueType, s->valueCount, YES); 41 | if (!vec_equal(vec, s->fromVec)) { 42 | s->fromVec = vec; 43 | 44 | if (s->tracing) { 45 | [s->tracer updateFromValue:aValue]; 46 | } 47 | } 48 | } 49 | 50 | - (id)toValue 51 | { 52 | return POPBox(__state->toVec, __state->valueType); 53 | } 54 | 55 | - (void)setToValue:(id)aValue 56 | { 57 | POPPropertyAnimationState *s = (POPPropertyAnimationState *)POPAnimationGetState(self); 58 | VectorRef vec = POPUnbox(aValue, s->valueType, s->valueCount, YES); 59 | 60 | if (!vec_equal(vec, s->toVec)) { 61 | s->toVec = vec; 62 | 63 | // invalidate to dependent state 64 | s->didReachToValue = false; 65 | s->distanceVec = NULL; 66 | 67 | if (s->tracing) { 68 | [s->tracer updateToValue:aValue]; 69 | } 70 | 71 | // automatically unpause active animations 72 | if (s->active && s->paused) { 73 | s->setPaused(false); 74 | } 75 | } 76 | } 77 | 78 | - (id)currentValue 79 | { 80 | return POPBox(__state->currentValue(), __state->valueType); 81 | } 82 | 83 | #pragma mark - Utility 84 | 85 | - (void)_appendDescription:(NSMutableString *)s debug:(BOOL)debug 86 | { 87 | [s appendFormat:@"; from = %@; to = %@", describe(__state->fromVec), describe(__state->toVec)]; 88 | 89 | if (_state->active) 90 | [s appendFormat:@"; currentValue = %@", describe(__state->currentValue())]; 91 | 92 | if (__state->velocityVec && 0 != __state->velocityVec->norm()) 93 | [s appendFormat:@"; velocity = %@", describe(__state->velocityVec)]; 94 | 95 | if (!self.removedOnCompletion) 96 | [s appendFormat:@"; removedOnCompletion = %@", POPStringFromBOOL(self.removedOnCompletion)]; 97 | 98 | if (__state->progressMarkers) 99 | [s appendFormat:@"; progressMarkers = [%@]", [__state->progressMarkers componentsJoinedByString:@", "]]; 100 | 101 | if (_state->active) 102 | [s appendFormat:@"; progress = %f", __state->progress]; 103 | } 104 | 105 | @end 106 | -------------------------------------------------------------------------------- /TestPop/TestPop.xcodeproj/xcuserdata/xudongjin.xcuserdatad/xcschemes/TestPop.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 61 | 62 | 68 | 69 | 70 | 71 | 72 | 73 | 79 | 80 | 86 | 87 | 88 | 89 | 91 | 92 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/WebCore/UnitBezier.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 Apple Inc. All Rights Reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 1. Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #ifndef UnitBezier_h 27 | #define UnitBezier_h 28 | 29 | #include 30 | 31 | namespace WebCore { 32 | 33 | struct UnitBezier { 34 | UnitBezier(double p1x, double p1y, double p2x, double p2y) 35 | { 36 | // Calculate the polynomial coefficients, implicit first and last control points are (0,0) and (1,1). 37 | cx = 3.0 * p1x; 38 | bx = 3.0 * (p2x - p1x) - cx; 39 | ax = 1.0 - cx -bx; 40 | 41 | cy = 3.0 * p1y; 42 | by = 3.0 * (p2y - p1y) - cy; 43 | ay = 1.0 - cy - by; 44 | } 45 | 46 | double sampleCurveX(double t) 47 | { 48 | // `ax t^3 + bx t^2 + cx t' expanded using Horner's rule. 49 | return ((ax * t + bx) * t + cx) * t; 50 | } 51 | 52 | double sampleCurveY(double t) 53 | { 54 | return ((ay * t + by) * t + cy) * t; 55 | } 56 | 57 | double sampleCurveDerivativeX(double t) 58 | { 59 | return (3.0 * ax * t + 2.0 * bx) * t + cx; 60 | } 61 | 62 | // Given an x value, find a parametric value it came from. 63 | double solveCurveX(double x, double epsilon) 64 | { 65 | double t0; 66 | double t1; 67 | double t2; 68 | double x2; 69 | double d2; 70 | int i; 71 | 72 | // First try a few iterations of Newton's method -- normally very fast. 73 | for (t2 = x, i = 0; i < 8; i++) { 74 | x2 = sampleCurveX(t2) - x; 75 | if (fabs (x2) < epsilon) 76 | return t2; 77 | d2 = sampleCurveDerivativeX(t2); 78 | if (fabs(d2) < 1e-6) 79 | break; 80 | t2 = t2 - x2 / d2; 81 | } 82 | 83 | // Fall back to the bisection method for reliability. 84 | t0 = 0.0; 85 | t1 = 1.0; 86 | t2 = x; 87 | 88 | if (t2 < t0) 89 | return t0; 90 | if (t2 > t1) 91 | return t1; 92 | 93 | while (t0 < t1) { 94 | x2 = sampleCurveX(t2); 95 | if (fabs(x2 - x) < epsilon) 96 | return t2; 97 | if (x > x2) 98 | t0 = t2; 99 | else 100 | t1 = t2; 101 | t2 = (t1 - t0) * .5 + t0; 102 | } 103 | 104 | // Failure. 105 | return t2; 106 | } 107 | 108 | double solve(double x, double epsilon) 109 | { 110 | return sampleCurveY(solveCurveX(x, epsilon)); 111 | } 112 | 113 | private: 114 | double ax; 115 | double bx; 116 | double cx; 117 | 118 | double ay; 119 | double by; 120 | double cy; 121 | }; 122 | } 123 | #endif 124 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | POPdemo 2 | ======= 3 | 4 | A simple demo for facebook's pop framework. 5 | 6 | ### pop一共有四个大类 7 | 8 | POPSpringAnimation 有弹性效果的动画类(个人比较喜欢这个) 9 | POPBasicAnimation 基本动画类 10 | POPDecayAnimation 衰减动画类 11 | POPCustomAnimation 可以自定义动画的类 12 | 13 | 14 | ### 导入pop 15 | 很简单,直接把pop文件夹拖到项目里,然后导入pop.h即可。 16 | 17 | #import "POP.h" 18 | 19 | 20 | ### 下面的代码示例用POPSpringAnimation做一个弹性放大-缩小的效果 21 | 22 | ```objective-c 23 | - (void)viewDidLoad 24 | { 25 | [super viewDidLoad]; 26 | // Do any additional setup after loading the view from its nib. 27 | 28 | //添加手势 29 | UITapGestureRecognizer *gestureForSpring = [[UITapGestureRecognizer alloc] init]; 30 | [gestureForSpring addTarget:self action:@selector(changeSize:)]; 31 | [_springView addGestureRecognizer:gestureForSpring]; 32 | 33 | } 34 | 35 | 36 | 37 | - (void)changeSize:(UITapGestureRecognizer*)tap{ 38 | - 39 | //用POPSpringAnimation 让viewBlue实现弹性放大缩小的效果 40 | POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerSize]; 41 | 42 | CGRect rect = _springView.frame; 43 | if (rect.size.width==100) { 44 | springAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(300, 300)]; 45 | } 46 | else{ 47 | springAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(100, 100)]; 48 | } 49 | 50 | //弹性值 51 | springAnimation.springBounciness = 20.0; 52 | //弹性速度 53 | springAnimation.springSpeed = 20.0; 54 | 55 | [_springView.layer pop_addAnimation:springAnimation forKey:@"changesize"]; 56 | 57 | } 58 | ``` 59 | 60 | ### 效果如下 61 | ![image](https://github.com/jxd001/POPdemo/blob/master/TestPop/Untitled1.gif?raw=true) 62 | 63 | 64 | ### 上面的代码是改变view的size,下面示例改变position 65 | ```objective-c 66 | POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPosition]; 67 | 68 | CGPoint point = _springView.center; 69 | 70 | if (point.y==240) { 71 | springAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(point.x, -230)]; 72 | } 73 | else{ 74 | springAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(point.x, 240)]; 75 | } 76 | 77 | //弹性值 78 | springAnimation.springBounciness = 20.0; 79 | //弹性速度 80 | springAnimation.springSpeed = 20.0; 81 | 82 | [_springView pop_addAnimation:springAnimation forKey:@"changeposition"]; 83 | 84 | ``` 85 | 86 | ### 效果: 87 | ![image](https://github.com/jxd001/POPdemo/blob/master/TestPop/Untitled2.gif?raw=true)
88 | 这个效果可以做一个弹出框从上往下跳出来,我之前做过这个效果,用了N多代码,用pop只用几句代码就实现了。
89 | ### 一个比较实用的效果,弹出菜单: 90 | ![image](https://github.com/jxd001/POPdemo/blob/master/TestPop/Untitled3.gif?raw=true)
91 | 代码如下:
92 | ```objective-c 93 | 94 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStyleDone target:self action:@selector(showPop)]; 95 | 96 | - (void)showPop{ 97 | 98 | if (_isOpened) { 99 | [self hidePop]; 100 | return; 101 | } 102 | _isOpened = YES; 103 | 104 | POPSpringAnimation *positionAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame]; 105 | positionAnimation.fromValue = [NSValue valueWithCGRect:_hidePosition]; 106 | positionAnimation.toValue = [NSValue valueWithCGRect:_showPosition]; 107 | positionAnimation.springBounciness = 15.0f; 108 | positionAnimation.springSpeed = 20.0f; 109 | [_popView pop_addAnimation:positionAnimation forKey:@"frameAnimation"]; 110 | } 111 | 112 | - (void)hidePop{ 113 | 114 | POPBasicAnimation *positionAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewFrame]; 115 | positionAnimation.fromValue = [NSValue valueWithCGRect:_showPosition]; 116 | positionAnimation.toValue = [NSValue valueWithCGRect:_hidePosition]; 117 | //key一样就会用后面的动画覆盖之前的 118 | [_popView pop_addAnimation:positionAnimation forKey:@"frameAnimation"]; 119 | 120 | _isOpened = NO; 121 | } 122 | 123 | ``` 124 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPSpringAnimationInternal.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPAnimationExtras.h" 11 | #import "POPPropertyAnimationInternal.h" 12 | #import "POPSpringAnimation.h" 13 | 14 | struct _POPSpringAnimationState : _POPPropertyAnimationState 15 | { 16 | SpringSolver4d *solver; 17 | CGFloat springSpeed; 18 | CGFloat springBounciness; // normalized springiness 19 | CGFloat dynamicsTension; // tension 20 | CGFloat dynamicsFriction; // friction 21 | CGFloat dynamicsMass; // mass 22 | 23 | _POPSpringAnimationState(id __unsafe_unretained anim) : _POPPropertyAnimationState(anim), 24 | solver(nullptr), 25 | springSpeed(12.), 26 | springBounciness(4.), 27 | dynamicsTension(0), 28 | dynamicsFriction(0), 29 | dynamicsMass(0) 30 | { 31 | type = kPOPAnimationSpring; 32 | } 33 | 34 | bool hasConverged() 35 | { 36 | NSUInteger count = valueCount; 37 | if (shouldRound()) { 38 | return vec_equal(previous2Vec, previousVec) && vec_equal(previousVec, toVec); 39 | } else { 40 | if (!previousVec || !previous2Vec) 41 | return false; 42 | 43 | CGFloat t = dynamicsThreshold / 5; 44 | 45 | const CGFloat *toValues = toVec->data(); 46 | const CGFloat *previousValues = previousVec->data(); 47 | const CGFloat *previous2Values = previous2Vec->data(); 48 | 49 | for (NSUInteger idx = 0; idx < count; idx++) { 50 | if ((fabsf(toValues[idx] - previousValues[idx]) >= t) || (fabsf(previous2Values[idx] - previousValues[idx]) >= t)) { 51 | return false; 52 | } 53 | } 54 | return true; 55 | } 56 | } 57 | 58 | bool isDone() { 59 | if (_POPPropertyAnimationState::isDone()) { 60 | return true; 61 | } 62 | return solver->started() && (hasConverged() || solver->hasConverged()); 63 | } 64 | 65 | void updatedDynamics() 66 | { 67 | if (NULL != solver) { 68 | solver->setConstants(dynamicsTension, dynamicsFriction, dynamicsMass); 69 | } 70 | } 71 | 72 | void updatedDynamicsThreshold() 73 | { 74 | _POPPropertyAnimationState::updatedDynamicsThreshold(); 75 | if (NULL != solver) { 76 | solver->setThreshold(dynamicsThreshold); 77 | } 78 | } 79 | 80 | void updatedBouncinessAndSpeed() { 81 | [POPSpringAnimation convertBounciness:springBounciness speed:springSpeed toTension:&dynamicsTension friction:&dynamicsFriction mass:&dynamicsMass]; 82 | updatedDynamics(); 83 | } 84 | 85 | bool advance(CFTimeInterval time, CFTimeInterval dt, id obj) { 86 | // advance past not yet initialized animations 87 | if (NULL == currentVec) { 88 | return false; 89 | } 90 | 91 | CFTimeInterval localTime = time - startTime; 92 | 93 | Vector4d value = vector4d(currentVec); 94 | Vector4d toValue = vector4d(toVec); 95 | Vector4d velocity = vector4d(velocityVec); 96 | 97 | SSState4d state; 98 | state.p = toValue - value; 99 | 100 | // the solver assumes a spring of size zero 101 | // flip the velocity from user perspective to solver perspective 102 | state.v = velocity * -1; 103 | 104 | solver->advance(state, localTime, dt); 105 | value = toValue - state.p; 106 | 107 | // flip velocity back to user perspective 108 | velocity = state.v * -1; 109 | 110 | *currentVec = value; 111 | 112 | if (velocityVec) { 113 | *velocityVec = velocity; 114 | } 115 | 116 | clampCurrentValue(); 117 | 118 | return true; 119 | } 120 | 121 | virtual void reset(bool all) { 122 | _POPPropertyAnimationState::reset(all); 123 | 124 | if (solver) { 125 | solver->setConstants(dynamicsTension, dynamicsFriction, dynamicsMass); 126 | solver->reset(); 127 | } 128 | } 129 | }; 130 | 131 | typedef struct _POPSpringAnimationState POPSpringAnimationState; 132 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPDecayAnimationInternal.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPDecayAnimation.h" 11 | #import "POPPropertyAnimationInternal.h" 12 | 13 | // minimal velocity factor before decay animation is considered complete, in units / s 14 | static CGFloat kPOPAnimationDecayMinimalVelocityFactor = 5.; 15 | 16 | // default decay animation deceleration 17 | static CGFloat kPOPAnimationDecayDecelerationDefault = 0.998; 18 | 19 | static void decay_position(CGFloat *x, CGFloat *v, NSUInteger count, CFTimeInterval dt, CGFloat deceleration) 20 | { 21 | dt *= 1000; 22 | 23 | // v0 = v / 1000 24 | // v = v0 * powf(deceleration, dt); 25 | // v = v * 1000; 26 | 27 | // x0 = x; 28 | // x = x0 + v0 * deceleration * (1 - powf(deceleration, dt)) / (1 - deceleration) 29 | float v0[count]; 30 | float kv = powf(deceleration, dt); 31 | float kx = deceleration * (1 - kv) / (1 - deceleration); 32 | 33 | for (NSUInteger idx = 0; idx < count; idx++) { 34 | v0[idx] = v[idx] / 1000.; 35 | v[idx] = v0[idx] * kv * 1000.; 36 | x[idx] = x[idx] + v0[idx] * kx; 37 | } 38 | } 39 | 40 | struct _POPDecayAnimationState : _POPPropertyAnimationState 41 | { 42 | double deceleration; 43 | CFTimeInterval duration; 44 | 45 | _POPDecayAnimationState(id __unsafe_unretained anim) : 46 | _POPPropertyAnimationState(anim), 47 | duration(0), 48 | deceleration(kPOPAnimationDecayDecelerationDefault) { 49 | type = kPOPAnimationDecay; 50 | } 51 | 52 | bool isDone() { 53 | if (_POPPropertyAnimationState::isDone()) { 54 | return true; 55 | } 56 | 57 | CGFloat f = dynamicsThreshold * kPOPAnimationDecayMinimalVelocityFactor; 58 | const CGFloat *velocityValues = vec_data(velocityVec); 59 | for (NSUInteger idx = 0; idx < valueCount; idx++) { 60 | if (fabsf(velocityValues[idx]) >= f) 61 | return false; 62 | } 63 | return true; 64 | 65 | } 66 | 67 | void computeDestinationValues() { 68 | // to value assuming final velocity as a factor of dynamics threshold 69 | // derived from v' = v * d^dt used in decay_position 70 | // to compute the to value with maximal dt, p' = p + (v * d) / (1 - d) 71 | VectorRef fromValue = NULL != currentVec ? currentVec : fromVec; 72 | if (!fromValue) { 73 | return; 74 | } 75 | 76 | VectorRef toValue(Vector::new_vector(fromValue.get())); 77 | 78 | // compute duration till threshold velocity 79 | Vector4r scaledVelocity = vector4(velocityVec) / 1000.; 80 | 81 | double k = dynamicsThreshold * kPOPAnimationDecayMinimalVelocityFactor / 1000.; 82 | double vx = k / scaledVelocity.x; 83 | double vy = k / scaledVelocity.y; 84 | double vz = k / scaledVelocity.z; 85 | double vw = k / scaledVelocity.w; 86 | double d = log(deceleration) * 1000.; 87 | duration = MAX(MAX(MAX(log(fabs(vx)) / d, log(fabs(vy)) / d), log(fabs(vz)) / d), log(fabs(vw)) / d); 88 | 89 | // ensure velocity threshold is exceeded 90 | if (isnan(duration) || duration < 0) { 91 | duration = 0; 92 | } else { 93 | // compute to value 94 | Vector4r velocity = velocityVec->vector4r(); 95 | decay_position(toValue->data(), velocity.data(), valueCount, duration, deceleration); 96 | } 97 | 98 | toVec = toValue; 99 | } 100 | 101 | bool advance(CFTimeInterval time, CFTimeInterval dt, id obj) { 102 | // advance past not yet initialized animations 103 | if (NULL == currentVec) { 104 | return false; 105 | } 106 | 107 | decay_position(currentVec->data(), velocityVec->data(), valueCount, dt, deceleration); 108 | 109 | // clamp to compute end value; avoid possibility of decaying past 110 | clampCurrentValue(kPOPAnimationClampEnd | clampMode); 111 | 112 | return true; 113 | } 114 | 115 | }; 116 | 117 | typedef struct _POPDecayAnimationState POPDecayAnimationState; 118 | -------------------------------------------------------------------------------- /TestPop/Controllers/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // TestPop 4 | // 5 | // Created by XuDong Jin on 14-5-1. 6 | // Copyright (c) 2014年 XuDong Jin. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | 11 | @interface ViewController () 12 | { 13 | NSArray *dataArray; 14 | } 15 | @end 16 | 17 | @implementation ViewController 18 | 19 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 20 | { 21 | self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 22 | if (self) { 23 | // Custom initialization 24 | } 25 | return self; 26 | } 27 | 28 | - (void)viewDidLoad 29 | { 30 | [super viewDidLoad]; 31 | // Do any additional setup after loading the view from its nib. 32 | 33 | 34 | NSDictionary *springDic = @{@"type":@"POPSpringAnimation",@"animations":@[@"kPOPLayerSize",@"kPOPLayerPosition",@"kPOPViewFrame"]}; 35 | NSDictionary *basicDic = @{@"type":@"POPBasicAnimation",@"animations":@[@"kPOPViewAlpha"]}; 36 | NSDictionary *combinDic = @{@"type":@"CombineAnimation",@"animations":@[@" POPbox"]}; 37 | 38 | dataArray = @[springDic,basicDic,combinDic]; 39 | 40 | 41 | self.title = @"POP"; 42 | 43 | 44 | /* 45 | POPSpringAnimation 有弹簧效果的动画类 46 | 47 | POPBasicAnimation 基本动画类 48 | 49 | POPDecayAnimation 衰减动画类 50 | 51 | POPCustomAnimation 可以自定义动画的类 52 | */ 53 | } 54 | 55 | #pragma mark - UITableView Datasource 56 | 57 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 58 | return dataArray.count; 59 | } 60 | 61 | - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 62 | return 30; 63 | } 64 | 65 | - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 66 | UILabel *senctionHeader = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)]; 67 | senctionHeader.text = dataArray[section][@"type"]; 68 | senctionHeader.backgroundColor = [UIColor lightGrayColor]; 69 | senctionHeader.textAlignment = NSTextAlignmentCenter; 70 | senctionHeader.textColor = [UIColor whiteColor]; 71 | senctionHeader.font = [UIFont systemFontOfSize:14]; 72 | return senctionHeader; 73 | } 74 | 75 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 76 | NSArray *animations = dataArray[section][@"animations"]; 77 | return animations.count; 78 | } 79 | 80 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 81 | static NSString *cellIdentifier = @"Cell"; 82 | 83 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 84 | 85 | if(cell == nil) { 86 | cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 87 | } 88 | 89 | NSArray *animations = dataArray[indexPath.section][@"animations"]; 90 | 91 | cell.textLabel.text = animations[indexPath.row]; 92 | cell.textLabel.font = [UIFont systemFontOfSize:13]; 93 | 94 | return cell; 95 | } 96 | 97 | #pragma mark - UITableView Delegate methods 98 | 99 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 100 | return 44; 101 | } 102 | 103 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 104 | NSString *sectionName = dataArray[indexPath.section][@"type"]; 105 | NSString *classNamePre = [sectionName stringByReplacingOccurrencesOfString:@"Animation" withString:@""]; 106 | NSArray *animations = dataArray[indexPath.section][@"animations"]; 107 | NSString *classNameSuf = [animations[indexPath.row] substringFromIndex:1]; 108 | NSString *classNmae = [classNamePre stringByAppendingString:classNameSuf]; 109 | 110 | Class class = NSClassFromString([classNamePre stringByAppendingString:classNameSuf]); 111 | 112 | UIViewController *ctrl = [[class alloc] initWithNibName:classNmae bundle:nil]; 113 | ctrl.title = classNameSuf; 114 | 115 | [self.navigationController pushViewController:ctrl animated:YES]; 116 | } 117 | 118 | 119 | - (void)didReceiveMemoryWarning 120 | { 121 | [super didReceiveMemoryWarning]; 122 | // Dispose of any resources that can be recreated. 123 | } 124 | 125 | @end 126 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimation.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import 11 | 12 | #import "POPAnimationTracer.h" 13 | #import "POPGeometry.h" 14 | 15 | @class CAMediaTimingFunction; 16 | 17 | /** 18 | @abstract The abstract animation base class. 19 | @discussion Instantiate and use one of the concrete animation subclasses. 20 | */ 21 | @interface POPAnimation : NSObject 22 | 23 | /** 24 | @abstract The name of the animation. 25 | @discussion Optional property to help identify the animation. 26 | */ 27 | @property (copy, nonatomic) NSString *name; 28 | 29 | /** 30 | @abstract The beginTime of the animation in media time. 31 | @discussion Defaults to 0 and starts immediately. 32 | */ 33 | @property (assign, nonatomic) CFTimeInterval beginTime; 34 | 35 | /** 36 | @abstract The animation delegate. 37 | @discussion See {@ref POPAnimationDelegate} for details. 38 | */ 39 | @property (weak, nonatomic) id delegate; 40 | 41 | /** 42 | @abstract The animation tracer. 43 | @discussion Returns the existing tracer, creating one if needed. Call start/stop on the tracer to toggle event collection. 44 | */ 45 | @property (readonly, nonatomic) POPAnimationTracer *tracer; 46 | 47 | /** 48 | @abstract Optional block called on animation completion. 49 | */ 50 | @property (copy, nonatomic) void (^completionBlock)(POPAnimation *anim, BOOL finished); 51 | 52 | /** 53 | @abstract Flag indicating whether animation should be removed on completion. 54 | @discussion Setting to NO can facilitate animation reuse. Defaults to YES. 55 | */ 56 | @property (assign, nonatomic) BOOL removedOnCompletion; 57 | 58 | /** 59 | @abstract Flag indicating whether animation is paused. 60 | @discussion A paused animation is excluded from the list of active animations. On initial creation, defaults to YES. On animation addition, the animation is implicity unpaused. On animation completion, the animation is implicity paused including for animations with removedOnCompletion set to NO. 61 | */ 62 | @property (assign, nonatomic, getter = isPaused) BOOL paused; 63 | 64 | @end 65 | 66 | /** 67 | @abstract The animation delegate. 68 | */ 69 | @protocol POPAnimationDelegate 70 | @optional 71 | 72 | /** 73 | @abstract Called on animation start. 74 | @param anim The relevant animation. 75 | */ 76 | - (void)pop_animationDidStart:(POPAnimation *)anim; 77 | 78 | /** 79 | @abstract Called when value meets or exceeds to value. 80 | @param anim The relevant animation. 81 | */ 82 | - (void)pop_animationDidReachToValue:(POPAnimation *)anim; 83 | 84 | /** 85 | @abstract Called on animation stop. 86 | @param anim The relevant animation. 87 | @param finished Flag indicating finished state. Flag is true if the animation reached completion before being removed. 88 | */ 89 | - (void)pop_animationDidStop:(POPAnimation *)anim finished:(BOOL)finished; 90 | 91 | /** 92 | @abstract Called each frame animation is applied. 93 | @param anim The relevant animation. 94 | */ 95 | - (void)pop_animationDidApply:(POPAnimation *)anim; 96 | 97 | @end 98 | 99 | 100 | @interface NSObject (POP) 101 | 102 | /** 103 | @abstract Add an animation to the reciver. 104 | @param anim The animation to add. 105 | @param key The key used to identify the animation. 106 | @discussion The 'key' may be any string such that only one animation per unique key is added per object. 107 | */ 108 | - (void)pop_addAnimation:(POPAnimation *)anim forKey:(NSString *)key; 109 | 110 | /** 111 | @abstract Remove all animations attached to the receiver. 112 | */ 113 | - (void)pop_removeAllAnimations; 114 | 115 | /** 116 | @abstract Remove any animation attached to the receiver for 'key'. 117 | @param key The key used to identify the animation. 118 | */ 119 | - (void)pop_removeAnimationForKey:(NSString *)key; 120 | 121 | /** 122 | @abstract Returns an array containing the keys of all animations currently attached to the receiver. 123 | @param The order of keys reflects the order in which animations will be applied. 124 | */ 125 | - (NSArray *)pop_animationKeys; 126 | 127 | /** 128 | @abstract Returns any animation attached to the receiver. 129 | @param key The key used to identify the animation. 130 | @returns The animation currently attached, or nil if no such animation exists. 131 | */ 132 | - (id)pop_animationForKey:(NSString *)key; 133 | 134 | @end 135 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPSpringAnimation.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPSpringAnimationInternal.h" 11 | 12 | @implementation POPSpringAnimation 13 | 14 | #pragma mark - Lifecycle 15 | 16 | #undef __state 17 | #define __state ((POPSpringAnimationState *)_state) 18 | 19 | + (instancetype)animation 20 | { 21 | return [[self alloc] init]; 22 | } 23 | 24 | + (instancetype)animationWithPropertyNamed:(NSString *)aName 25 | { 26 | POPSpringAnimation *anim = [self animation]; 27 | anim.property = [POPAnimatableProperty propertyWithName:aName]; 28 | return anim; 29 | } 30 | 31 | - (void)_initState 32 | { 33 | _state = new POPSpringAnimationState(self); 34 | } 35 | 36 | - (id)init 37 | { 38 | self = [super _init]; 39 | if (nil != self) { 40 | __state->solver = new SpringSolver4d(1, 1, 1); 41 | __state->updatedDynamicsThreshold(); 42 | __state->updatedBouncinessAndSpeed(); 43 | } 44 | return self; 45 | } 46 | 47 | - (void)dealloc 48 | { 49 | if (__state) { 50 | delete __state->solver; 51 | __state->solver = NULL; 52 | } 53 | } 54 | 55 | #pragma mark - Properties 56 | 57 | - (id)velocity 58 | { 59 | return POPBox(__state->velocityVec, __state->valueType); 60 | } 61 | 62 | - (void)setVelocity:(id)aValue 63 | { 64 | POPPropertyAnimationState *s = __state; 65 | VectorRef vec = POPUnbox(aValue, s->valueType, s->valueCount, YES); 66 | if (!vec_equal(vec, s->velocityVec)) { 67 | s->velocityVec = vec; 68 | 69 | if (s->tracing) { 70 | [s->tracer updateVelocity:aValue]; 71 | } 72 | } 73 | } 74 | 75 | DEFINE_RW_PROPERTY(POPSpringAnimationState, dynamicsTension, setDynamicsTension:, CGFloat, [self _updatedDynamicsTension];); 76 | DEFINE_RW_PROPERTY(POPSpringAnimationState, dynamicsFriction, setDynamicsFriction:, CGFloat, [self _updatedDynamicsFriction];); 77 | DEFINE_RW_PROPERTY(POPSpringAnimationState, dynamicsMass, setDynamicsMass:, CGFloat, [self _updatedDynamicsMass];); 78 | 79 | FB_PROPERTY_GET(POPSpringAnimationState, springSpeed, CGFloat); 80 | - (void)setSpringSpeed:(CGFloat)aFloat 81 | { 82 | POPSpringAnimationState *s = __state; 83 | if (s->userSpecifiedDynamics || aFloat != s->springSpeed) { 84 | s->springSpeed = aFloat; 85 | s->userSpecifiedDynamics = false; 86 | s->updatedBouncinessAndSpeed(); 87 | if (s->tracing) { 88 | [s->tracer updateSpeed:aFloat]; 89 | } 90 | } 91 | } 92 | 93 | FB_PROPERTY_GET(POPSpringAnimationState, springBounciness, CGFloat); 94 | - (void)setSpringBounciness:(CGFloat)aFloat 95 | { 96 | POPSpringAnimationState *s = __state; 97 | if (s->userSpecifiedDynamics || aFloat != s->springBounciness) { 98 | s->springBounciness = aFloat; 99 | s->userSpecifiedDynamics = false; 100 | s->updatedBouncinessAndSpeed(); 101 | if (s->tracing) { 102 | [s->tracer updateBounciness:aFloat]; 103 | } 104 | } 105 | } 106 | 107 | - (SpringSolver4d *)solver 108 | { 109 | return __state->solver; 110 | } 111 | 112 | - (void)setSolver:(SpringSolver4d *)aSolver 113 | { 114 | if (aSolver != __state->solver) { 115 | if (__state->solver) { 116 | delete(__state->solver); 117 | } 118 | __state->solver = aSolver; 119 | } 120 | } 121 | 122 | #pragma mark - Utility 123 | 124 | - (void)_updatedDynamicsTension 125 | { 126 | __state->userSpecifiedDynamics = true; 127 | if(__state->tracing) { 128 | [__state->tracer updateTension:__state->dynamicsTension]; 129 | } 130 | __state->updatedDynamics(); 131 | } 132 | 133 | - (void)_updatedDynamicsFriction 134 | { 135 | __state->userSpecifiedDynamics = true; 136 | if(__state->tracing) { 137 | [__state->tracer updateFriction:__state->dynamicsFriction]; 138 | } 139 | __state->updatedDynamics(); 140 | } 141 | 142 | - (void)_updatedDynamicsMass 143 | { 144 | __state->userSpecifiedDynamics = true; 145 | if(__state->tracing) { 146 | [__state->tracer updateMass:__state->dynamicsMass]; 147 | } 148 | __state->updatedDynamics(); 149 | } 150 | 151 | - (void)_appendDescription:(NSMutableString *)s debug:(BOOL)debug 152 | { 153 | [super _appendDescription:s debug:debug]; 154 | 155 | if (debug) { 156 | if (_state->userSpecifiedDynamics) { 157 | [s appendFormat:@"; dynamics = (tension:%f, friction:%f, mass:%f)", __state->dynamicsTension, __state->dynamicsFriction, __state->dynamicsMass]; 158 | } else { 159 | [s appendFormat:@"; bounciness = %f; speed = %f", __state->springBounciness, __state->springSpeed]; 160 | } 161 | } 162 | } 163 | 164 | @end 165 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimationExtras.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPAnimationExtras.h" 11 | 12 | #import "POPAnimationPrivate.h" 13 | #import "POPMath.h" 14 | 15 | #if TARGET_OS_IPHONE 16 | #import 17 | #endif 18 | 19 | #if TARGET_IPHONE_SIMULATOR 20 | UIKIT_EXTERN CGFloat UIAnimationDragCoefficient(); // UIKit private drag coeffient, use judiciously 21 | #endif 22 | 23 | CGFloat POPAnimationDragCoefficient() 24 | { 25 | #if TARGET_IPHONE_SIMULATOR 26 | return UIAnimationDragCoefficient(); 27 | #else 28 | return 1.0; 29 | #endif 30 | } 31 | 32 | @implementation CAAnimation (POPAnimationExtras) 33 | 34 | - (void)pop_applyDragCoefficient 35 | { 36 | CGFloat k = POPAnimationDragCoefficient(); 37 | if (k != 0 && k != 1) 38 | self.speed = 1 / k; 39 | } 40 | 41 | @end 42 | 43 | @implementation POPSpringAnimation (POPAnimationExtras) 44 | 45 | static const CGFloat POPBouncy3NormalizationRange = 20.0; 46 | static const CGFloat POPBouncy3NormalizationScale = 1.7; 47 | static const CGFloat POPBouncy3BouncinessNormalizedMin = 0.0; 48 | static const CGFloat POPBouncy3BouncinessNormalizedMax = 0.8; 49 | static const CGFloat POPBouncy3SpeedNormalizedMin = 0.5; 50 | static const CGFloat POPBouncy3SpeedNormalizedMax = 200; 51 | static const CGFloat POPBouncy3FrictionInterpolationMax = 0.01; 52 | 53 | + (void)convertBounciness:(CGFloat)bounciness speed:(CGFloat)speed toTension:(CGFloat *)outTension friction:(CGFloat *)outFriction mass:(CGFloat *)outMass 54 | { 55 | double b = normalize(bounciness / POPBouncy3NormalizationScale, 0, POPBouncy3NormalizationRange); 56 | b = project_normal(b, POPBouncy3BouncinessNormalizedMin, POPBouncy3BouncinessNormalizedMax); 57 | 58 | double s = normalize(speed / POPBouncy3NormalizationScale, 0, POPBouncy3NormalizationRange); 59 | 60 | CGFloat tension = project_normal(s, POPBouncy3SpeedNormalizedMin, POPBouncy3SpeedNormalizedMax); 61 | CGFloat friction = quadratic_out_interpolation(b, b3_nobounce(tension), POPBouncy3FrictionInterpolationMax); 62 | 63 | tension = POP_ANIMATION_TENSION_FOR_QC_TENSION(tension); 64 | friction = POP_ANIMATION_FRICTION_FOR_QC_FRICTION(friction); 65 | 66 | if (outTension) { 67 | *outTension = tension; 68 | } 69 | 70 | if (outFriction) { 71 | *outFriction = friction; 72 | } 73 | 74 | if (outMass) { 75 | *outMass = 1.0; 76 | } 77 | } 78 | 79 | + (void)convertTension:(CGFloat)tension friction:(CGFloat)friction toBounciness:(CGFloat *)outBounciness speed:(CGFloat *)outSpeed 80 | { 81 | // Convert to QC values, in which our calculations are done. 82 | CGFloat qcFriction = QC_FRICTION_FOR_POP_ANIMATION_FRICTION(friction); 83 | CGFloat qcTension = QC_TENSION_FOR_POP_ANIMATION_TENSION(tension); 84 | 85 | // Friction is a function of bounciness and tension, according to the following: 86 | // friction = quadratic_out_interpolation(b, b3_nobounce(tension), POPBouncy3FrictionInterpolationMax); 87 | // Solve for bounciness, given a tension and friction. 88 | 89 | CGFloat nobounceTension = b3_nobounce(qcTension); 90 | CGFloat bounciness1, bounciness2; 91 | 92 | quadratic_solve((nobounceTension - POPBouncy3FrictionInterpolationMax), // a 93 | 2 * (POPBouncy3FrictionInterpolationMax - nobounceTension), // b 94 | (nobounceTension - qcFriction), // c 95 | bounciness1, // x1 96 | bounciness2); // x2 97 | 98 | 99 | // Choose the quadratic solution within the normalized bounciness range 100 | CGFloat projectedNormalizedBounciness = (bounciness2 < POPBouncy3BouncinessNormalizedMax) ? bounciness2 : bounciness1; 101 | CGFloat projectedNormalizedSpeed = qcTension; 102 | 103 | // Reverse projection + normalization 104 | CGFloat bounciness = ((POPBouncy3NormalizationRange * POPBouncy3NormalizationScale) / (POPBouncy3BouncinessNormalizedMax - POPBouncy3BouncinessNormalizedMin)) * (projectedNormalizedBounciness - POPBouncy3BouncinessNormalizedMin); 105 | CGFloat speed = ((POPBouncy3NormalizationRange * POPBouncy3NormalizationScale) / (POPBouncy3SpeedNormalizedMax - POPBouncy3SpeedNormalizedMin)) * (projectedNormalizedSpeed - POPBouncy3SpeedNormalizedMin); 106 | 107 | // Write back results 108 | if (outBounciness) { 109 | *outBounciness = bounciness; 110 | } 111 | 112 | if (outSpeed) { 113 | *outSpeed = speed; 114 | } 115 | } 116 | 117 | @end 118 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimatableProperty.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import 11 | #import 12 | 13 | @class POPMutableAnimatableProperty; 14 | 15 | /** 16 | @abstract Describes an animatable property. 17 | */ 18 | @interface POPAnimatableProperty : NSObject 19 | 20 | /** 21 | @abstract Property accessor. 22 | @param name The name of the property. 23 | @return The animatable property with that name or nil if it does not exist. 24 | @discussion Common animatable properties are included by default. Use the provided constants to reference. 25 | */ 26 | + (id)propertyWithName:(NSString *)name; 27 | 28 | /** 29 | @abstract The designated initializer. 30 | @param name The name of the property. 31 | @param block The block used to configure the property on creation. 32 | @return The animatable property with name if it exists, otherwise a newly created instance configured by block. 33 | @discussion Custom properties should use reverse-DNS naming. A newly created instance is only mutable in the scope of block. Once constructed, a property becomes immutable. 34 | */ 35 | + (id)propertyWithName:(NSString *)name initializer:(void (^)(POPMutableAnimatableProperty *prop))block; 36 | 37 | /** 38 | @abstract The name of the property. 39 | @discussion Used to uniquely identify an animatable property. 40 | */ 41 | @property (readonly, nonatomic, copy) NSString *name; 42 | 43 | /** 44 | @abstract Block used to read values from a property into an array of floats. 45 | */ 46 | @property (readonly, nonatomic, copy) void (^readBlock)(id obj, CGFloat values[]); 47 | 48 | /** 49 | @abstract Block used to write values from an array of floats into a property. 50 | */ 51 | @property (readonly, nonatomic, copy) void (^writeBlock)(id obj, const CGFloat values[]); 52 | 53 | /** 54 | @abstract The threshold value used when determining completion of dynamics simulations. 55 | */ 56 | @property (readonly, nonatomic, assign) CGFloat threshold; 57 | 58 | @end 59 | 60 | /** 61 | @abstract A mutable animatable property intended for configuration. 62 | */ 63 | @interface POPMutableAnimatableProperty : POPAnimatableProperty 64 | 65 | /** 66 | @abstract A read-write version of POPAnimatableProperty name property. 67 | */ 68 | @property (readwrite, nonatomic, copy) NSString *name; 69 | 70 | /** 71 | @abstract A read-write version of POPAnimatableProperty readBlock property. 72 | */ 73 | @property (readwrite, nonatomic, copy) void (^readBlock)(id obj, CGFloat values[]); 74 | 75 | /** 76 | @abstract A read-write version of POPAnimatableProperty writeBlock property. 77 | */ 78 | @property (readwrite, nonatomic, copy) void (^writeBlock)(id obj, const CGFloat values[]); 79 | 80 | /** 81 | @abstract A read-write version of POPAnimatableProperty threshold property. 82 | */ 83 | @property (readwrite, nonatomic, assign) CGFloat threshold; 84 | 85 | @end 86 | 87 | /** 88 | Common CALayer property names. 89 | */ 90 | extern NSString * const kPOPLayerBackgroundColor; 91 | extern NSString * const kPOPLayerBounds; 92 | extern NSString * const kPOPLayerOpacity; 93 | extern NSString * const kPOPLayerPosition; 94 | extern NSString * const kPOPLayerPositionX; 95 | extern NSString * const kPOPLayerPositionY; 96 | extern NSString * const kPOPLayerRotation; 97 | extern NSString * const kPOPLayerRotationX; 98 | extern NSString * const kPOPLayerRotationY; 99 | extern NSString * const kPOPLayerScaleX; 100 | extern NSString * const kPOPLayerScaleXY; 101 | extern NSString * const kPOPLayerScaleY; 102 | extern NSString * const kPOPLayerSize; 103 | extern NSString * const kPOPLayerSubscaleXY; 104 | extern NSString * const kPOPLayerSubtranslationX; 105 | extern NSString * const kPOPLayerSubtranslationXY; 106 | extern NSString * const kPOPLayerSubtranslationY; 107 | extern NSString * const kPOPLayerSubtranslationZ; 108 | extern NSString * const kPOPLayerTranslationX; 109 | extern NSString * const kPOPLayerTranslationXY; 110 | extern NSString * const kPOPLayerTranslationY; 111 | extern NSString * const kPOPLayerTranslationZ; 112 | extern NSString * const kPOPLayerZPosition; 113 | 114 | 115 | #if TARGET_OS_IPHONE 116 | 117 | /** 118 | Common UIView property names. 119 | */ 120 | extern NSString * const kPOPViewAlpha; 121 | extern NSString * const kPOPViewBackgroundColor; 122 | extern NSString * const kPOPViewBounds; 123 | extern NSString * const kPOPViewCenter; 124 | extern NSString * const kPOPViewFrame; 125 | extern NSString * const kPOPViewScaleX; 126 | extern NSString * const kPOPViewScaleXY; 127 | extern NSString * const kPOPViewScaleY; 128 | extern NSString * const kPOPViewSize; 129 | 130 | 131 | /** 132 | Common UITableView property names. 133 | */ 134 | extern NSString * const kPOPTableViewContentOffset; 135 | extern NSString * const kPOPTableViewContentSize; 136 | 137 | 138 | extern NSString * const kPOPLabelFontSize; 139 | 140 | #endif 141 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimationTracer.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPAnimationTracer.h" 11 | 12 | #import 13 | 14 | #import "POPAnimationEventInternal.h" 15 | #import "POPAnimationInternal.h" 16 | #import "POPSpringAnimation.h" 17 | 18 | @implementation POPAnimationTracer 19 | { 20 | __weak POPAnimation *_animation; 21 | POPAnimationState *_animationState; 22 | NSMutableArray *_events; 23 | BOOL _animationHasVelocity; 24 | } 25 | 26 | static POPAnimationEvent *create_event(POPAnimationTracer *self, POPAnimationEventType type, id value = nil, bool recordAnimation = false) 27 | { 28 | bool useLocalTime = 0 != self->_animationState->startTime; 29 | CFTimeInterval time = useLocalTime 30 | ? self->_animationState->lastTime - self->_animationState->startTime 31 | : self->_animationState->lastTime; 32 | 33 | POPAnimationEvent *event; 34 | 35 | if (!value) { 36 | event = [[POPAnimationEvent alloc] initWithType:type time:time]; 37 | } else { 38 | event = [[POPAnimationValueEvent alloc] initWithType:type time:time value:value]; 39 | if (self->_animationHasVelocity) { 40 | [(POPAnimationValueEvent *)event setVelocity:[(POPSpringAnimation *)self->_animation velocity]]; 41 | } 42 | } 43 | 44 | if (recordAnimation) { 45 | event.animationDescription = [self->_animation description]; 46 | } 47 | 48 | return event; 49 | } 50 | 51 | - (id)initWithAnimation:(POPAnimation *)anAnim 52 | { 53 | self = [super init]; 54 | if (nil != self) { 55 | _animation = anAnim; 56 | _animationState = POPAnimationGetState(anAnim); 57 | _events = [[NSMutableArray alloc] initWithCapacity:50]; 58 | _animationHasVelocity = [anAnim respondsToSelector:@selector(velocity)]; 59 | } 60 | return self; 61 | } 62 | 63 | - (void)readPropertyValue:(id)aValue 64 | { 65 | POPAnimationEvent *event = create_event(self, kPOPAnimationEventPropertyRead, aValue); 66 | [_events addObject:event]; 67 | } 68 | 69 | - (void)writePropertyValue:(id)aValue 70 | { 71 | POPAnimationEvent *event = create_event(self, kPOPAnimationEventPropertyWrite, aValue); 72 | [_events addObject:event]; 73 | } 74 | 75 | - (void)updateToValue:(id)aValue 76 | { 77 | POPAnimationEvent *event = create_event(self, kPOPAnimationEventToValueUpdate, aValue); 78 | [_events addObject:event]; 79 | } 80 | 81 | - (void)updateFromValue:(id)aValue 82 | { 83 | POPAnimationEvent *event = create_event(self, kPOPAnimationEventFromValueUpdate, aValue); 84 | [_events addObject:event]; 85 | } 86 | 87 | - (void)updateVelocity:(id)aValue 88 | { 89 | POPAnimationEvent *event = create_event(self, kPOPAnimationEventVelocityUpdate, aValue); 90 | [_events addObject:event]; 91 | } 92 | 93 | - (void)updateSpeed:(float)aFloat 94 | { 95 | POPAnimationEvent *event = create_event(self, kPOPAnimationEventSpeedUpdate, @(aFloat)); 96 | [_events addObject:event]; 97 | } 98 | 99 | - (void)updateBounciness:(float)aFloat 100 | { 101 | POPAnimationEvent *event = create_event(self, kPOPAnimationEventBouncinessUpdate, @(aFloat)); 102 | [_events addObject:event]; 103 | } 104 | 105 | - (void)updateFriction:(float)aFloat 106 | { 107 | POPAnimationEvent *event = create_event(self, kPOPAnimationEventFrictionUpdate, @(aFloat)); 108 | [_events addObject:event]; 109 | } 110 | 111 | - (void)updateMass:(float)aFloat 112 | { 113 | POPAnimationEvent *event = create_event(self, kPOPAnimationEventMassUpdate, @(aFloat)); 114 | [_events addObject:event]; 115 | } 116 | 117 | - (void)updateTension:(float)aFloat 118 | { 119 | POPAnimationEvent *event = create_event(self, kPOPAnimationEventTensionUpdate, @(aFloat)); 120 | [_events addObject:event]; 121 | } 122 | 123 | - (void)didStart 124 | { 125 | POPAnimationEvent *event = create_event(self, kPOPAnimationEventDidStart, nil, true); 126 | [_events addObject:event]; 127 | } 128 | 129 | - (void)didStop:(BOOL)finished 130 | { 131 | POPAnimationEvent *event = create_event(self, kPOPAnimationEventDidStop, @(finished), true); 132 | [_events addObject:event]; 133 | 134 | if (_shouldLogAndResetOnCompletion) { 135 | NSLog(@"events:%@", self.allEvents); 136 | [self reset]; 137 | } 138 | } 139 | 140 | - (void)didReachToValue:(id)aValue 141 | { 142 | POPAnimationEvent *event = create_event(self, kPOPAnimationEventDidReachToValue, aValue); 143 | [_events addObject:event]; 144 | } 145 | 146 | - (void)start 147 | { 148 | POPAnimationState *s = POPAnimationGetState(_animation); 149 | s->tracing = true; 150 | } 151 | 152 | - (void)stop 153 | { 154 | POPAnimationState *s = POPAnimationGetState(_animation); 155 | s->tracing = false; 156 | } 157 | 158 | - (void)reset 159 | { 160 | [_events removeAllObjects]; 161 | } 162 | 163 | - (NSArray *)allEvents 164 | { 165 | return [_events copy]; 166 | } 167 | 168 | - (NSArray *)writeEvents 169 | { 170 | return [self eventsWithType:kPOPAnimationEventPropertyWrite]; 171 | } 172 | 173 | - (NSArray *)eventsWithType:(POPAnimationEventType)aType 174 | { 175 | NSMutableArray *array = [NSMutableArray array]; 176 | for (POPAnimationEvent *event in _events) { 177 | if (aType == event.type) { 178 | [array addObject:event]; 179 | } 180 | } 181 | return array; 182 | } 183 | 184 | @end 185 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPSpringSolver.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import 11 | 12 | #import "POPVector.h" 13 | 14 | namespace POP { 15 | 16 | template 17 | struct SSState 18 | { 19 | T p; 20 | T v; 21 | }; 22 | 23 | template 24 | struct SSDerivative 25 | { 26 | T dp; 27 | T dv; 28 | }; 29 | 30 | typedef SSState SSState4d; 31 | typedef SSDerivative SSDerivative4d; 32 | 33 | const CFTimeInterval solverDt = 0.001f; 34 | const CFTimeInterval maxSolverDt = 30.0f; 35 | 36 | /** 37 | Templated spring solver class. 38 | */ 39 | template 40 | class SpringSolver 41 | { 42 | double _k; // stiffness 43 | double _b; // dampening 44 | double _m; // mass 45 | 46 | double _tp; // threshold 47 | double _tv; // threshold velocity 48 | double _ta; // threshold acceleration 49 | 50 | CFTimeInterval _accumulatedTime; 51 | SSState _lastState; 52 | T _lastDv; 53 | bool _started; 54 | 55 | public: 56 | SpringSolver(double k, double b, double m = 1) : _k(k), _b(b), _m(m), _started(false) 57 | { 58 | _accumulatedTime = 0; 59 | _lastState.p = T::Zero(); 60 | _lastState.v = T::Zero(); 61 | _lastDv = T::Zero(); 62 | setThreshold(1.); 63 | } 64 | 65 | ~SpringSolver() 66 | { 67 | } 68 | 69 | bool started() 70 | { 71 | return _started; 72 | } 73 | 74 | void setConstants(double k, double b, double m) 75 | { 76 | _k = k; 77 | _b = b; 78 | _m = m; 79 | } 80 | 81 | void setThreshold(double t) 82 | { 83 | _tp = t / 2; // half a unit 84 | _tv = 25.0 * t; // 5 units per second, squared for comparison 85 | _ta = 625.0 * t * t; // 5 units per second squared, squared for comparison 86 | } 87 | 88 | T acceleration(const SSState &state, double t) 89 | { 90 | return state.p*(-_k/_m) - state.v*(_b/_m); 91 | } 92 | 93 | SSDerivative evaluate(const SSState &initial, double t) 94 | { 95 | SSDerivative output; 96 | output.dp = initial.v; 97 | output.dv = acceleration(initial, t); 98 | return output; 99 | } 100 | 101 | SSDerivative evaluate(const SSState &initial, double t, double dt, const SSDerivative &d) 102 | { 103 | SSState state; 104 | state.p = initial.p + d.dp*dt; 105 | state.v = initial.v + d.dv*dt; 106 | SSDerivative output; 107 | output.dp = state.v; 108 | output.dv = acceleration(state, t+dt); 109 | return output; 110 | } 111 | 112 | void integrate(SSState &state, double t, double dt) 113 | { 114 | SSDerivative a = evaluate(state, t); 115 | SSDerivative b = evaluate(state, t, dt*0.5, a); 116 | SSDerivative c = evaluate(state, t, dt*0.5, b); 117 | SSDerivative d = evaluate(state, t, dt, c); 118 | 119 | T dpdt = (a.dp + (b.dp + c.dp)*2.0 + d.dp) * (1.0/6.0); 120 | T dvdt = (a.dv + (b.dv + c.dv)*2.0 + d.dv) * (1.0/6.0); 121 | 122 | state.p = state.p + dpdt*dt; 123 | state.v = state.v + dvdt*dt; 124 | 125 | _lastDv = dvdt; 126 | } 127 | 128 | SSState interpolate(const SSState &previous, const SSState ¤t, double alpha) 129 | { 130 | SSState state; 131 | state.p = current.p*alpha + previous.p*(1-alpha); 132 | state.v = current.v*alpha + previous.v*(1-alpha); 133 | return state; 134 | } 135 | 136 | void advance(SSState &state, double t, double dt) 137 | { 138 | _started = true; 139 | 140 | if (dt > maxSolverDt) { 141 | // excessive time step, force shut down 142 | _lastDv = _lastState.v = _lastState.p = T::Zero(); 143 | } else { 144 | _accumulatedTime += dt; 145 | 146 | SSState previousState = state, currentState = state; 147 | while (_accumulatedTime >= solverDt) { 148 | previousState = currentState; 149 | this->integrate(currentState, t, solverDt); 150 | t += solverDt; 151 | _accumulatedTime -= solverDt; 152 | } 153 | CFTimeInterval alpha = _accumulatedTime / solverDt; 154 | _lastState = state = this->interpolate(previousState, currentState, alpha); 155 | } 156 | } 157 | 158 | bool hasConverged() 159 | { 160 | if (!_started) { 161 | return false; 162 | } 163 | 164 | for (int idx = 0; idx < _lastState.p.size(); idx++) { 165 | if (fabs(_lastState.p(idx)) >= _tp) { 166 | return false; 167 | } 168 | } 169 | 170 | return (_lastState.v.squaredNorm() < _tv) && (_lastDv.squaredNorm() < _ta); 171 | } 172 | 173 | void reset() 174 | { 175 | _accumulatedTime = 0; 176 | _lastState.p = T::Zero(); 177 | _lastState.v = T::Zero(); 178 | _lastDv = T::Zero(); 179 | _started = false; 180 | } 181 | }; 182 | 183 | /** 184 | Convenience spring solver type definitions. 185 | */ 186 | typedef SpringSolver SpringSolver2d; 187 | typedef SpringSolver SpringSolver3d; 188 | typedef SpringSolver SpringSolver4d; 189 | } 190 | 191 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPLayerExtras.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import 11 | 12 | #import "POPDefines.h" 13 | 14 | POP_EXTERN_C_BEGIN 15 | 16 | #pragma mark - Scale 17 | 18 | /** 19 | @abstract Returns layer scale factor for the x axis. 20 | */ 21 | extern CGFloat POPLayerGetScaleX(CALayer *l); 22 | 23 | /** 24 | @abstract Set layer scale factor for the x axis. 25 | */ 26 | extern void POPLayerSetScaleX(CALayer *l, CGFloat f); 27 | 28 | /** 29 | @abstract Returns layer scale factor for the y axis. 30 | */ 31 | extern CGFloat POPLayerGetScaleY(CALayer *l); 32 | 33 | /** 34 | @abstract Set layer scale factor for the y axis. 35 | */ 36 | extern void POPLayerSetScaleY(CALayer *l, CGFloat f); 37 | 38 | /** 39 | @abstract Returns layer scale factor for the z axis. 40 | */ 41 | extern CGFloat POPLayerGetScaleZ(CALayer *l); 42 | 43 | /** 44 | @abstract Set layer scale factor for the z axis. 45 | */ 46 | extern void POPLayerSetScaleZ(CALayer *l, CGFloat f); 47 | 48 | /** 49 | @abstract Returns layer scale factors for x and y access as point. 50 | */ 51 | extern CGPoint POPLayerGetScaleXY(CALayer *l); 52 | 53 | /** 54 | @abstract Sets layer x and y scale factors given point. 55 | */ 56 | extern void POPLayerSetScaleXY(CALayer *l, CGPoint p); 57 | 58 | #pragma mark - Translation 59 | 60 | /** 61 | @abstract Returns layer translation factor for the x axis. 62 | */ 63 | extern CGFloat POPLayerGetTranslationX(CALayer *l); 64 | 65 | /** 66 | @abstract Set layer translation factor for the x axis. 67 | */ 68 | extern void POPLayerSetTranslationX(CALayer *l, CGFloat f); 69 | 70 | /** 71 | @abstract Returns layer translation factor for the y axis. 72 | */ 73 | extern CGFloat POPLayerGetTranslationY(CALayer *l); 74 | 75 | /** 76 | @abstract Set layer translation factor for the y axis. 77 | */ 78 | extern void POPLayerSetTranslationY(CALayer *l, CGFloat f); 79 | 80 | /** 81 | @abstract Returns layer translation factor for the z axis. 82 | */ 83 | extern CGFloat POPLayerGetTranslationZ(CALayer *l); 84 | 85 | /** 86 | @abstract Set layer translation factor for the z axis. 87 | */ 88 | extern void POPLayerSetTranslationZ(CALayer *l, CGFloat f); 89 | 90 | /** 91 | @abstract Returns layer translation factors for x and y access as point. 92 | */ 93 | extern CGPoint POPLayerGetTranslationXY(CALayer *l); 94 | 95 | /** 96 | @abstract Sets layer x and y translation factors given point. 97 | */ 98 | extern void POPLayerSetTranslationXY(CALayer *l, CGPoint p); 99 | 100 | #pragma mark - Rotation 101 | 102 | /** 103 | @abstract Returns layer rotation, in radians, in the X axis. 104 | */ 105 | extern CGFloat POPLayerGetRotationX(CALayer *l); 106 | 107 | /** 108 | @abstract Sets layer rotation, in radians, in the X axis. 109 | */ 110 | extern void POPLayerSetRotationX(CALayer *l, CGFloat f); 111 | 112 | /** 113 | @abstract Returns layer rotation, in radians, in the Y axis. 114 | */ 115 | extern CGFloat POPLayerGetRotationY(CALayer *l); 116 | 117 | /** 118 | @abstract Sets layer rotation, in radians, in the Y axis. 119 | */ 120 | extern void POPLayerSetRotationY(CALayer *l, CGFloat f); 121 | 122 | /** 123 | @abstract Returns layer rotation, in radians, in the Z axis. 124 | */ 125 | extern CGFloat POPLayerGetRotationZ(CALayer *l); 126 | 127 | /** 128 | @abstract Sets layer rotation, in radians, in the Z axis. 129 | */ 130 | extern void POPLayerSetRotationZ(CALayer *l, CGFloat f); 131 | 132 | /** 133 | @abstract Returns layer rotation, in radians, in the Z axis. 134 | */ 135 | extern CGFloat POPLayerGetRotation(CALayer *l); 136 | 137 | /** 138 | @abstract Sets layer rotation, in radians, in the Z axis. 139 | */ 140 | extern void POPLayerSetRotation(CALayer *l, CGFloat f); 141 | 142 | #pragma mark - Sublayer Scale 143 | 144 | /** 145 | @abstract Returns sublayer scale factors for x and y access as point. 146 | */ 147 | extern CGPoint POPLayerGetSubScaleXY(CALayer *l); 148 | 149 | /** 150 | @abstract Sets sublayer x and y scale factors given point. 151 | */ 152 | extern void POPLayerSetSubScaleXY(CALayer *l, CGPoint p); 153 | 154 | #pragma mark - Sublayer Translation 155 | 156 | /** 157 | @abstract Returns sublayer translation factor for the x axis. 158 | */ 159 | extern CGFloat POPLayerGetSubTranslationX(CALayer *l); 160 | 161 | /** 162 | @abstract Set sublayer translation factor for the x axis. 163 | */ 164 | extern void POPLayerSetSubTranslationX(CALayer *l, CGFloat f); 165 | 166 | /** 167 | @abstract Returns sublayer translation factor for the y axis. 168 | */ 169 | extern CGFloat POPLayerGetSubTranslationY(CALayer *l); 170 | 171 | /** 172 | @abstract Set sublayer translation factor for the y axis. 173 | */ 174 | extern void POPLayerSetSubTranslationY(CALayer *l, CGFloat f); 175 | 176 | /** 177 | @abstract Returns sublayer translation factor for the z axis. 178 | */ 179 | extern CGFloat POPLayerGetSubTranslationZ(CALayer *l); 180 | 181 | /** 182 | @abstract Set sublayer translation factor for the z axis. 183 | */ 184 | extern void POPLayerSetSubTranslationZ(CALayer *l, CGFloat f); 185 | 186 | /** 187 | @abstract Returns sublayer translation factors for x and y access as point. 188 | */ 189 | extern CGPoint POPLayerGetSubTranslationXY(CALayer *l); 190 | 191 | /** 192 | @abstract Sets sublayer x and y translation factors given point. 193 | */ 194 | extern void POPLayerSetSubTranslationXY(CALayer *l, CGPoint p); 195 | 196 | POP_EXTERN_C_END 197 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimation.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPAnimationInternal.h" 11 | #import "POPAnimationTracerInternal.h" 12 | 13 | #import 14 | #include 15 | 16 | #import "POPAnimationExtras.h" 17 | #import "POPAnimationRuntime.h" 18 | #import "POPAnimatorPrivate.h" 19 | #import "POPAction.h" 20 | 21 | using namespace POP; 22 | 23 | #pragma mark - POPAnimation 24 | 25 | @implementation POPAnimation 26 | 27 | #pragma mark - Lifecycle 28 | 29 | - (id)init 30 | { 31 | [NSException raise:NSStringFromClass([self class]) format:@"Attempting to instantiate an abstract class. Use a concrete subclass instead."]; 32 | return nil; 33 | } 34 | 35 | - (id)_init 36 | { 37 | self = [super init]; 38 | if (nil != self) { 39 | [self _initState]; 40 | } 41 | return self; 42 | } 43 | 44 | - (void)_initState 45 | { 46 | _state = new POPAnimationState(self); 47 | } 48 | 49 | - (void)dealloc 50 | { 51 | if (_state) { 52 | delete _state; 53 | _state = NULL; 54 | }; 55 | } 56 | 57 | #pragma mark - Properties 58 | 59 | - (id)delegate 60 | { 61 | return _state->delegate; 62 | } 63 | 64 | - (void)setDelegate:(id)delegate 65 | { 66 | _state->setDelegate(delegate); 67 | } 68 | 69 | - (BOOL)isPaused 70 | { 71 | return _state->paused; 72 | } 73 | 74 | - (void)setPaused:(BOOL)paused 75 | { 76 | _state->setPaused(paused ? true : false); 77 | } 78 | 79 | FB_PROPERTY_GET(POPAnimationState, type, POPAnimationType); 80 | DEFINE_RW_PROPERTY_OBJ_COPY(POPAnimationState, completionBlock, setCompletionBlock:, POPAnimationCompletionBlock); 81 | DEFINE_RW_PROPERTY_OBJ_COPY(POPAnimationState, name, setName:, NSString*); 82 | DEFINE_RW_PROPERTY(POPAnimationState, beginTime, setBeginTime:, CFTimeInterval); 83 | DEFINE_RW_FLAG(POPAnimationState, removedOnCompletion, removedOnCompletion, setRemovedOnCompletion:); 84 | 85 | - (id)valueForUndefinedKey:(NSString *)key 86 | { 87 | return _state->dict[key]; 88 | } 89 | 90 | - (void)setValue:(id)value forUndefinedKey:(NSString *)key 91 | { 92 | if (!value) { 93 | [_state->dict removeObjectForKey:key]; 94 | } else { 95 | if (!_state->dict) 96 | _state->dict = [[NSMutableDictionary alloc] init]; 97 | _state->dict[key] = value; 98 | } 99 | } 100 | 101 | - (POPAnimationTracer *)tracer 102 | { 103 | POPAnimationState *s = POPAnimationGetState(self); 104 | if (!s->tracer) { 105 | s->tracer = [[POPAnimationTracer alloc] initWithAnimation:self]; 106 | } 107 | return s->tracer; 108 | } 109 | 110 | - (NSString *)description 111 | { 112 | NSMutableString *s = [NSMutableString stringWithFormat:@"<%@:%p", NSStringFromClass([self class]), self]; 113 | [self _appendDescription:s debug:NO]; 114 | [s appendString:@">"]; 115 | return s; 116 | } 117 | 118 | - (NSString *)debugDescription 119 | { 120 | NSMutableString *s = [NSMutableString stringWithFormat:@"<%@:%p", NSStringFromClass([self class]), self]; 121 | [self _appendDescription:s debug:YES]; 122 | [s appendString:@">"]; 123 | return s; 124 | } 125 | 126 | #pragma mark - Utility 127 | 128 | POPAnimationState *POPAnimationGetState(POPAnimation *a) 129 | { 130 | return a->_state; 131 | } 132 | 133 | - (BOOL)_advance:(id)object currentTime:(CFTimeInterval)currentTime elapsedTime:(CFTimeInterval)elapsedTime 134 | { 135 | return YES; 136 | } 137 | 138 | - (void)_appendDescription:(NSMutableString *)s debug:(BOOL)debug 139 | { 140 | if (_state->name) 141 | [s appendFormat:@"; name = %@", _state->name]; 142 | 143 | if (!self.removedOnCompletion) 144 | [s appendFormat:@"; removedOnCompletion = %@", POPStringFromBOOL(self.removedOnCompletion)]; 145 | 146 | if (debug) { 147 | if (_state->active) 148 | [s appendFormat:@"; active = %@", POPStringFromBOOL(_state->active)]; 149 | 150 | if (_state->paused) 151 | [s appendFormat:@"; paused = %@", POPStringFromBOOL(_state->paused)]; 152 | } 153 | 154 | if (_state->beginTime) { 155 | [s appendFormat:@"; beginTime = %f", _state->beginTime]; 156 | } 157 | 158 | for (NSString *key in _state->dict) { 159 | [s appendFormat:@"; %@ = %@", key, _state->dict[key]]; 160 | } 161 | } 162 | 163 | @end 164 | 165 | 166 | #pragma mark - POPPropertyAnimation 167 | 168 | #pragma mark - POPBasicAnimation 169 | 170 | #pragma mark - POPDecayAnimation 171 | 172 | @implementation NSObject (POP) 173 | 174 | - (void)pop_addAnimation:(POPAnimation *)anim forKey:(NSString *)key 175 | { 176 | [[POPAnimator sharedAnimator] addAnimation:anim forObject:self key:key]; 177 | } 178 | 179 | - (void)pop_removeAllAnimations 180 | { 181 | [[POPAnimator sharedAnimator] removeAllAnimationsForObject:self]; 182 | } 183 | 184 | - (void)pop_removeAnimationForKey:(NSString *)key 185 | { 186 | [[POPAnimator sharedAnimator] removeAnimationForObject:self key:key]; 187 | } 188 | 189 | - (NSArray *)pop_animationKeys 190 | { 191 | return [[POPAnimator sharedAnimator] animationKeysForObject:self]; 192 | } 193 | 194 | - (id)pop_animationForKey:(NSString *)key 195 | { 196 | return [[POPAnimator sharedAnimator] animationForObject:self key:key]; 197 | } 198 | 199 | @end 200 | 201 | @implementation NSProxy (POP) 202 | 203 | - (void)pop_addAnimation:(POPAnimation *)anim forKey:(NSString *)key 204 | { 205 | [[POPAnimator sharedAnimator] addAnimation:anim forObject:self key:key]; 206 | } 207 | 208 | - (void)pop_removeAllAnimations 209 | { 210 | [[POPAnimator sharedAnimator] removeAllAnimationsForObject:self]; 211 | } 212 | 213 | - (void)pop_removeAnimationForKey:(NSString *)key 214 | { 215 | [[POPAnimator sharedAnimator] removeAnimationForObject:self key:key]; 216 | } 217 | 218 | - (NSArray *)pop_animationKeys 219 | { 220 | return [[POPAnimator sharedAnimator] animationKeysForObject:self]; 221 | } 222 | 223 | - (id)pop_animationForKey:(NSString *)key 224 | { 225 | return [[POPAnimator sharedAnimator] animationForObject:self key:key]; 226 | } 227 | 228 | @end 229 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPLayerExtras.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPLayerExtras.h" 11 | 12 | #include "TransformationMatrix.h" 13 | 14 | using namespace WebCore; 15 | 16 | #define DECOMPOSE_TRANSFORM(L) \ 17 | TransformationMatrix _m(L.transform); \ 18 | TransformationMatrix::DecomposedType _d; \ 19 | _m.decompose(_d); 20 | 21 | #define RECOMPOSE_TRANSFORM(L) \ 22 | _m.recompose(_d); \ 23 | L.transform = _m.transform3d(); 24 | 25 | #define RECOMPOSE_ROT_TRANSFORM(L) \ 26 | _m.recompose(_d, true); \ 27 | L.transform = _m.transform3d(); 28 | 29 | #define DECOMPOSE_SUBLAYER_TRANSFORM(L) \ 30 | TransformationMatrix _m(L.sublayerTransform); \ 31 | TransformationMatrix::DecomposedType _d; \ 32 | _m.decompose(_d); 33 | 34 | #define RECOMPOSE_SUBLAYER_TRANSFORM(L) \ 35 | _m.recompose(_d); \ 36 | L.sublayerTransform = _m.transform3d(); 37 | 38 | #pragma mark - Scale 39 | 40 | CGFloat POPLayerGetScaleX(CALayer *l) 41 | { 42 | DECOMPOSE_TRANSFORM(l); 43 | return _d.scaleX; 44 | } 45 | 46 | void POPLayerSetScaleX(CALayer *l, CGFloat f) 47 | { 48 | DECOMPOSE_TRANSFORM(l); 49 | _d.scaleX = f; 50 | RECOMPOSE_TRANSFORM(l); 51 | } 52 | 53 | CGFloat POPLayerGetScaleY(CALayer *l) 54 | { 55 | DECOMPOSE_TRANSFORM(l); 56 | return _d.scaleY; 57 | } 58 | 59 | void POPLayerSetScaleY(CALayer *l, CGFloat f) 60 | { 61 | DECOMPOSE_TRANSFORM(l); 62 | _d.scaleY = f; 63 | RECOMPOSE_TRANSFORM(l); 64 | } 65 | 66 | CGFloat POPLayerGetScaleZ(CALayer *l) 67 | { 68 | DECOMPOSE_TRANSFORM(l); 69 | return _d.scaleZ; 70 | } 71 | 72 | void POPLayerSetScaleZ(CALayer *l, CGFloat f) 73 | { 74 | DECOMPOSE_TRANSFORM(l); 75 | _d.scaleZ = f; 76 | RECOMPOSE_TRANSFORM(l); 77 | } 78 | 79 | CGPoint POPLayerGetScaleXY(CALayer *l) 80 | { 81 | DECOMPOSE_TRANSFORM(l); 82 | return CGPointMake(_d.scaleX, _d.scaleY); 83 | } 84 | 85 | void POPLayerSetScaleXY(CALayer *l, CGPoint p) 86 | { 87 | DECOMPOSE_TRANSFORM(l); 88 | _d.scaleX = p.x; 89 | _d.scaleY = p.y; 90 | RECOMPOSE_TRANSFORM(l); 91 | } 92 | 93 | #pragma mark - Translation 94 | 95 | CGFloat POPLayerGetTranslationX(CALayer *l) 96 | { 97 | DECOMPOSE_TRANSFORM(l); 98 | return _d.translateX; 99 | } 100 | 101 | void POPLayerSetTranslationX(CALayer *l, CGFloat f) 102 | { 103 | DECOMPOSE_TRANSFORM(l); 104 | _d.translateX = f; 105 | RECOMPOSE_TRANSFORM(l); 106 | } 107 | 108 | CGFloat POPLayerGetTranslationY(CALayer *l) 109 | { 110 | DECOMPOSE_TRANSFORM(l); 111 | return _d.translateY; 112 | } 113 | 114 | void POPLayerSetTranslationY(CALayer *l, CGFloat f) 115 | { 116 | DECOMPOSE_TRANSFORM(l); 117 | _d.translateY = f; 118 | RECOMPOSE_TRANSFORM(l); 119 | } 120 | 121 | CGFloat POPLayerGetTranslationZ(CALayer *l) 122 | { 123 | DECOMPOSE_TRANSFORM(l); 124 | return _d.translateZ; 125 | } 126 | 127 | void POPLayerSetTranslationZ(CALayer *l, CGFloat f) 128 | { 129 | DECOMPOSE_TRANSFORM(l); 130 | _d.translateZ = f; 131 | RECOMPOSE_TRANSFORM(l); 132 | } 133 | 134 | CGPoint POPLayerGetTranslationXY(CALayer *l) 135 | { 136 | DECOMPOSE_TRANSFORM(l); 137 | return CGPointMake(_d.translateX, _d.translateY); 138 | } 139 | 140 | void POPLayerSetTranslationXY(CALayer *l, CGPoint p) 141 | { 142 | DECOMPOSE_TRANSFORM(l); 143 | _d.translateX = p.x; 144 | _d.translateY = p.y; 145 | RECOMPOSE_TRANSFORM(l); 146 | } 147 | 148 | #pragma mark - Rotation 149 | 150 | CGFloat POPLayerGetRotationX(CALayer *l) 151 | { 152 | DECOMPOSE_TRANSFORM(l); 153 | return _d.rotateX; 154 | } 155 | 156 | void POPLayerSetRotationX(CALayer *l, CGFloat f) 157 | { 158 | DECOMPOSE_TRANSFORM(l); 159 | _d.rotateX = f; 160 | RECOMPOSE_ROT_TRANSFORM(l); 161 | } 162 | 163 | CGFloat POPLayerGetRotationY(CALayer *l) 164 | { 165 | DECOMPOSE_TRANSFORM(l); 166 | return _d.rotateY; 167 | } 168 | 169 | void POPLayerSetRotationY(CALayer *l, CGFloat f) 170 | { 171 | DECOMPOSE_TRANSFORM(l); 172 | _d.rotateY = f; 173 | RECOMPOSE_ROT_TRANSFORM(l); 174 | } 175 | 176 | CGFloat POPLayerGetRotationZ(CALayer *l) 177 | { 178 | DECOMPOSE_TRANSFORM(l); 179 | return _d.rotateZ; 180 | } 181 | 182 | void POPLayerSetRotationZ(CALayer *l, CGFloat f) 183 | { 184 | DECOMPOSE_TRANSFORM(l); 185 | _d.rotateZ = f; 186 | RECOMPOSE_ROT_TRANSFORM(l); 187 | } 188 | 189 | CGFloat POPLayerGetRotation(CALayer *l) 190 | { 191 | return POPLayerGetRotationZ(l); 192 | } 193 | 194 | void POPLayerSetRotation(CALayer *l, CGFloat f) 195 | { 196 | POPLayerSetRotationZ(l, f); 197 | } 198 | 199 | #pragma mark - Sublayer Scale 200 | 201 | CGPoint POPLayerGetSubScaleXY(CALayer *l) 202 | { 203 | DECOMPOSE_SUBLAYER_TRANSFORM(l); 204 | return CGPointMake(_d.scaleX, _d.scaleY); 205 | } 206 | 207 | void POPLayerSetSubScaleXY(CALayer *l, CGPoint p) 208 | { 209 | DECOMPOSE_SUBLAYER_TRANSFORM(l); 210 | _d.scaleX = p.x; 211 | _d.scaleY = p.y; 212 | RECOMPOSE_SUBLAYER_TRANSFORM(l); 213 | } 214 | 215 | #pragma mark - Sublayer Translation 216 | 217 | extern CGFloat POPLayerGetSubTranslationX(CALayer *l) 218 | { 219 | DECOMPOSE_SUBLAYER_TRANSFORM(l); 220 | return _d.translateX; 221 | } 222 | 223 | extern void POPLayerSetSubTranslationX(CALayer *l, CGFloat f) 224 | { 225 | DECOMPOSE_SUBLAYER_TRANSFORM(l); 226 | _d.translateX = f; 227 | RECOMPOSE_SUBLAYER_TRANSFORM(l); 228 | } 229 | 230 | extern CGFloat POPLayerGetSubTranslationY(CALayer *l) 231 | { 232 | DECOMPOSE_SUBLAYER_TRANSFORM(l); 233 | return _d.translateY; 234 | } 235 | 236 | extern void POPLayerSetSubTranslationY(CALayer *l, CGFloat f) 237 | { 238 | DECOMPOSE_SUBLAYER_TRANSFORM(l); 239 | _d.translateY = f; 240 | RECOMPOSE_SUBLAYER_TRANSFORM(l); 241 | } 242 | 243 | extern CGFloat POPLayerGetSubTranslationZ(CALayer *l) 244 | { 245 | DECOMPOSE_SUBLAYER_TRANSFORM(l); 246 | return _d.translateZ; 247 | } 248 | 249 | extern void POPLayerSetSubTranslationZ(CALayer *l, CGFloat f) 250 | { 251 | DECOMPOSE_SUBLAYER_TRANSFORM(l); 252 | _d.translateZ = f; 253 | RECOMPOSE_SUBLAYER_TRANSFORM(l); 254 | } 255 | 256 | extern CGPoint POPLayerGetSubTranslationXY(CALayer *l) 257 | { 258 | DECOMPOSE_SUBLAYER_TRANSFORM(l); 259 | return CGPointMake(_d.translateX, _d.translateY); 260 | } 261 | 262 | extern void POPLayerSetSubTranslationXY(CALayer *l, CGPoint p) 263 | { 264 | DECOMPOSE_SUBLAYER_TRANSFORM(l); 265 | _d.translateX = p.x; 266 | _d.translateY = p.y; 267 | RECOMPOSE_SUBLAYER_TRANSFORM(l); 268 | } 269 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPVector.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPVector.h" 11 | #import "POPAnimationRuntime.h" 12 | #import "POPCGUtils.h" 13 | 14 | namespace POP 15 | { 16 | 17 | Vector::Vector(const size_t count) 18 | { 19 | _count = count; 20 | _values = 0 != count ? (CGFloat *)calloc(count, sizeof(CGFloat)) : NULL; 21 | } 22 | 23 | Vector::Vector(const Vector& other) 24 | { 25 | _count = other.size(); 26 | _values = 0 != _count ? (CGFloat *)calloc(_count, sizeof(CGFloat)) : NULL; 27 | if (0 != _count) { 28 | memcpy(_values, other.data(), _count * sizeof(CGFloat)); 29 | } 30 | } 31 | 32 | Vector::~Vector() 33 | { 34 | if (NULL != _values) { 35 | free(_values); 36 | _values = NULL; 37 | } 38 | _count = 0; 39 | } 40 | 41 | void Vector::swap(Vector &first, Vector &second) 42 | { 43 | using std::swap; 44 | swap(first._count, second._count); 45 | swap(first._values, second._values); 46 | } 47 | 48 | Vector& Vector::operator=(const Vector& other) 49 | { 50 | Vector temp(other); 51 | swap(*this, temp); 52 | return *this; 53 | } 54 | 55 | bool Vector::operator==(const Vector &other) const { 56 | if (_count != other.size()) { 57 | return false; 58 | } 59 | 60 | const CGFloat * const values = other.data(); 61 | 62 | for (NSUInteger idx = 0; idx < _count; idx++) { 63 | if (_values[idx] != values[idx]) { 64 | return false; 65 | } 66 | } 67 | 68 | return true; 69 | } 70 | 71 | bool Vector::operator!=(const Vector &other) const { 72 | if (_count == other.size()) { 73 | return false; 74 | } 75 | 76 | const CGFloat * const values = other.data(); 77 | 78 | for (NSUInteger idx = 0; idx < _count; idx++) { 79 | if (_values[idx] != values[idx]) { 80 | return false; 81 | } 82 | } 83 | 84 | return true; 85 | } 86 | 87 | Vector *Vector::new_vector(NSUInteger count, const CGFloat *values) 88 | { 89 | if (0 == count) { 90 | return NULL; 91 | } 92 | 93 | Vector *v = new Vector(count); 94 | if (NULL != values) { 95 | memcpy(v->_values, values, count * sizeof(CGFloat)); 96 | } 97 | return v; 98 | } 99 | 100 | Vector *Vector::new_vector(const Vector * const other) 101 | { 102 | if (NULL == other) { 103 | return NULL; 104 | } 105 | 106 | return Vector::new_vector(other->size(), other->data()); 107 | } 108 | 109 | Vector *Vector::new_vector(NSUInteger count, Vector4r vec) 110 | { 111 | if (0 == count) { 112 | return NULL; 113 | } 114 | 115 | Vector *v = new Vector(count); 116 | 117 | NSCAssert(count <= 4, @"unexpected count %lu", (unsigned long)count); 118 | for (NSUInteger i = 0; i < MIN(count, 4); i++) { 119 | v->_values[i] = vec[i]; 120 | } 121 | 122 | return v; 123 | } 124 | 125 | Vector4r Vector::vector4r() const 126 | { 127 | Vector4r v = Vector4r::Zero(); 128 | for (int i = 0; i < _count; i++) { 129 | v(i) = _values[i]; 130 | } 131 | return v; 132 | } 133 | 134 | Vector2r Vector::vector2r() const 135 | { 136 | Vector2r v = Vector2r::Zero(); 137 | if (_count > 0) v(0) = _values[0]; 138 | if (_count > 1) v(1) = _values[1]; 139 | return v; 140 | } 141 | 142 | Vector *Vector::new_cg_float(CGFloat f) 143 | { 144 | Vector *v = new Vector(1); 145 | v->_values[0] = f; 146 | return v; 147 | } 148 | 149 | CGPoint Vector::cg_point () const 150 | { 151 | Vector2r v = vector2r(); 152 | return CGPointMake(v(0), v(1)); 153 | } 154 | 155 | Vector *Vector::new_cg_point(const CGPoint &p) 156 | { 157 | Vector *v = new Vector(2); 158 | v->_values[0] = p.x; 159 | v->_values[1] = p.y; 160 | return v; 161 | } 162 | 163 | CGSize Vector::cg_size () const 164 | { 165 | Vector2r v = vector2r(); 166 | return CGSizeMake(v(0), v(1)); 167 | } 168 | 169 | Vector *Vector::new_cg_size(const CGSize &s) 170 | { 171 | Vector *v = new Vector(2); 172 | v->_values[0] = s.width; 173 | v->_values[1] = s.height; 174 | return v; 175 | } 176 | 177 | CGRect Vector::cg_rect() const 178 | { 179 | return _count < 4 ? CGRectZero : CGRectMake(_values[0], _values[1], _values[2], _values[3]); 180 | } 181 | 182 | Vector *Vector::new_cg_rect(const CGRect &r) 183 | { 184 | Vector *v = new Vector(4); 185 | v->_values[0] = r.origin.x; 186 | v->_values[1] = r.origin.y; 187 | v->_values[2] = r.size.width; 188 | v->_values[3] = r.size.height; 189 | return v; 190 | } 191 | 192 | CGAffineTransform Vector::cg_affine_transform() const 193 | { 194 | if (_count < 6) { 195 | return CGAffineTransformIdentity; 196 | } 197 | 198 | NSCAssert(size() >= 6, @"unexpected vector size:%lu", (unsigned long)size()); 199 | CGAffineTransform t; 200 | t.a = _values[0]; 201 | t.b = _values[1]; 202 | t.c = _values[2]; 203 | t.d = _values[3]; 204 | t.tx = _values[4]; 205 | t.ty = _values[5]; 206 | return t; 207 | } 208 | 209 | Vector *Vector::new_cg_affine_transform(const CGAffineTransform &t) 210 | { 211 | Vector *v = new Vector(4); 212 | v->_values[0] = t.a; 213 | v->_values[1] = t.b; 214 | v->_values[2] = t.c; 215 | v->_values[3] = t.d; 216 | v->_values[4] = t.tx; 217 | v->_values[5] = t.ty; 218 | return v; 219 | } 220 | 221 | CGColorRef Vector::cg_color() const 222 | { 223 | if (_count < 4) { 224 | return NULL; 225 | } 226 | return POPCGColorRGBACreate(_values); 227 | } 228 | 229 | Vector *Vector::new_cg_color(CGColorRef color) 230 | { 231 | CGFloat rgba[4]; 232 | POPCGColorGetRGBAComponents(color, rgba); 233 | return new_vector(4, rgba); 234 | } 235 | 236 | void Vector::subRound(CGFloat sub) 237 | { 238 | for (NSUInteger idx = 0; idx < _count; idx++) { 239 | _values[idx] = POPSubRound(_values[idx], sub); 240 | } 241 | } 242 | 243 | CGFloat Vector::norm() const 244 | { 245 | return sqrtr(squaredNorm()); 246 | } 247 | 248 | CGFloat Vector::squaredNorm() const 249 | { 250 | CGFloat d = 0; 251 | for (NSUInteger idx = 0; idx < _count; idx++) { 252 | d += (_values[idx] * _values[idx]); 253 | } 254 | return d; 255 | } 256 | 257 | NSString * const Vector::toString() const 258 | { 259 | if (0 == _count) 260 | return @"()"; 261 | 262 | if (1 == _count) 263 | return [NSString stringWithFormat:@"%f", _values[0]]; 264 | 265 | if (2 == _count) 266 | return [NSString stringWithFormat:@"(%.3f, %.3f)", _values[0], _values[1]]; 267 | 268 | NSMutableString *s = [NSMutableString stringWithCapacity:10]; 269 | 270 | for (NSUInteger idx = 0; idx < _count; idx++) { 271 | if (0 == idx) { 272 | [s appendFormat:@"[%.3f", _values[idx]]; 273 | } else if (idx == _count - 1) { 274 | [s appendFormat:@", %.3f]", _values[idx]]; 275 | } else { 276 | [s appendFormat:@", %.3f", _values[idx]]; 277 | } 278 | } 279 | 280 | return s; 281 | 282 | } 283 | } 284 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPAnimationRuntime.mm: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPAnimationRuntime.h" 11 | 12 | #import 13 | 14 | #import 15 | 16 | #if TARGET_OS_IPHONE 17 | #import 18 | #else 19 | #import 20 | #endif 21 | 22 | #import "POPVector.h" 23 | #import "POPAnimationRuntime.h" 24 | #import "POPGeometry.h" 25 | 26 | static Boolean pointerEqual(const void *ptr1, const void *ptr2) { 27 | return ptr1 == ptr2; 28 | } 29 | 30 | static CFHashCode pointerHash(const void *ptr) { 31 | return (CFHashCode)(ptr); 32 | } 33 | 34 | CFMutableDictionaryRef POPDictionaryCreateMutableWeakPointerToWeakPointer(NSUInteger capacity) 35 | { 36 | CFDictionaryKeyCallBacks kcb = kCFTypeDictionaryKeyCallBacks; 37 | 38 | // weak, pointer keys 39 | kcb.retain = NULL; 40 | kcb.retain = NULL; 41 | kcb.equal = pointerEqual; 42 | kcb.hash = pointerHash; 43 | 44 | CFDictionaryValueCallBacks vcb = kCFTypeDictionaryValueCallBacks; 45 | 46 | // weak, pointer values 47 | vcb.retain = NULL; 48 | vcb.release = NULL; 49 | vcb.equal = pointerEqual; 50 | 51 | return CFDictionaryCreateMutable(NULL, capacity, &kcb, &vcb); 52 | } 53 | 54 | CFMutableDictionaryRef POPDictionaryCreateMutableWeakPointerToStrongObject(NSUInteger capacity) 55 | { 56 | CFDictionaryKeyCallBacks kcb = kCFTypeDictionaryKeyCallBacks; 57 | 58 | // weak, pointer keys 59 | kcb.retain = NULL; 60 | kcb.release = NULL; 61 | kcb.equal = pointerEqual; 62 | kcb.hash = pointerHash; 63 | 64 | // strong, object values 65 | CFDictionaryValueCallBacks vcb = kCFTypeDictionaryValueCallBacks; 66 | 67 | return CFDictionaryCreateMutable(NULL, capacity, &kcb, &vcb); 68 | } 69 | 70 | static bool FBCompareTypeEncoding(const char *objctype, POPValueType type) 71 | { 72 | switch (type) 73 | { 74 | case kPOPValueFloat: 75 | return (strcmp(objctype, @encode(float)) == 0 76 | || strcmp(objctype, @encode(double)) == 0 77 | ); 78 | 79 | case kPOPValuePoint: 80 | return (strcmp(objctype, @encode(CGPoint)) == 0 81 | #if !TARGET_OS_IPHONE 82 | || strcmp(objctype, @encode(NSPoint)) == 0 83 | #endif 84 | ); 85 | 86 | case kPOPValueSize: 87 | return (strcmp(objctype, @encode(CGSize)) == 0 88 | #if !TARGET_OS_IPHONE 89 | || strcmp(objctype, @encode(NSSize)) == 0 90 | #endif 91 | ); 92 | 93 | case kPOPValueRect: 94 | return (strcmp(objctype, @encode(CGRect)) == 0 95 | #if !TARGET_OS_IPHONE 96 | || strcmp(objctype, @encode(NSRect)) == 0 97 | #endif 98 | ); 99 | 100 | case kPOPValueAffineTransform: 101 | return strcmp(objctype, @encode(CGAffineTransform)) == 0; 102 | 103 | case kPOPValueTransform: 104 | return strcmp(objctype, @encode(CATransform3D)) == 0; 105 | 106 | case kPOPValueRange: 107 | return strcmp(objctype, @encode(CFRange)) == 0 108 | || strcmp(objctype, @encode (NSRange)) == 0; 109 | 110 | case kPOPValueInteger: 111 | return (strcmp(objctype, @encode(int)) == 0 112 | || strcmp(objctype, @encode(unsigned int)) == 0 113 | || strcmp(objctype, @encode(short)) == 0 114 | || strcmp(objctype, @encode(unsigned short)) == 0 115 | || strcmp(objctype, @encode(long)) == 0 116 | || strcmp(objctype, @encode(unsigned long)) == 0 117 | || strcmp(objctype, @encode(long long)) == 0 118 | || strcmp(objctype, @encode(unsigned long long)) == 0 119 | ); 120 | default: 121 | return false; 122 | } 123 | } 124 | 125 | POPValueType POPSelectValueType(const char *objctype, const POPValueType *types, size_t length) 126 | { 127 | if (NULL != objctype) { 128 | for (size_t idx = 0; idx < length; idx++) { 129 | if (FBCompareTypeEncoding(objctype, types[idx])) 130 | return types[idx]; 131 | } 132 | } 133 | return kPOPValueUnknown; 134 | } 135 | 136 | POPValueType POPSelectValueType(id obj, const POPValueType *types, size_t length) 137 | { 138 | if ([obj isKindOfClass:[NSValue class]]) { 139 | return POPSelectValueType([obj objCType], types, length); 140 | } else if (CFGetTypeID((__bridge CFTypeRef)obj) == CGColorGetTypeID()) { 141 | return kPOPValueColor; 142 | } 143 | return kPOPValueUnknown; 144 | } 145 | 146 | const POPValueType kPOPAnimatableAllTypes[9] = {kPOPValueInteger, kPOPValueFloat, kPOPValuePoint, kPOPValueSize, kPOPValueRect, kPOPValueAffineTransform, kPOPValueTransform, kPOPValueRange, kPOPValueColor}; 147 | 148 | const POPValueType kPOPAnimatableSupportTypes[7] = {kPOPValueInteger, kPOPValueFloat, kPOPValuePoint, kPOPValueSize, kPOPValueRect, kPOPValueColor}; 149 | 150 | NSString *POPValueTypeToString(POPValueType t) 151 | { 152 | switch (t) { 153 | case kPOPValueUnknown: 154 | return @"unknown"; 155 | case kPOPValueInteger: 156 | return @"int"; 157 | case kPOPValueFloat: 158 | return @"CGFloat"; 159 | case kPOPValuePoint: 160 | return @"CGPoint"; 161 | case kPOPValueSize: 162 | return @"CGSize"; 163 | case kPOPValueRect: 164 | return @"CGRect"; 165 | case kPOPValueAffineTransform: 166 | return @"CGAffineTransform"; 167 | case kPOPValueTransform: 168 | return @"CATransform3D"; 169 | case kPOPValueRange: 170 | return @"CFRange"; 171 | case kPOPValueColor: 172 | return @"CGColorRef"; 173 | default: 174 | return nil; 175 | } 176 | } 177 | 178 | id POPBox(VectorConstRef vec, POPValueType type, bool force) 179 | { 180 | if (NULL == vec) 181 | return nil; 182 | 183 | switch (type) { 184 | case kPOPValueInteger: 185 | case kPOPValueFloat: 186 | return @(vec->data()[0]); 187 | break; 188 | case kPOPValuePoint: 189 | return [NSValue valueWithCGPoint:vec->cg_point()]; 190 | break; 191 | case kPOPValueSize: 192 | return [NSValue valueWithCGSize:vec->cg_size()]; 193 | break; 194 | case kPOPValueRect: 195 | return [NSValue valueWithCGRect:vec->cg_rect()]; 196 | break; 197 | case kPOPValueColor: { 198 | return (__bridge_transfer id)vec->cg_color(); 199 | break; 200 | } 201 | default: 202 | return force ? [NSValue valueWithCGPoint:vec->cg_point()] : nil; 203 | break; 204 | } 205 | } 206 | 207 | static VectorRef vectorize(id value, POPValueType type) 208 | { 209 | Vector *vec = NULL; 210 | 211 | switch (type) { 212 | case kPOPValueInteger: 213 | case kPOPValueFloat: 214 | vec = Vector::new_cg_float([value floatValue]); 215 | break; 216 | case kPOPValuePoint: 217 | vec = Vector::new_cg_point([value CGPointValue]); 218 | break; 219 | case kPOPValueSize: 220 | vec = Vector::new_cg_size([value CGSizeValue]); 221 | break; 222 | case kPOPValueRect: 223 | vec = Vector::new_cg_rect([value CGRectValue]); 224 | break; 225 | case kPOPValueAffineTransform: 226 | vec = Vector::new_cg_affine_transform([value CGAffineTransformValue]); 227 | break; 228 | case kPOPValueColor: 229 | vec = Vector::new_cg_color((__bridge CGColorRef)value); 230 | default: 231 | break; 232 | } 233 | 234 | return VectorRef(vec); 235 | } 236 | 237 | VectorRef POPUnbox(id value, POPValueType &animationType, NSUInteger &count, bool validate) 238 | { 239 | if (nil == value) { 240 | count = 0; 241 | return VectorRef(NULL); 242 | } 243 | 244 | // determine type of value 245 | POPValueType valueType = POPSelectValueType(value, kPOPAnimatableSupportTypes, POP_ARRAY_COUNT(kPOPAnimatableSupportTypes)); 246 | 247 | // handle unknown types 248 | if (kPOPValueUnknown == valueType) { 249 | NSString *valueDesc = kPOPValueUnknown != valueType ? POPValueTypeToString(valueType) : [[value class] description]; 250 | [NSException raise:@"Unsuported value" format:@"Animating %@ values is not supported", valueDesc]; 251 | } 252 | 253 | // vectorize 254 | VectorRef vec = vectorize(value, valueType); 255 | 256 | if (kPOPValueUnknown == animationType || 0 == count) { 257 | // update animation type based on value type 258 | animationType = valueType; 259 | if (NULL != vec) { 260 | count = vec->size(); 261 | } 262 | } else if (validate) { 263 | // allow for mismatched types, so long as vector size matches 264 | if (count != vec->size()) { 265 | [NSException raise:@"Invalid value" format:@"%@ should be of type %@", value, POPValueTypeToString(animationType)]; 266 | } 267 | } 268 | 269 | return vec; 270 | } 271 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/POPPropertyAnimationInternal.h: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2014-present, Facebook, Inc. 3 | All rights reserved. 4 | 5 | This source code is licensed under the BSD-style license found in the 6 | LICENSE file in the root directory of this source tree. An additional grant 7 | of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | 10 | #import "POPAnimationInternal.h" 11 | #import "POPPropertyAnimation.h" 12 | 13 | static void clampValue(CGFloat &value, CGFloat fromValue, CGFloat toValue, NSUInteger clamp) 14 | { 15 | BOOL increasing = (toValue > fromValue); 16 | 17 | // Clamp start of animation. 18 | if ((kPOPAnimationClampStart & clamp) && 19 | ((increasing && (value < fromValue)) || (!increasing && (value > fromValue)))) { 20 | value = fromValue; 21 | } 22 | 23 | // Clamp end of animation. 24 | if ((kPOPAnimationClampEnd & clamp) && 25 | ((increasing && (value > toValue)) || (!increasing && (value < toValue)))) { 26 | value = toValue; 27 | } 28 | } 29 | 30 | struct _POPPropertyAnimationState : _POPAnimationState 31 | { 32 | POPAnimatableProperty *property; 33 | POPValueType valueType; 34 | NSUInteger valueCount; 35 | VectorRef fromVec; 36 | VectorRef toVec; 37 | VectorRef currentVec; 38 | VectorRef previousVec; 39 | VectorRef previous2Vec; 40 | VectorRef velocityVec; 41 | VectorRef distanceVec; 42 | CGFloat roundingFactor; 43 | NSUInteger clampMode; 44 | NSArray *progressMarkers; 45 | POPProgressMarker *progressMarkerState; 46 | NSUInteger progressMarkerCount; 47 | NSUInteger nextProgressMarkerIdx; 48 | CGFloat dynamicsThreshold; 49 | 50 | _POPPropertyAnimationState(id __unsafe_unretained anim) : _POPAnimationState(anim), 51 | property(nil), 52 | valueType((POPValueType)0), 53 | valueCount(nil), 54 | fromVec(nullptr), 55 | toVec(nullptr), 56 | currentVec(nullptr), 57 | previousVec(nullptr), 58 | previous2Vec(nullptr), 59 | velocityVec(nullptr), 60 | distanceVec(nullptr), 61 | roundingFactor(0), 62 | clampMode(0), 63 | progressMarkers(nil), 64 | progressMarkerState(nil), 65 | progressMarkerCount(0), 66 | nextProgressMarkerIdx(0), 67 | dynamicsThreshold(0) 68 | { 69 | type = kPOPAnimationBasic; 70 | } 71 | 72 | ~_POPPropertyAnimationState() 73 | { 74 | if (progressMarkerState) { 75 | free(progressMarkerState); 76 | progressMarkerState = NULL; 77 | } 78 | } 79 | 80 | bool canProgress() { 81 | return hasValue(); 82 | } 83 | 84 | bool shouldRound() { 85 | return 0 != roundingFactor; 86 | } 87 | 88 | bool hasValue() { 89 | return 0 != valueCount; 90 | } 91 | 92 | bool isDone() { 93 | // inherit done 94 | if (_POPAnimationState::isDone()) { 95 | return true; 96 | } 97 | 98 | // consider an animation with no values done 99 | if (!hasValue() && !isCustom()) { 100 | return true; 101 | } 102 | 103 | return false; 104 | } 105 | 106 | // returns a copy of the currentVec, rounding if needed 107 | VectorRef currentValue() { 108 | if (!shouldRound()) { 109 | return VectorRef(Vector::new_vector(currentVec.get())); 110 | } else { 111 | VectorRef vec = VectorRef(Vector::new_vector(currentVec.get())); 112 | vec->subRound(1 / roundingFactor); 113 | return vec; 114 | } 115 | } 116 | 117 | void resetProgressMarkerState() 118 | { 119 | for (NSUInteger idx = 0; idx < progressMarkerCount; idx++) 120 | progressMarkerState[idx].reached = false; 121 | 122 | nextProgressMarkerIdx = 0; 123 | } 124 | 125 | void updatedProgressMarkers() 126 | { 127 | if (progressMarkerState) { 128 | free(progressMarkerState); 129 | progressMarkerState = NULL; 130 | } 131 | 132 | progressMarkerCount = progressMarkers.count; 133 | 134 | if (0 != progressMarkerCount) { 135 | progressMarkerState = (POPProgressMarker *)malloc(progressMarkerCount * sizeof(POPProgressMarker)); 136 | [progressMarkers enumerateObjectsUsingBlock:^(NSNumber *progressMarker, NSUInteger idx, BOOL *stop) { 137 | progressMarkerState[idx].reached = false; 138 | progressMarkerState[idx].progress = [progressMarker floatValue]; 139 | }]; 140 | } 141 | 142 | nextProgressMarkerIdx = 0; 143 | } 144 | 145 | virtual void updatedDynamicsThreshold() 146 | { 147 | dynamicsThreshold = property.threshold; 148 | } 149 | 150 | bool advanceProgress(CGFloat p) 151 | { 152 | bool advanced = progress != p; 153 | if (advanced) { 154 | progress = p; 155 | NSUInteger count = valueCount; 156 | VectorRef outVec(Vector::new_vector(count, NULL)); 157 | 158 | if (1.0 == progress) { 159 | if (outVec && toVec) { 160 | *outVec = *toVec; 161 | } 162 | } else { 163 | interpolate_vector(count, vec_data(outVec), vec_data(fromVec), vec_data(toVec), progress); 164 | } 165 | 166 | currentVec = outVec; 167 | clampCurrentValue(); 168 | delegateProgress(); 169 | } 170 | return advanced; 171 | } 172 | 173 | void computeProgress() { 174 | if (!canProgress()) { 175 | return; 176 | } 177 | 178 | static ComputeProgressFunctor func; 179 | Vector4r v = vector4(currentVec); 180 | Vector4r f = vector4(fromVec); 181 | Vector4r t = vector4(toVec); 182 | progress = func(v, f, t); 183 | } 184 | 185 | void delegateProgress() { 186 | if (!canProgress()) { 187 | return; 188 | } 189 | 190 | if (delegateDidProgress && progressMarkerState) { 191 | 192 | while (nextProgressMarkerIdx < progressMarkerCount) { 193 | if (progress < progressMarkerState[nextProgressMarkerIdx].progress) 194 | break; 195 | 196 | if (!progressMarkerState[nextProgressMarkerIdx].reached) { 197 | ActionEnabler enabler; 198 | [delegate pop_animation:self didReachProgress:progressMarkerState[nextProgressMarkerIdx].progress]; 199 | progressMarkerState[nextProgressMarkerIdx].reached = true; 200 | } 201 | 202 | nextProgressMarkerIdx++; 203 | } 204 | } 205 | 206 | if (!didReachToValue) { 207 | bool didReachToValue = false; 208 | if (0 == valueCount) { 209 | didReachToValue = true; 210 | } else { 211 | Vector4r distance = toVec->vector4r(); 212 | distance -= currentVec->vector4r(); 213 | 214 | if (0 == distance.squaredNorm()) { 215 | didReachToValue = true; 216 | } else { 217 | // components 218 | if (distanceVec) { 219 | didReachToValue = true; 220 | const CGFloat *distanceValues = distanceVec->data(); 221 | for (NSUInteger idx = 0; idx < valueCount; idx++) { 222 | didReachToValue &= signbit(distance[idx]) != signbit(distanceValues[idx]); 223 | } 224 | } 225 | } 226 | } 227 | 228 | if (didReachToValue) { 229 | handleDidReachToValue(); 230 | } 231 | } 232 | } 233 | 234 | void handleDidReachToValue() { 235 | didReachToValue = true; 236 | 237 | if (delegateDidReachToValue) { 238 | ActionEnabler enabler; 239 | [delegate pop_animationDidReachToValue:self]; 240 | } 241 | 242 | if (tracing) { 243 | [tracer didReachToValue:POPBox(currentValue(), valueType, true)]; 244 | } 245 | } 246 | 247 | void readObjectValue(VectorRef *ptrVec, id obj) 248 | { 249 | // use current object value as from value 250 | pop_animatable_read_block read = property.readBlock; 251 | if (NULL != read) { 252 | 253 | Vector4r vec = read_values(read, obj, valueCount); 254 | *ptrVec = VectorRef(Vector::new_vector(valueCount, vec)); 255 | 256 | if (tracing) { 257 | [tracer readPropertyValue:POPBox(*ptrVec, valueType, true)]; 258 | } 259 | } 260 | } 261 | 262 | virtual void willRun(bool started, id obj) { 263 | // ensure from value initialized 264 | if (NULL == fromVec) { 265 | readObjectValue(&fromVec, obj); 266 | } 267 | 268 | // ensure to value initialized 269 | if (NULL == toVec) { 270 | // compute decay to value 271 | if (kPOPAnimationDecay == type) { 272 | [self toValue]; 273 | } else { 274 | // read to value 275 | readObjectValue(&toVec, obj); 276 | } 277 | } 278 | 279 | // handle one time value initialization on start 280 | if (started) { 281 | 282 | // initialize current vec 283 | if (!currentVec) { 284 | currentVec = VectorRef(Vector::new_vector(valueCount, NULL)); 285 | 286 | // initialize current value with from value 287 | // only do this on initial creation to avoid overwriting current value 288 | // on paused animation continuation 289 | if (currentVec && fromVec) { 290 | *currentVec = *fromVec; 291 | } 292 | } 293 | 294 | // ensure velocity values 295 | if (!velocityVec) { 296 | velocityVec = VectorRef(Vector::new_vector(valueCount, NULL)); 297 | } 298 | } 299 | 300 | // ensure distance value initialized 301 | // depends on current value set on one time start 302 | if (NULL == distanceVec) { 303 | 304 | // not yet started animations may not have from value 305 | VectorRef fromVec2 = NULL != currentVec ? currentVec : fromVec; 306 | 307 | if (fromVec2 && toVec) { 308 | Vector4r distance = toVec->vector4r(); 309 | distance -= fromVec2->vector4r(); 310 | 311 | if (0 != distance.squaredNorm()) { 312 | distanceVec = VectorRef(Vector::new_vector(valueCount, distance)); 313 | } 314 | } 315 | } 316 | } 317 | 318 | virtual void reset(bool all) { 319 | _POPAnimationState::reset(all); 320 | 321 | if (all) { 322 | currentVec = NULL; 323 | previousVec = NULL; 324 | previous2Vec = NULL; 325 | } 326 | progress = 0; 327 | resetProgressMarkerState(); 328 | didReachToValue = false; 329 | distanceVec = NULL; 330 | } 331 | 332 | void clampCurrentValue(NSUInteger clamp) 333 | { 334 | if (kPOPAnimationClampNone == clamp) 335 | return; 336 | 337 | // Clamp all vector values 338 | CGFloat *currentValues = currentVec->data(); 339 | const CGFloat *fromValues = fromVec->data(); 340 | const CGFloat *toValues = toVec->data(); 341 | 342 | for (NSUInteger idx = 0; idx < valueCount; idx++) { 343 | clampValue(currentValues[idx], fromValues[idx], toValues[idx], clamp); 344 | } 345 | } 346 | 347 | void clampCurrentValue() 348 | { 349 | clampCurrentValue(clampMode); 350 | } 351 | }; 352 | 353 | typedef struct _POPPropertyAnimationState POPPropertyAnimationState; 354 | 355 | @interface POPPropertyAnimation () 356 | 357 | @end 358 | 359 | -------------------------------------------------------------------------------- /TestPop/TestPop/pop/WebCore/TransformationMatrix.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005, 2006 Apple Computer, Inc. All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 1. Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * 13 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | 26 | #ifndef TransformationMatrix_h 27 | #define TransformationMatrix_h 28 | 29 | #include //for memcpy 30 | #include 31 | #include 32 | 33 | namespace WebCore { 34 | 35 | class TransformationMatrix { 36 | public: 37 | 38 | typedef double Matrix4[4][4]; 39 | 40 | TransformationMatrix() { makeIdentity(); } 41 | TransformationMatrix(const TransformationMatrix& t) { *this = t; } 42 | TransformationMatrix(double a, double b, double c, double d, double e, double f) { setMatrix(a, b, c, d, e, f); } 43 | TransformationMatrix(double m11, double m12, double m13, double m14, 44 | double m21, double m22, double m23, double m24, 45 | double m31, double m32, double m33, double m34, 46 | double m41, double m42, double m43, double m44) 47 | { 48 | setMatrix(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44); 49 | } 50 | 51 | void setMatrix(double a, double b, double c, double d, double e, double f) 52 | { 53 | m_matrix[0][0] = a; m_matrix[0][1] = b; m_matrix[0][2] = 0; m_matrix[0][3] = 0; 54 | m_matrix[1][0] = c; m_matrix[1][1] = d; m_matrix[1][2] = 0; m_matrix[1][3] = 0; 55 | m_matrix[2][0] = 0; m_matrix[2][1] = 0; m_matrix[2][2] = 1; m_matrix[2][3] = 0; 56 | m_matrix[3][0] = e; m_matrix[3][1] = f; m_matrix[3][2] = 0; m_matrix[3][3] = 1; 57 | } 58 | 59 | void setMatrix(double m11, double m12, double m13, double m14, 60 | double m21, double m22, double m23, double m24, 61 | double m31, double m32, double m33, double m34, 62 | double m41, double m42, double m43, double m44) 63 | { 64 | m_matrix[0][0] = m11; m_matrix[0][1] = m12; m_matrix[0][2] = m13; m_matrix[0][3] = m14; 65 | m_matrix[1][0] = m21; m_matrix[1][1] = m22; m_matrix[1][2] = m23; m_matrix[1][3] = m24; 66 | m_matrix[2][0] = m31; m_matrix[2][1] = m32; m_matrix[2][2] = m33; m_matrix[2][3] = m34; 67 | m_matrix[3][0] = m41; m_matrix[3][1] = m42; m_matrix[3][2] = m43; m_matrix[3][3] = m44; 68 | } 69 | 70 | TransformationMatrix& operator =(const TransformationMatrix &t) 71 | { 72 | setMatrix(t.m_matrix); 73 | return *this; 74 | } 75 | 76 | TransformationMatrix& makeIdentity() 77 | { 78 | setMatrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); 79 | return *this; 80 | } 81 | 82 | bool isIdentity() const 83 | { 84 | return m_matrix[0][0] == 1 && m_matrix[0][1] == 0 && m_matrix[0][2] == 0 && m_matrix[0][3] == 0 && 85 | m_matrix[1][0] == 0 && m_matrix[1][1] == 1 && m_matrix[1][2] == 0 && m_matrix[1][3] == 0 && 86 | m_matrix[2][0] == 0 && m_matrix[2][1] == 0 && m_matrix[2][2] == 1 && m_matrix[2][3] == 0 && 87 | m_matrix[3][0] == 0 && m_matrix[3][1] == 0 && m_matrix[3][2] == 0 && m_matrix[3][3] == 1; 88 | } 89 | 90 | // This form preserves the double math from input to output 91 | void map(double x, double y, double& x2, double& y2) const { multVecMatrix(x, y, x2, y2); } 92 | 93 | double m11() const { return m_matrix[0][0]; } 94 | void setM11(double f) { m_matrix[0][0] = f; } 95 | double m12() const { return m_matrix[0][1]; } 96 | void setM12(double f) { m_matrix[0][1] = f; } 97 | double m13() const { return m_matrix[0][2]; } 98 | void setM13(double f) { m_matrix[0][2] = f; } 99 | double m14() const { return m_matrix[0][3]; } 100 | void setM14(double f) { m_matrix[0][3] = f; } 101 | double m21() const { return m_matrix[1][0]; } 102 | void setM21(double f) { m_matrix[1][0] = f; } 103 | double m22() const { return m_matrix[1][1]; } 104 | void setM22(double f) { m_matrix[1][1] = f; } 105 | double m23() const { return m_matrix[1][2]; } 106 | void setM23(double f) { m_matrix[1][2] = f; } 107 | double m24() const { return m_matrix[1][3]; } 108 | void setM24(double f) { m_matrix[1][3] = f; } 109 | double m31() const { return m_matrix[2][0]; } 110 | void setM31(double f) { m_matrix[2][0] = f; } 111 | double m32() const { return m_matrix[2][1]; } 112 | void setM32(double f) { m_matrix[2][1] = f; } 113 | double m33() const { return m_matrix[2][2]; } 114 | void setM33(double f) { m_matrix[2][2] = f; } 115 | double m34() const { return m_matrix[2][3]; } 116 | void setM34(double f) { m_matrix[2][3] = f; } 117 | double m41() const { return m_matrix[3][0]; } 118 | void setM41(double f) { m_matrix[3][0] = f; } 119 | double m42() const { return m_matrix[3][1]; } 120 | void setM42(double f) { m_matrix[3][1] = f; } 121 | double m43() const { return m_matrix[3][2]; } 122 | void setM43(double f) { m_matrix[3][2] = f; } 123 | double m44() const { return m_matrix[3][3]; } 124 | void setM44(double f) { m_matrix[3][3] = f; } 125 | 126 | double a() const { return m_matrix[0][0]; } 127 | void setA(double a) { m_matrix[0][0] = a; } 128 | 129 | double b() const { return m_matrix[0][1]; } 130 | void setB(double b) { m_matrix[0][1] = b; } 131 | 132 | double c() const { return m_matrix[1][0]; } 133 | void setC(double c) { m_matrix[1][0] = c; } 134 | 135 | double d() const { return m_matrix[1][1]; } 136 | void setD(double d) { m_matrix[1][1] = d; } 137 | 138 | double e() const { return m_matrix[3][0]; } 139 | void setE(double e) { m_matrix[3][0] = e; } 140 | 141 | double f() const { return m_matrix[3][1]; } 142 | void setF(double f) { m_matrix[3][1] = f; } 143 | 144 | // this = this * mat 145 | TransformationMatrix& multiply(const TransformationMatrix&); 146 | 147 | TransformationMatrix& scale(double); 148 | TransformationMatrix& scaleNonUniform(double sx, double sy); 149 | TransformationMatrix& scale3d(double sx, double sy, double sz); 150 | 151 | TransformationMatrix& rotate(double d) { return rotate3d(0, 0, d); } 152 | TransformationMatrix& rotateFromVector(double x, double y); 153 | TransformationMatrix& rotate3d(double rx, double ry, double rz); 154 | 155 | // The vector (x,y,z) is normalized if it's not already. A vector of 156 | // (0,0,0) uses a vector of (0,0,1). 157 | TransformationMatrix& rotate3d(double x, double y, double z, double angle); 158 | 159 | TransformationMatrix& translate(double tx, double ty); 160 | TransformationMatrix& translate3d(double tx, double ty, double tz); 161 | 162 | // translation added with a post-multiply 163 | TransformationMatrix& translateRight(double tx, double ty); 164 | TransformationMatrix& translateRight3d(double tx, double ty, double tz); 165 | 166 | TransformationMatrix& flipX(); 167 | TransformationMatrix& flipY(); 168 | TransformationMatrix& skew(double angleX, double angleY); 169 | TransformationMatrix& skewX(double angle) { return skew(angle, 0); } 170 | TransformationMatrix& skewY(double angle) { return skew(0, angle); } 171 | 172 | TransformationMatrix& applyPerspective(double p); 173 | bool hasPerspective() const { return m_matrix[2][3] != 0.0f; } 174 | 175 | bool isInvertible() const; 176 | 177 | // This method returns the identity matrix if it is not invertible. 178 | // Use isInvertible() before calling this if you need to know. 179 | TransformationMatrix inverse() const; 180 | 181 | // decompose the matrix into its component parts 182 | typedef struct { 183 | double scaleX, scaleY, scaleZ; 184 | double skewXY, skewXZ, skewYZ; 185 | double rotateX, rotateY, rotateZ; 186 | double quaternionX, quaternionY, quaternionZ, quaternionW; 187 | double translateX, translateY, translateZ; 188 | double perspectiveX, perspectiveY, perspectiveZ, perspectiveW; 189 | } DecomposedType; 190 | 191 | bool decompose(DecomposedType& decomp) const; 192 | void recompose(const DecomposedType& decomp, bool useEulerAngle = false); 193 | 194 | void blend(const TransformationMatrix& from, double progress); 195 | 196 | bool isAffine() const 197 | { 198 | return (m13() == 0 && m14() == 0 && m23() == 0 && m24() == 0 && 199 | m31() == 0 && m32() == 0 && m33() == 1 && m34() == 0 && m43() == 0 && m44() == 1); 200 | } 201 | 202 | // Throw away the non-affine parts of the matrix (lossy!) 203 | void makeAffine(); 204 | 205 | bool operator==(const TransformationMatrix& m2) const 206 | { 207 | return (m_matrix[0][0] == m2.m_matrix[0][0] && 208 | m_matrix[0][1] == m2.m_matrix[0][1] && 209 | m_matrix[0][2] == m2.m_matrix[0][2] && 210 | m_matrix[0][3] == m2.m_matrix[0][3] && 211 | m_matrix[1][0] == m2.m_matrix[1][0] && 212 | m_matrix[1][1] == m2.m_matrix[1][1] && 213 | m_matrix[1][2] == m2.m_matrix[1][2] && 214 | m_matrix[1][3] == m2.m_matrix[1][3] && 215 | m_matrix[2][0] == m2.m_matrix[2][0] && 216 | m_matrix[2][1] == m2.m_matrix[2][1] && 217 | m_matrix[2][2] == m2.m_matrix[2][2] && 218 | m_matrix[2][3] == m2.m_matrix[2][3] && 219 | m_matrix[3][0] == m2.m_matrix[3][0] && 220 | m_matrix[3][1] == m2.m_matrix[3][1] && 221 | m_matrix[3][2] == m2.m_matrix[3][2] && 222 | m_matrix[3][3] == m2.m_matrix[3][3]); 223 | } 224 | 225 | bool operator!=(const TransformationMatrix& other) const { return !(*this == other); } 226 | 227 | // *this = *this * t (i.e., a multRight) 228 | TransformationMatrix& operator*=(const TransformationMatrix& t) 229 | { 230 | return multiply(t); 231 | } 232 | 233 | // result = *this * t (i.e., a multRight) 234 | TransformationMatrix operator*(const TransformationMatrix& t) 235 | { 236 | TransformationMatrix result = *this; 237 | result.multiply(t); 238 | return result; 239 | } 240 | 241 | CATransform3D transform3d () const; 242 | CGAffineTransform affineTransform () const; 243 | 244 | TransformationMatrix(const CATransform3D&); 245 | operator CATransform3D() const; 246 | 247 | TransformationMatrix(const CGAffineTransform&); 248 | operator CGAffineTransform() const; 249 | 250 | private: 251 | 252 | // multiply passed 2D point by matrix (assume z=0) 253 | void multVecMatrix(double x, double y, double& dstX, double& dstY) const; 254 | 255 | // multiply passed 3D point by matrix 256 | void multVecMatrix(double x, double y, double z, double& dstX, double& dstY, double& dstZ) const; 257 | 258 | void setMatrix(const Matrix4 m) 259 | { 260 | if (m && m != m_matrix) 261 | memcpy(m_matrix, m, sizeof(Matrix4)); 262 | } 263 | 264 | bool isIdentityOrTranslation() const 265 | { 266 | return m_matrix[0][0] == 1 && m_matrix[0][1] == 0 && m_matrix[0][2] == 0 && m_matrix[0][3] == 0 && 267 | m_matrix[1][0] == 0 && m_matrix[1][1] == 1 && m_matrix[1][2] == 0 && m_matrix[1][3] == 0 && 268 | m_matrix[2][0] == 0 && m_matrix[2][1] == 0 && m_matrix[2][2] == 1 && m_matrix[2][3] == 0 && 269 | m_matrix[3][3] == 1; 270 | } 271 | 272 | Matrix4 m_matrix; 273 | }; 274 | 275 | } // namespace WebCore 276 | 277 | #endif // TransformationMatrix_h 278 | --------------------------------------------------------------------------------