├── LICENSE ├── POPProperty.swift ├── POPSwiftWrapper.h ├── POPSwiftWrapper.m └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /POPProperty.swift: -------------------------------------------------------------------------------- 1 | // NB: Not all POP-supported properties are defined 2 | 3 | import QuartzCore 4 | 5 | // CALayer 6 | let kPOPLayerBackgroundColor = "backgroundColor" 7 | let kPOPLayerBounds = "bounds" 8 | let kPOPLayerCornerRadius = "cornerRadius" 9 | let kPOPLayerBorderWidth = "borderWidth" 10 | let kPOPLayerBorderColor = "borderColor" 11 | let kPOPLayerOpacity = "opacity" 12 | let kPOPLayerPosition = "position" 13 | let kPOPLayerPositionX = "positionX" 14 | let kPOPLayerPositionY = "positionY" 15 | let kPOPLayerRotation = "rotation" 16 | let kPOPLayerRotationX = "rotationX" 17 | let kPOPLayerRotationY = "rotationY" 18 | let kPOPLayerScaleX = "scaleX" 19 | let kPOPLayerScaleXY = "scaleXY" 20 | let kPOPLayerScaleY = "scaleY" 21 | let kPOPLayerSize = "size" 22 | let kPOPLayerSubscaleXY = "subscaleXY" 23 | let kPOPLayerSubtranslationX = "subtranslationX" 24 | let kPOPLayerSubtranslationXY = "subtranslationXY" 25 | let kPOPLayerSubtranslationY = "subtranslationY" 26 | let kPOPLayerSubtranslationZ = "subtranslationZ" 27 | let kPOPLayerTranslationX = "translationX" 28 | let kPOPLayerTranslationXY = "translationXY" 29 | let kPOPLayerTranslationY = "translationY" 30 | let kPOPLayerTranslationZ = "translationZ" 31 | let kPOPLayerZPosition = "zPosition" 32 | let kPOPLayerShadowColor = "shadowColor" 33 | let kPOPLayerShadowOffset = "shadowOffset" 34 | let kPOPLayerShadowOpacity = "shadowOpacity" 35 | let kPOPLayerShadowRadius = "shadowRadius" 36 | 37 | // CAShapeLayer 38 | let kPOPShapeLayerStrokeStart = "shapeLayer.strokeStart" 39 | let kPOPShapeLayerStrokeEnd = "shapeLayer.strokeEnd" 40 | let kPOPShapeLayerStrokeColor = "shapeLayer.strokeColor" 41 | 42 | // UIScrollView 43 | let kPOPScrollViewContentOffset = "scrollView.contentOffset" 44 | let kPOPScrollViewContentSize = "scrollView.contentSize" 45 | let kPOPScrollViewZoomScale = "scrollView.zoomScale" 46 | 47 | //UILabel 48 | let kPOPLabelTextColor = "label.textColor" 49 | 50 | let kPOPThresholdColor = 0.01 51 | let kPOPThresholdPoint = 1.0 52 | let kPOPThresholdScale = 0.005 53 | //let kPOPThresholdOpacity = kPOPThresholdColor 54 | //let kPOPThresholdRotation = kPOPThresholdColor 55 | //let kPOPThresholdRadius = kPOPThresholdColor 56 | -------------------------------------------------------------------------------- /POPSwiftWrapper.h: -------------------------------------------------------------------------------- 1 | 2 | #import 3 | #import 4 | #import 5 | 6 | /// Abstract animation effect class 7 | @interface _POPAnimation : NSObject 8 | 9 | - (NSString*) description; 10 | 11 | + (void) addAnimation:(_POPAnimation*)anim key:(NSString*)key obj:(id)obj; 12 | 13 | + (void) removeAllAnimations:(id)obj; 14 | 15 | + (void) removeAnimationForKey:(NSString*)key obj:(id)obj; 16 | 17 | + (NSArray*) animationKeys:(id)obj; 18 | 19 | + (NSDictionary*) animations:(id)obj; 20 | 21 | + (_POPAnimation*) animationForKey:(NSString*)key obj:(id)obj; 22 | 23 | - (_POPAnimation*) mutableCopy; 24 | 25 | @property (copy, nonatomic) 26 | NSString *name; 27 | 28 | @property (copy, nonatomic) 29 | void (^completionBlock)(_POPAnimation* anim, BOOL finished); 30 | 31 | @property (assign, nonatomic) 32 | BOOL removedOnCompletion; 33 | 34 | @property (assign, nonatomic, getter = isPaused) 35 | BOOL paused; 36 | 37 | @property (assign, nonatomic) 38 | BOOL autoreverses; 39 | 40 | @property (assign, nonatomic) 41 | NSInteger repeatCount; 42 | 43 | @property (assign, nonatomic) 44 | BOOL repeatForever; 45 | 46 | @property (strong) 47 | POPAnimation* ref; 48 | 49 | @end 50 | 51 | @class _POPAnimatableProperty; 52 | 53 | /// Property animation effect class 54 | @interface _POPPropertyAnimation : _POPAnimation 55 | 56 | - (_POPPropertyAnimation*) mutableCopy; 57 | 58 | @property (strong, nonatomic) 59 | _POPAnimatableProperty *property; 60 | 61 | - (NSString*) description; 62 | 63 | @property (copy, nonatomic) 64 | id fromValue; 65 | 66 | @property (copy, nonatomic) 67 | id toValue; 68 | 69 | @property (readonly) 70 | CGFloat progress; 71 | 72 | @property (assign, nonatomic) 73 | CGFloat roundingFactor; 74 | 75 | @property (assign, nonatomic) 76 | NSUInteger clampMode; 77 | 78 | @property (assign, nonatomic, getter = isAdditive) 79 | BOOL additive; 80 | 81 | @property (strong) 82 | POPPropertyAnimation* ref; 83 | 84 | @end 85 | 86 | /// 87 | /// 88 | /// 89 | 90 | @interface _POPSpringAnimation : _POPPropertyAnimation 91 | 92 | - (instancetype) initWithTension:(CGFloat)tension friction:(CGFloat)friction mass:(CGFloat)mass; 93 | 94 | - (instancetype) initWithTension:(CGFloat)tension friction:(CGFloat)friction mass:(CGFloat)mass velocity:(CGFloat)velocity; 95 | 96 | - (_POPSpringAnimation*) mutableCopy; 97 | 98 | - (NSString*) description; 99 | 100 | @property (copy, nonatomic) 101 | id velocity; 102 | 103 | @property (assign, nonatomic) 104 | CGFloat springBounciness; 105 | 106 | @property (assign, nonatomic) 107 | CGFloat springSpeed; 108 | 109 | @property (assign, nonatomic) 110 | CGFloat dynamicsTension; 111 | 112 | @property (assign, nonatomic) 113 | CGFloat dynamicsFriction; 114 | 115 | @property (assign, nonatomic) 116 | CGFloat dynamicsMass; 117 | 118 | @property (strong) 119 | POPSpringAnimation* ref; 120 | 121 | @end 122 | 123 | /// 124 | /// 125 | /// 126 | 127 | @interface _POPBasicAnimation : _POPPropertyAnimation 128 | 129 | - (NSString*) description; 130 | 131 | - (instancetype) initWithDuration:(CGFloat)duration; 132 | 133 | - (instancetype) initWithDuration:(CGFloat)duration timingFunction:(CAMediaTimingFunction*)timingFunction; 134 | 135 | - (_POPBasicAnimation*) mutableCopy; 136 | 137 | @property (assign, nonatomic) 138 | CFTimeInterval duration; 139 | 140 | @property (strong, nonatomic) 141 | CAMediaTimingFunction *timingFunction; 142 | 143 | @property (strong) 144 | POPBasicAnimation* ref; 145 | 146 | @end 147 | 148 | /// 149 | /// 150 | /// 151 | 152 | @interface _POPDecayAnimation : _POPPropertyAnimation 153 | 154 | - (NSString*) description; 155 | 156 | -(instancetype)initWithVelocity:(CGFloat)velocity; 157 | 158 | -(instancetype)initWithVelocity:(CGFloat)velocity decel:(CGFloat)decel; 159 | 160 | -(_POPDecayAnimation*)mutableCopy; 161 | 162 | @property (copy, nonatomic) 163 | id velocity; 164 | 165 | @property (copy, nonatomic, readonly) 166 | id originalVelocity; 167 | 168 | @property (assign, nonatomic) 169 | CGFloat deceleration; 170 | 171 | @property (readonly, assign, nonatomic) 172 | CFTimeInterval duration; 173 | 174 | - (void)setToValue:(id)toValue NS_UNAVAILABLE; 175 | 176 | - (id)reversedVelocity; 177 | 178 | @property (strong) 179 | POPDecayAnimation* ref; 180 | 181 | @end 182 | 183 | /// 184 | /// 185 | /// 186 | 187 | @interface POPAnimator (Swift) 188 | 189 | - (void)addAnimation:(POPAnimation *)anim forObject:(id)obj key:(NSString *)key; 190 | 191 | - (void)removeAllAnimationsForObject:(id)obj; 192 | 193 | - (void)removeAnimationForObject:(id)obj key:(NSString *)key; 194 | 195 | - (NSArray *)animationKeysForObject:(id)obj; 196 | 197 | - (id)animationForObject:(id)obj key:(NSString *)key; 198 | 199 | @end 200 | 201 | /// 202 | /// 203 | /// 204 | 205 | @interface _POPMutableAnimatableProperty: NSObject 206 | 207 | @property (readwrite, nonatomic, copy) 208 | NSString *name; 209 | 210 | @property (readwrite, nonatomic, copy) 211 | void (^readBlock)(id obj, CGFloat values[]); 212 | 213 | @property (readwrite, nonatomic, copy) 214 | void (^writeBlock)(id obj, const CGFloat values[]); 215 | 216 | @property (readwrite, nonatomic, assign) 217 | CGFloat threshold; 218 | 219 | @property (strong) 220 | POPMutableAnimatableProperty* ref; 221 | 222 | @end 223 | 224 | /// 225 | /// 226 | /// 227 | 228 | @interface _POPAnimatableProperty: NSObject 229 | 230 | + (_POPAnimatableProperty*) propertyWithName:(NSString *)name; 231 | 232 | + (_POPAnimatableProperty*) propertyWithName:(NSString *)name initializer:(void (^)(_POPMutableAnimatableProperty* prop))block; 233 | 234 | @property (readonly, nonatomic, copy) 235 | NSString *name; 236 | 237 | @property (readonly, nonatomic, copy) 238 | void (^readBlock)(id obj, CGFloat values[]); 239 | 240 | @property (readonly, nonatomic, copy) 241 | void (^writeBlock)(id obj, const CGFloat values[]); 242 | 243 | @property (readonly, nonatomic, assign) 244 | CGFloat threshold; 245 | 246 | @property (strong) 247 | POPAnimatableProperty* ref; 248 | 249 | @end 250 | -------------------------------------------------------------------------------- /POPSwiftWrapper.m: -------------------------------------------------------------------------------- 1 | 2 | #import "POPSwiftWrapper.h" 3 | #import 4 | #import 5 | #import "POP/POPAnimator.h" 6 | 7 | @implementation _POPAnimation 8 | 9 | -(NSString *)description { 10 | return @""; 11 | } 12 | 13 | +(NSDictionary*)animations:(id)obj { 14 | if (obj) { 15 | NSArray* keys = [self animationKeys:obj]; 16 | NSMutableDictionary* anims = [NSMutableDictionary dictionary]; 17 | for (NSString* key in keys) { 18 | [anims setObject:[self animationForKey:key obj:obj] forKey:key]; 19 | } 20 | return anims; 21 | } 22 | 23 | return @{}; 24 | } 25 | 26 | +(void)addAnimation:(_POPAnimation*)anim key:(NSString *)key obj:(id)obj { 27 | if (!obj || !key || !anim) return; 28 | [[POPAnimator sharedAnimator] addAnimation:anim.ref forObject:obj key:key]; 29 | } 30 | 31 | +(void)removeAllAnimations:(id)obj { 32 | if (obj) [[POPAnimator sharedAnimator] removeAllAnimationsForObject:obj]; 33 | } 34 | 35 | +(void)removeAnimationForKey:(NSString *)key obj:(id)obj { 36 | if (!obj || !key) return; 37 | [[POPAnimator sharedAnimator] removeAnimationForObject:obj key:key]; 38 | } 39 | 40 | +(NSArray *)animationKeys:(id)obj { 41 | return obj ? [[POPAnimator sharedAnimator] animationKeysForObject:obj] : @[]; 42 | } 43 | 44 | +(_POPAnimation*)animationForKey:(NSString *)key obj:(id)obj { 45 | if (!obj || !key) return nil; 46 | _POPAnimation* anim = [_POPAnimation new]; 47 | anim.ref = [[POPAnimator sharedAnimator] animationForObject:obj key:key]; 48 | return anim; 49 | } 50 | 51 | // Must be overridden 52 | -(_POPAnimation *)mutableCopy { return nil; } 53 | 54 | -(NSString *)name { return self.ref.name; } 55 | -(void)setName:(NSString *)name { self.ref.name = name; } 56 | 57 | @synthesize completionBlock = _completionBlock; 58 | -(void (^)(_POPAnimation *, BOOL))completionBlock { return _completionBlock; } 59 | -(void)setCompletionBlock:(void (^)(_POPAnimation *, BOOL))completionBlock { 60 | _completionBlock = completionBlock; 61 | __weak _POPAnimation* wself = self; 62 | self.ref.completionBlock = ^(POPAnimation* anim, BOOL finished){ 63 | wself.completionBlock(wself, finished); 64 | }; 65 | } 66 | 67 | -(BOOL)removedOnCompletion { return self.ref.removedOnCompletion; } 68 | -(void)setRemovedOnCompletion:(BOOL)removedOnCompletion { self.ref.removedOnCompletion = removedOnCompletion; } 69 | 70 | -(BOOL)isPaused { return self.ref.isPaused; } 71 | -(void)setPaused:(BOOL)paused { self.ref.paused = paused; } 72 | 73 | -(BOOL)autoreverses { return self.ref.autoreverses; } 74 | -(void)setAutoreverses:(BOOL)autoreverses { self.ref.autoreverses = autoreverses; } 75 | 76 | -(NSInteger)repeatCount { return self.ref.repeatCount; } 77 | -(void)setRepeatCount:(NSInteger)repeatCount { self.ref.repeatCount = repeatCount; } 78 | 79 | -(BOOL)repeatForever { return self.ref.repeatForever; } 80 | -(void)setRepeatForever:(BOOL)repeatForever { self.ref.repeatForever = repeatForever; } 81 | 82 | @end 83 | 84 | @implementation _POPPropertyAnimation 85 | 86 | -(NSString *)description { 87 | NSString* props = [NSString stringWithFormat:@", fromValue: %@, toValue: %@, property: %@, roundingFactor: %f, clampMode: %lu, additive: %@", self.ref.fromValue, self.ref.toValue, self.ref.property.name, self.ref.roundingFactor, self.ref.clampMode, [NSNumber numberWithBool:self.ref.additive]]; 88 | return [[super description] stringByAppendingString:props]; 89 | } 90 | 91 | - (_POPPropertyAnimation*) mutableCopy { return nil; } 92 | 93 | -(CGFloat)progress { return self.ref.progress; } 94 | 95 | @synthesize property = _property; 96 | -(_POPAnimatableProperty *)property { return _property; } 97 | -(void)setProperty:(_POPAnimatableProperty *)property { 98 | _property = property; 99 | self.ref.property = property.ref; 100 | } 101 | 102 | -(id)fromValue { return self.ref.fromValue; } 103 | -(void)setFromValue:(id)fromValue { self.ref.fromValue = fromValue; } 104 | 105 | -(id)toValue { return self.ref.toValue; } 106 | -(void)setToValue:(id)toValue { self.ref.toValue = toValue; } 107 | 108 | -(CGFloat)roundingFactor { return self.ref.roundingFactor; } 109 | -(void)setRoundingFactor:(CGFloat)roundingFactor { self.ref.roundingFactor = roundingFactor; } 110 | 111 | -(NSUInteger)clampMode { return self.ref.clampMode; } 112 | -(void)setClampMode:(NSUInteger)clampMode { self.ref.clampMode = clampMode; } 113 | 114 | -(BOOL)isAdditive { return self.ref.isAdditive; } 115 | -(void)setAdditive:(BOOL)additive { self.ref.additive = additive; } 116 | 117 | @end 118 | 119 | @implementation _POPDecayAnimation 120 | 121 | -(NSString *)description { 122 | NSString* props = [NSString stringWithFormat:@", velocity: %@, deceleration: %f", self.ref.velocity, self.ref.deceleration]; 123 | return [[super description] stringByAppendingString:props]; 124 | } 125 | 126 | -(_POPDecayAnimation*)mutableCopy { 127 | return [[_POPDecayAnimation alloc] initWithVelocity:[self.ref.velocity floatValue] decel:self.ref.deceleration]; 128 | } 129 | 130 | -(instancetype)initWithVelocity:(CGFloat)velocity { 131 | return self = [[_POPDecayAnimation alloc] initWithVelocity:velocity decel:self.deceleration]; 132 | } 133 | 134 | -(instancetype)initWithVelocity:(CGFloat)velocity decel:(CGFloat)decel { 135 | self = [super init]; 136 | self.ref = [POPDecayAnimation animation]; 137 | self.ref.velocity = @(velocity); 138 | self.ref.deceleration = decel; 139 | return self; 140 | } 141 | 142 | - (id) velocity { return self.ref.velocity; } 143 | - (void) setVelocity:(id)velocity { self.ref.velocity = velocity; } 144 | 145 | - (CGFloat) deceleration { return self.ref.deceleration; } 146 | - (void) setDeceleration:(CGFloat)deceleration { self.ref.deceleration = deceleration; } 147 | 148 | - (CFTimeInterval) duration { return self.ref.duration; } 149 | 150 | - (id) reversedVelocity { return self.ref.reversedVelocity; } 151 | 152 | - (id) originalVelocity { return self.ref.originalVelocity; } 153 | 154 | @end 155 | 156 | @implementation _POPSpringAnimation 157 | 158 | -(NSString *)description { 159 | NSString* props = [NSString stringWithFormat:@", tension: %f, friction: %f, mass: %f, velocity: %@", self.ref.dynamicsTension, self.ref.dynamicsFriction, self.ref.dynamicsMass, self.ref.velocity]; 160 | return [[super description] stringByAppendingString:props]; 161 | } 162 | 163 | -(instancetype)initWithTension:(CGFloat)tension friction:(CGFloat)friction mass:(CGFloat)mass { 164 | self = [super init]; 165 | self.ref = [POPSpringAnimation animation]; 166 | self.ref.dynamicsTension = tension; 167 | self.ref.dynamicsFriction = friction; 168 | self.ref.dynamicsMass = mass; 169 | return self; 170 | } 171 | 172 | -(instancetype)initWithTension:(CGFloat)tension friction:(CGFloat)friction mass:(CGFloat)mass velocity:(CGFloat)velocity { 173 | self = [[_POPSpringAnimation alloc] initWithTension:tension friction:friction mass:mass]; 174 | self.ref.velocity = @(velocity); 175 | return self; 176 | } 177 | 178 | /// NB: Does not copy "velocity" 179 | -(_POPSpringAnimation *)mutableCopy { 180 | return [[_POPSpringAnimation alloc] initWithTension:self.ref.dynamicsTension friction:self.ref.dynamicsFriction mass:self.ref.dynamicsMass]; 181 | } 182 | 183 | -(CGFloat)springBounciness { return self.ref.springBounciness; } 184 | -(void)setSpringBounciness:(CGFloat)springBounciness { self.ref.springBounciness = springBounciness; } 185 | 186 | -(CGFloat)springSpeed { return self.ref.springSpeed; } 187 | -(void)setSpringSpeed:(CGFloat)springSpeed { self.ref.springSpeed = springSpeed; } 188 | 189 | -(CGFloat)dynamicsTension { return self.ref.dynamicsTension; } 190 | -(void)setDynamicsTension:(CGFloat)dynamicsTension { self.ref.dynamicsTension = dynamicsTension; } 191 | 192 | -(CGFloat)dynamicsFriction { return self.ref.dynamicsFriction; } 193 | -(void)setDynamicsFriction:(CGFloat)dynamicsFriction { self.ref.dynamicsFriction = dynamicsFriction; } 194 | 195 | -(CGFloat)dynamicsMass { return self.ref.dynamicsMass; } 196 | -(void)setDynamicsMass:(CGFloat)dynamicsMass { self.ref.dynamicsMass = dynamicsMass; } 197 | 198 | -(id)velocity { return self.ref.velocity; } 199 | -(void)setVelocity:(id)velocity { self.ref.velocity = velocity; } 200 | 201 | @end 202 | 203 | @implementation _POPBasicAnimation 204 | 205 | - (NSString *)description { 206 | NSString* props = [NSString stringWithFormat:@", duration: %f, timingFunction: %@", self.ref.duration, self.ref.timingFunction]; 207 | return [[super description] stringByAppendingString:props]; 208 | } 209 | 210 | - (instancetype)initWithDuration:(CGFloat)duration timingFunction:(CAMediaTimingFunction *)timingFunction { 211 | self = [super init]; 212 | self.ref = [POPBasicAnimation animation]; 213 | self.ref.duration = duration; 214 | self.ref.timingFunction = timingFunction; 215 | return self; 216 | } 217 | 218 | -(instancetype)initWithDuration:(CGFloat)duration { 219 | return [[_POPBasicAnimation alloc] initWithDuration:duration timingFunction:nil]; 220 | } 221 | 222 | -(_POPBasicAnimation *)mutableCopy { 223 | return [[_POPBasicAnimation alloc] initWithDuration:self.ref.duration timingFunction:self.ref.timingFunction]; 224 | } 225 | 226 | -(CFTimeInterval)duration { return self.ref.duration; } 227 | -(CAMediaTimingFunction *)timingFunction { return self.ref.timingFunction; } 228 | 229 | @end 230 | 231 | @implementation _POPAnimatableProperty 232 | 233 | +(_POPAnimatableProperty*)propertyWithName:(NSString *)name { 234 | _POPAnimatableProperty* prop = [_POPAnimatableProperty new]; 235 | prop.ref = [POPAnimatableProperty propertyWithName:name]; 236 | return prop; 237 | } 238 | 239 | +(_POPAnimatableProperty*)propertyWithName:(NSString *)name initializer:(void (^)(_POPMutableAnimatableProperty*))block { 240 | _POPAnimatableProperty* prop = [_POPAnimatableProperty new]; 241 | prop.ref = [POPAnimatableProperty propertyWithName:name initializer:^(POPMutableAnimatableProperty* prop){ 242 | _POPMutableAnimatableProperty* mprop = [_POPMutableAnimatableProperty new]; 243 | mprop.ref = prop; 244 | block(mprop); 245 | }]; 246 | return prop; 247 | } 248 | 249 | -(NSString *)name { return self.ref.name; } 250 | 251 | -(void (^)(id, CGFloat *))readBlock { return self.ref.readBlock; } 252 | 253 | -(void (^)(id, const CGFloat *))writeBlock { return self.ref.writeBlock; } 254 | 255 | -(CGFloat)threshold { return self.ref.threshold; } 256 | 257 | @end 258 | 259 | @implementation _POPMutableAnimatableProperty 260 | 261 | -(NSString *)name { return self.ref.name; } 262 | -(void)setName:(NSString *)name { self.ref.name = name; } 263 | 264 | -(void (^)(id, CGFloat *))readBlock { return self.ref.readBlock; } 265 | -(void)setReadBlock:(void (^)(id, CGFloat *))readBlock { self.ref.readBlock = readBlock; } 266 | 267 | -(void (^)(id, const CGFloat *))writeBlock { return self.ref.writeBlock; } 268 | -(void)setWriteBlock:(void (^)(id, const CGFloat *))writeBlock { self.ref.writeBlock = writeBlock; } 269 | 270 | -(CGFloat)threshold { return self.ref.threshold; } 271 | -(void)setThreshold:(CGFloat)threshold { self.ref.threshold = threshold; } 272 | 273 | @end 274 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##Facebook POP for Swift 2 | 3 | **What is this?** A wrapper for [Facebook POP](https://github.com/facebook/pop) written in Objective-C (whereas POP is written in Objective-C++) 4 | 5 | **Why would I need this?** [Objective-C++ can't be imported into Swift](http://stackoverflow.com/a/24042893/2228559), as of June 24 2014. 6 | 7 | -- 8 | 9 | ####Example 10 | 11 | ````Swift 12 | // Create view to animate 13 | var view = UIView(frame: UIScreen.mainScreen().bounds) 14 | view.backgroundColor = UIColor.blueColor() 15 | 16 | // Create animation 17 | var anim = _POPSpringAnimation(tension: 100, friction: 10, mass: 1) 18 | anim.property = _POPAnimatableProperty(name: kPOPLayerOpacity) 19 | anim.toValue = 0 20 | _POPAnimation.addAnimation(anim, key: anim.property.name, obj: view.layer) 21 | ```` 22 | 23 | -- 24 | 25 | ####Install 26 | 27 | Add `#import "POPSwiftWrapper.h"` to the **.h** file pointed to under *Build Settings -> Swift Compiler - Code Generation -> Objective-C Bridging Header*. 28 | 29 | -- 30 | 31 | ####Swift classes 32 | 33 | I didn't wrap *every* POP class, as you will see. Here's a list of what I wrapped: 34 | 35 | - _POPAnimation 36 | - _POPPropertyAnimation 37 | - _POPSpringAnimation 38 | - _POPBasicAnimation 39 | - _POPDecayAnimation 40 | - _POPAnimatableProperty 41 | - _POPMutableAnimatableProperty 42 | 43 | -- 44 | 45 | ####_POPAnimation class methods 46 | 47 | I've added some class methods to `_POPAnimation` that implement POP's `NSObject` category. If you want, you can make a Swift extension on `NSObject` like POP does it, but these class methods allow `AnyObject` to be the associated object, which allows the animation of Swift object properties. In order to do this, a category on `POPAnimator` is required and can be found at the bottom of **POPSwiftWrapper.h**. 48 | 49 | ````Objective-C 50 | + (void) addAnimation:(_POPAnimation*)anim key:(NSString*)key obj:(id)obj; 51 | + (void) removeAllAnimations:(id)obj; 52 | + (void) removeAnimationForKey:(NSString*)key obj:(id)obj; 53 | + (NSArray*) animationKeys:(id)obj; 54 | + (_POPAnimation*) animationForKey:(NSString*)key obj:(id)obj; 55 | ```` 56 | 57 | *...use like this...* 58 | 59 | ````Swift 60 | _POPAnimation.addAnimation(anim, key: anim.property.name, obj: view.layer) 61 | _POPAnimation.removeAllAnimations(view.layer) 62 | _POPAnimation.removeAnimationsForKey(kPOPLayerOpacity, obj: view.layer) 63 | var keys = _POPAnimation.animationKeys(view.layer) 64 | var anim = _POPAnimation(forKey: kPOPLayerOpacity, obj: view.layer) 65 | ```` 66 | 67 | -- 68 | 69 | ####Added convenience initializers 70 | 71 | I added some convenience initializers to `_POPSpringAnimation`, `_POPBasicAnimation`, and `_POPDecayAnimation`. I implemented `- (id) mutableCopy {}` in these three, as well! Enjoy, or take them out! 72 | 73 | **_POPSpringAnimation** 74 | 75 | ````Objective-C 76 | - (instancetype) initWithTension:(CGFloat)tension friction:(CGFloat)friction mass:(CGFloat)mass; 77 | - (instancetype) initWithTension:(CGFloat)tension friction:(CGFloat)friction mass:(CGFloat)mass velocity:(CGFloat)velocity; 78 | ```` 79 | 80 | *...use like this...* 81 | 82 | ````Swift 83 | var anim = _POPSpringAnimation(tension: 100, friction: 10, mass: 1) 84 | var anim = _POPSpringAnimation(tension: 100, friction: 10, mass: 1, velocity: 1000) 85 | ```` 86 | 87 | **_POPDecayAnimation** 88 | 89 | ````Objective-C 90 | -(instancetype)initWithVelocity:(CGFloat)velocity; 91 | -(instancetype)initWithVelocity:(CGFloat)velocity decel:(CGFloat)decel; 92 | ```` 93 | 94 | *...use like this...* 95 | 96 | ````Swift 97 | var anim = _POPDecayAnimation(velocity: 1000) 98 | var anim = _POPDecayAnimation(velocity: 1000, decel: 0.995) 99 | ```` 100 | 101 | **_POPBasicAnimation** 102 | 103 | ````Objective-C 104 | - (instancetype) initWithDuration:(CGFloat)duration; 105 | - (instancetype) initWithDuration:(CGFloat)duration timingFunction:(CAMediaTimingFunction*)timingFunction; 106 | ```` 107 | 108 | *...use like this...* 109 | 110 | ````Swift 111 | var anim = _POPBasicAnimation(duration: 1) 112 | var anim = _POPBasicAnimation(duration: 1, timingFunction: CAMediaTimingFunction(controlPoints: 0, 0, 1, 1)) 113 | ```` 114 | 115 | -- 116 | 117 | ####POPProperty.swift 118 | 119 | This file provides POP's property keys (eg: `kPOPLayerBounds`) as Swift constant `String`s. You can also access POP's threshold values through `kPOPThresholdColor`, `kPOPThresholdPoint`, and `kPOPThresholdScale`. 120 | 121 | Note: I didn't port all POP-supported property keys. Sorry for any inconvenience! 122 | 123 | -- 124 | 125 | ####Typealias 126 | 127 | Take advantage of Swift's `typealias` to make the wrapper names more memorable/readable. 128 | 129 | ````Swift 130 | typealias Animation = _POPAnimation 131 | typealias SpringAnimation = _POPSpringAnimation 132 | typealias DecayAnimation = _POPDecayAnimation 133 | typealias BasicAnimation = _POPBasicAnimation 134 | typealias AnimatableProperty = _POPAnimatableProperty 135 | typealias MutableAnimatableProperty = _POPMutableAnimatableProperty 136 | ```` 137 | 138 | -- 139 | 140 | Created by **Alec Larson** (@aleclarsoniv) 141 | --------------------------------------------------------------------------------