Enable content resizing, looping, completionblocks

This commit is contained in:
Brandon Withrow
2016-07-27 14:41:17 -07:00
parent ef04ffc29d
commit 07974f0058
5 changed files with 93 additions and 26 deletions

View File

@@ -13,6 +13,7 @@
- (instancetype)initWithDuration:(NSTimeInterval)duration {
self = [super init];
if (self) {
self.duration = duration;
_laAnimationDuration = duration;
}
return self;
@@ -43,6 +44,7 @@
- (void)setLoopAnimation:(BOOL)loopAnimation {
self.repeatCount = loopAnimation ? HUGE_VALF : 0;
for (CALayer *layer in self.animationSublayers) {
layer.duration = self.duration;
layer.repeatCount = loopAnimation ? HUGE_VALF : 0;
}

View File

@@ -10,15 +10,14 @@
@interface LACompView : UIView
- (instancetype)initWithModel:(LAComposition *)model;
@property (nonatomic, readonly) LAComposition *sceneModel;
@property (nonatomic, assign) BOOL debugModeOn;
+ (instancetype)animationNamed:(NSString *)animationName;
+ (instancetype)animationFromJSON:(NSDictionary *)animationJSON;
@property (nonatomic, assign) BOOL loopAnimation;
@property (nonatomic, assign) BOOL autoReverseAnimation;
@property (nonatomic, assign) CGFloat animationProgress;
- (void)playWithCompletion:(void (^)(void))completion;
- (void)play;
- (void)pause;

View File

@@ -7,19 +7,43 @@
//
#import "LACompView.h"
#import "LAModels.h"
@interface LACompView ()
@property (nonatomic, readonly) LAComposition *sceneModel;
@end
@implementation LACompView {
NSDictionary *_layerMap;
CALayer *_animationContainer;
}
+ (instancetype)animationNamed:(NSString *)animationName {
NSError *error;
NSString *filePath = [[NSBundle mainBundle] pathForResource:animationName ofType:@"json"];
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:filePath];
NSDictionary *JSONObject = [NSJSONSerialization JSONObjectWithData:jsonData
options:0 error:&error];
return [LACompView animationFromJSON:JSONObject];
}
+ (instancetype)animationFromJSON:(NSDictionary *)animationJSON {
LAComposition *laScene = [[LAComposition alloc] initWithJSON:animationJSON];
return [[LACompView alloc] initWithModel:laScene];
}
- (instancetype)initWithModel:(LAComposition *)model {
self = [super initWithFrame:model.compBounds];
if (self) {
_sceneModel = model;
_animationContainer = [CALayer new];
_animationContainer.frame = self.bounds;
[self.layer addSublayer:_animationContainer];
[self _buildSubviewsFromModel];
self.backgroundColor = [UIColor blackColor];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_viewtapped)];
[self addGestureRecognizer:tapGesture];
self.clipsToBounds = YES;
}
return self;
}
@@ -40,28 +64,29 @@
if (layer.matteType == LAMatteTypeAdd) {
maskedLayer = layerView;
}
[self.layer addSublayer:layerView];
[_animationContainer addSublayer:layerView];
}
}
_layerMap = layerMap;
}
- (void)_viewtapped {
self.debugModeOn = !self.debugModeOn;
}
- (void)setDebugModeOn:(BOOL)debugModeOn {
_debugModeOn = debugModeOn;
}
- (void)play {
- (void)playWithCompletion:(void (^)(void))completion {
[CATransaction begin];
[CATransaction setAnimationDuration:self.sceneModel.timeDuration];
if (completion) {
[CATransaction setCompletionBlock:completion];
}
for (LALayerView *layerView in _layerMap.allValues) {
[layerView play];
}
[CATransaction commit];
}
- (void)play {
[self playWithCompletion:nil];
}
- (void)pause {
for (LALayerView *layerView in _layerMap.allValues) {
[layerView pause];
@@ -89,4 +114,44 @@
}
}
- (void)layoutSubviews {
[super layoutSubviews];
CGPoint centerPoint = CGRectGetCenterPoint(self.bounds);
CATransform3D xform;
if (self.contentMode == UIViewContentModeScaleToFill) {
CGSize scaleSize = CGSizeMake(self.bounds.size.width / self.sceneModel.compBounds.size.width,
self.bounds.size.height / self.sceneModel.compBounds.size.height);
xform = CATransform3DMakeScale(scaleSize.width, scaleSize.height, 1);
} else if (self.contentMode == UIViewContentModeScaleAspectFit) {
CGFloat compAspect = self.sceneModel.compBounds.size.width / self.sceneModel.compBounds.size.height;
CGFloat viewAspect = self.bounds.size.width / self.bounds.size.height;
BOOL scaleWidth = compAspect > viewAspect;
CGFloat dominantDimension = scaleWidth ? self.bounds.size.width : self.bounds.size.height;
CGFloat compDimension = scaleWidth ? self.sceneModel.compBounds.size.width : self.sceneModel.compBounds.size.height;
CGFloat scale = dominantDimension / compDimension;
xform = CATransform3DMakeScale(scale, scale, 1);
} else if (self.contentMode == UIViewContentModeScaleAspectFill) {
CGFloat compAspect = self.sceneModel.compBounds.size.width / self.sceneModel.compBounds.size.height;
CGFloat viewAspect = self.bounds.size.width / self.bounds.size.height;
BOOL scaleWidth = compAspect < viewAspect;
CGFloat dominantDimension = scaleWidth ? self.bounds.size.width : self.bounds.size.height;
CGFloat compDimension = scaleWidth ? self.sceneModel.compBounds.size.width : self.sceneModel.compBounds.size.height;
CGFloat scale = dominantDimension / compDimension;
xform = CATransform3DMakeScale(scale, scale, 1);
} else {
xform = CATransform3DIdentity;
}
[CATransaction begin];
[CATransaction setDisableActions:YES];
_animationContainer.transform = xform;
_animationContainer.position = centerPoint;
[CATransaction commit];
}
@end

View File

@@ -42,7 +42,6 @@
@interface ViewController ()
@property (nonatomic, strong) UIButton *openButton;
@property (nonatomic, strong) LAComposition *currentScene;
@property (nonatomic, strong) LACompView *currentSceneView;
@property (nonatomic, strong) UIView *logView;
@property (nonatomic, strong) UITextView *logTextField;
@@ -59,10 +58,10 @@
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:filePath];
NSDictionary *JSONObject = [NSJSONSerialization JSONObjectWithData:jsonData
options:0 error:&error];
LAComposition *laScene = [[LAComposition alloc] initWithJSON:JSONObject];
LACompView *compView = [[LACompView alloc] initWithModel:laScene];
[self.view addSubview:compView];
[compView performSelector:@selector(play) withObject:nil afterDelay:3];
// LAComposition *laScene = [[LAComposition alloc] initWithJSON:JSONObject];
// LACompView *compView = [[LACompView alloc] initWithModel:laScene];
// [self.view addSubview:compView];
// [compView performSelector:@selector(play) withObject:nil afterDelay:3];
}
@@ -261,13 +260,13 @@
- (void)openFileURL:(NSString *)filePath {
[self.currentSceneView removeFromSuperview];
self.currentSceneView = nil;
self.currentScene = nil;
// self.currentScene = nil;
NSError *error;
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:filePath];
NSDictionary *JSONObject = [NSJSONSerialization JSONObjectWithData:jsonData
options:0 error:&error];
LAComposition *laScene = [[LAComposition alloc] initWithJSON:JSONObject];
// LAComposition *laScene = [MTLJSONAdapter modelOfClass:[LAComposition class] fromJSONDictionary:JSONObject error:&error];
@@ -287,13 +286,15 @@
// }
}
LACompView *compView = [[LACompView alloc] initWithModel:laScene];
LACompView *compView = [LACompView animationFromJSON:JSONObject];
[self.view addSubview:compView];
self.currentScene = laScene;
compView.loopAnimation = YES;
compView.contentMode = UIViewContentModeScaleAspectFit;
compView.frame = CGRectMake(0, 0, 200, 600);
// self.currentScene = laScene;
self.currentSceneView = compView;
[self.view sendSubviewToBack:self.currentSceneView];
compView.loopAnimation = YES;
[compView play];
}
@end