Skip to content

Commit

Permalink
feature: modify elements by attribute names
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerAndBull committed Apr 27, 2021
1 parent ac89183 commit 3a0418a
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 84 deletions.
4 changes: 2 additions & 2 deletions TABAnimated.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Pod::Spec.new do |s|

#tag方式:填tag名称
#commit方式:填commit的id
s.version = "2.5.3"
s.version = "2.5.4"
#库的简介
s.summary = "TABAnimated是一个ios平台上的网络过渡动画(骨架屏)的封装"

Expand All @@ -26,7 +26,7 @@ Pod::Spec.new do |s|
s.platform = :ios, "8.0"

#库的地址
s.source = { :git => "https://github.com/tigerAndBull/TABAnimated.git", :tag => "2.5.3" }
s.source = { :git => "https://github.com/tigerAndBull/TABAnimated.git", :tag => "2.5.4" }

s.source_files = 'TABAnimatedDemo/TABAnimated/**/*.{h,m}'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ - (CABasicAnimation *)scaleXAnimationDuration:(CGFloat)duration toValue:(CGFloat
animation.duration = duration;
animation.autoreverses = YES;
animation.repeatCount = HUGE_VALF;
animation.toValue = (toValue == 0.)?@0.6:@(toValue);
animation.toValue = (toValue == 0.)? @0.6 : @(toValue);
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
return animation;
}
Expand Down
10 changes: 10 additions & 0 deletions TABAnimatedDemo/TABAnimated/Decorate/Chain/TABComponentManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,19 @@ NS_ASSUME_NONNULL_BEGIN

@interface TABComponentManager : NSObject

/**
* 获取单个动画元素
* 使用方式:.animationN(x)
* x为字符串,映射组件的变量名
*
* @return TABBaseComponent对象
*/
- (TABBaseComponentStringBlock _Nullable)animationN;

/**
* 获取单个动画元素
* 使用方式:.animation(x)
* x为int值
*
* @return TABBaseComponent对象
*/
Expand Down
36 changes: 27 additions & 9 deletions TABAnimatedDemo/TABAnimated/Decorate/Chain/TABComponentManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ @interface TABComponentManager()
@property (nonatomic, strong) NSMutableArray <TABBaseComponent *> *components;
@property (nonatomic, strong) NSMutableArray <TABComponentLayer *> *layers;

@property (nonatomic, strong) NSMutableDictionary <NSString *, TABBaseComponent *> *dictComponents;

@property (nonatomic, strong) UIColor *animatedColor;

@end
Expand All @@ -36,27 +38,49 @@ - (instancetype)initWithBackgroundLayer:(TABComponentLayer *)backgroundLayer
_animatedColor = animatedColor;
_components = @[].mutableCopy;
_layers = layers.mutableCopy;
_dictComponents = @{}.mutableCopy;
_backgroundComponent = [TABBaseComponent componentWithLayer:backgroundLayer manager:self];

for (NSInteger i = 0; i < _layers.count; i++) {
TABComponentLayer *layer = _layers[i];
TABBaseComponent *component = [TABBaseComponent componentWithLayer:layer manager:self];
[_components addObject:component];
if (layer.tagName) {
[_dictComponents addEntriesFromDictionary:@{
layer.tagName : component
}];
}
}
}
return self;
}

- (TABBaseComponentStringBlock _Nullable)animationN {
return [self animationWithName];
}

- (TABBaseComponentStringBlock _Nullable)animationWithName {
__weak typeof(self) weakSelf = self;
return ^TABBaseComponent *(NSString *name) {
if (!weakSelf.dictComponents[name]) {
NSAssert(NO, @"The name is not exist, please check it carefully.");
TABComponentLayer *layer = TABComponentLayer.new;
layer.loadStyle = TABViewLoadAnimationRemove;
return [TABBaseComponent componentWithLayer:layer manager:weakSelf];
}
TABBaseComponent *component = weakSelf.dictComponents[name];
return component;
};
}

- (TABBaseComponentBlock _Nullable)animation {
__weak typeof(self) weakSelf = self;
return ^TABBaseComponent *(NSInteger index) {
if (index >= weakSelf.components.count) {
#ifdef DEBUG
NSAssert(NO, @"Array bound, please check it carefully.");
#else
TABComponentLayer *layer = TABComponentLayer.new;
layer.loadStyle = TABViewLoadAnimationRemove;
return [TABBaseComponent componentWithLayer:layer manager:weakSelf];
#endif
}
return weakSelf.components[index];
};
Expand All @@ -67,11 +91,8 @@ - (TABBaseComponentArrayBlock _Nullable)animations {
return ^NSArray <TABBaseComponent *> *(NSInteger location, NSInteger length) {

if (location + length > weakSelf.components.count) {
#ifdef DEBUG
NSAssert(NO, @"Array bound, please check it carefully.");
#else
return @[];
#endif
}

NSMutableArray <TABBaseComponent *> *tempArray = @[].mutableCopy;
Expand Down Expand Up @@ -104,11 +125,8 @@ - (TABBaseComponentArrayWithIndexsBlock)animationsWithIndexs {
if(arg < 0) continue;
if (arg > 1000) break;
if (arg >= weakSelf.components.count) {
#ifdef DEBUG
NSAssert(NO, @"如果运行到此断言,先检查是否调用了超过数组下标的index。若是确定没有,请取消使用该方法,使用单个获取的方式");
#else
break;
#endif
}

[resultArray addObject:weakSelf.components[arg]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface TABAnimatedProductHelper : NSObject

/// 填充数据, 并启动嵌套的view
/// @param view view
/// @param isHidden 是否隐藏子view
/// @param rootView 最初始view
+ (void)fullDataAndStartNestAnimation:(UIView *)view isHidden:(BOOL)isHidden rootView:(UIView *)rootView;
+ (void)fullDataAndStartNestAnimation:(UIView *)view isHidden:(BOOL)isHidden superView:(UIView *)superView rootView:(UIView *)rootView;

/**
恢复数据
Expand Down
53 changes: 48 additions & 5 deletions TABAnimatedDemo/TABAnimated/Product/TABAnimatedProductHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,47 @@
#import "UIView+TABAnimatedProduction.h"
#import "UIView+TABControlModel.h"
#import "UIView+TABControlAnimation.h"
#import <objc/runtime.h>
#import "UIView+TABAnimated.h"

static NSString * const kShortFillString = @" "; // 16
static NSString * const kLongFillString = @" "; // 48

static const CGFloat kTagDefaultFontSize = 12.f;
static const CGFloat kTagDefaultFontSize = 10.f;
static const CGFloat kTagLabelHeight = 20.f;
static const CGFloat kTagLabelMinWidth = 15.f;

@implementation TABAnimatedProductHelper

+ (void)fullDataAndStartNestAnimation:(UIView *)view isHidden:(BOOL)isHidden rootView:(UIView *)rootView {
+ (void)nameWithInstance:(UIView *)instance
superObject:(UIView *)superObject {
unsigned int numIvars = 0;
NSString *key = nil;
Ivar *ivars = class_copyIvarList(superObject.class, &numIvars);
for (int i = 0; i < numIvars; i++) {
Ivar thisIvar = ivars[i];
const char *type = ivar_getTypeEncoding(thisIvar);
NSString *stringType = [NSString stringWithCString:type encoding:NSUTF8StringEncoding];
if (![stringType hasPrefix:@"@"]) {
continue;
}

UIView *thisObject = object_getIvar(superObject, thisIvar);
if (thisObject == instance) {
key = [NSString stringWithUTF8String:ivar_getName(thisIvar)];
if (key && key.length > 0) {
if ([[key substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"_"]) {
thisObject.tab_name = [key substringFromIndex:1];
}else {
thisObject.tab_name = key;
}
}
}
}
free(ivars);
}

+ (void)fullDataAndStartNestAnimation:(UIView *)view isHidden:(BOOL)isHidden superView:(UIView *)superView rootView:(UIView *)rootView {

if ([view isKindOfClass:[UITableView class]] ||
[view isKindOfClass:[UICollectionView class]]) {
Expand All @@ -39,7 +69,16 @@ + (void)fullDataAndStartNestAnimation:(UIView *)view isHidden:(BOOL)isHidden roo
for (int i = 0; i < subViews.count; i++) {

UIView *subV = subViews[i];
[self fullDataAndStartNestAnimation:subV isHidden:isHidden rootView:rootView];

UIView *targetView;
if (![NSStringFromClass(subV.class) hasPrefix:@"UI"]) {
targetView = subV;
}else {
targetView = view;
}
[self fullDataAndStartNestAnimation:subV isHidden:isHidden superView:targetView rootView:rootView];

[TABAnimatedProductHelper nameWithInstance:subV superObject:superView];

if ([subV isKindOfClass:[UITableView class]] || [subV isKindOfClass:[UICollectionView class]]) {
if (subV.tabAnimated) {
Expand Down Expand Up @@ -219,7 +258,11 @@ + (TABComponentLayer *)getBackgroundLayerWithView:(UIView *)view controlView:(UI
+ (void)addTagWithComponentLayer:(TABComponentLayer *)layer isLines:(BOOL)isLines {
CATextLayer *textLayer = [CATextLayer layer];
CGFloat width = layer.frame.size.width > kTagLabelMinWidth ? layer.frame.size.width : kTagLabelMinWidth;
textLayer.string = [NSString stringWithFormat:@"%ld",(long)layer.tagIndex];
if (layer.tagName.length > 0) {
textLayer.string = [NSString stringWithFormat:@"%@ %ld", layer.tagName, (long)layer.tagIndex];
}else {
textLayer.string = [NSString stringWithFormat:@"%ld", (long)layer.tagIndex];
}
if (isLines) {
textLayer.frame = CGRectMake(0, 0, width, kTagLabelHeight);
}else if (layer.origin != TABComponentLayerOriginImageView) {
Expand All @@ -229,7 +272,7 @@ + (void)addTagWithComponentLayer:(TABComponentLayer *)layer isLines:(BOOL)isLine
}
textLayer.contentsScale = ([[UIScreen mainScreen] scale] > 3.0) ? [[UIScreen mainScreen] scale] : 3.0;
textLayer.fontSize = kTagDefaultFontSize;
textLayer.alignmentMode = kCAAlignmentRight;
textLayer.alignmentMode = kCAAlignmentCenter;
textLayer.foregroundColor = UIColor.redColor.CGColor;
[layer addSublayer:textLayer];
}
Expand Down
4 changes: 3 additions & 1 deletion TABAnimatedDemo/TABAnimated/Product/TABAnimatedProductImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#import "TABAnimationManagerImpl.h"

#import "TABAnimated.h"
#import "UIView+TABAnimated.h"

@interface TABAnimatedProductImpl() {
// self存在即存在
Expand Down Expand Up @@ -316,7 +317,7 @@ - (void)_productBackgroundLayerWithView:(UIView *)view needReset:(BOOL)needReset
- (void)_productWithView:(UIView *)view needReset:(BOOL)needReset isCard:(BOOL)isCard {

[self.weakTargetViewArray addPointer:(__bridge void * _Nullable)(view)];
[TABAnimatedProductHelper fullDataAndStartNestAnimation:view isHidden:!needReset rootView:view];
[TABAnimatedProductHelper fullDataAndStartNestAnimation:view isHidden:!needReset superView:view rootView:view];
[view layoutSubviews];
view.hidden = YES;

Expand Down Expand Up @@ -409,6 +410,7 @@ - (void)_recurseProductLayerWithView:(UIView *)view
layer = [self _createLayerWithView:subV needRemove:needRemove color:animatedColor isCard:isCard];
layer.serializationImpl = _controlView.tabAnimated.serializationImpl;
layer.tagIndex = self->_targetTagIndex;
layer.tagName = subV.tab_name;
[array addObject:layer];
_targetTagIndex++;
}
Expand Down
5 changes: 5 additions & 0 deletions TABAnimatedDemo/TABAnimated/Product/TABComponentLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ typedef NS_ENUM(NSInteger, TABComponentLayerOrigin) {
*/
@property (nonatomic, assign) NSInteger tagIndex;

/**
* 该动画元素基于UIView映射的属性名
*/
@property (nonatomic, copy) NSString *tagName;

#pragma mark - 配置成多行的动画元素

/**
Expand Down
9 changes: 9 additions & 0 deletions TABAnimatedDemo/TABAnimated/Product/TABComponentLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ - (instancetype)init {
_spaceDict = @{}.mutableCopy;
_widthDict = @{}.mutableCopy;
_heightDict = @{}.mutableCopy;
#ifdef DEBUG
self.masksToBounds = NO;
#endif
}
return self;
}
Expand Down Expand Up @@ -108,8 +111,10 @@ - (void)_addLinesLayer:(TABComponentLayer *)layer animatedHeight:(CGFloat)animat
TABViewLoadAnimationStyle loadStyle = layer.loadStyle;
BOOL withoutAnimation = layer.withoutAnimation;
NSInteger tagIndex = layer.tagIndex;
NSString *tagName = layer.tagName;
TABComponentLayerOrigin origin = layer.origin;


CGFloat textHeight;
if (animatedHeight > 0.) {
textHeight = animatedHeight;
Expand Down Expand Up @@ -158,6 +163,7 @@ - (void)_addLinesLayer:(TABComponentLayer *)layer animatedHeight:(CGFloat)animat

if (i == lines - 1) {
sub.tagIndex = tagIndex;
sub.tagName = tagName;
#ifdef DEBUG
// 添加红色标记
if ([TABAnimated sharedAnimated].openAnimationTag)
Expand Down Expand Up @@ -226,6 +232,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_widthDict forKey:@"widthDict"];
[aCoder encodeObject:_heightDict forKey:@"heightDict"];
[aCoder encodeObject:_spaceDict forKey:@"spaceDict"];
[aCoder encodeObject:_tagName forKey:@"tagName"];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
Expand Down Expand Up @@ -270,6 +277,7 @@ - (id)initWithCoder:(NSCoder *)aDecoder {
self.widthDict = [aDecoder decodeObjectForKey:@"widthDict"];
self.heightDict = [aDecoder decodeObjectForKey:@"heightDict"];
self.spaceDict = [aDecoder decodeObjectForKey:@"spaceDict"];
self.tagName = [aDecoder decodeObjectForKey:@"tagName"];
}

if (self.serializationImpl) {
Expand Down Expand Up @@ -325,6 +333,7 @@ - (id)copyWithZone:(NSZone *)zone {
layer.widthDict = self.widthDict;
layer.heightDict = self.heightDict;
layer.spaceDict = self.spaceDict;
layer.tagName = self.tagName;

if(self.lineLayers.count != 0) {
layer.lineLayers = @[].mutableCopy;
Expand Down
19 changes: 19 additions & 0 deletions TABAnimatedDemo/TABAnimated/Util/UIView+TABAnimated.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// UIView+TABAnimated.h
// TABAnimatedDemo
//
// Created by wenhuan on 2021/4/27.
// Copyright © 2021 tigerAndBull. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIView (TABAnimated)

@property (nonatomic, copy) NSString *tab_name;

@end

NS_ASSUME_NONNULL_END
22 changes: 22 additions & 0 deletions TABAnimatedDemo/TABAnimated/Util/UIView+TABAnimated.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// UIView+TABAnimated.m
// TABAnimatedDemo
//
// Created by wenhuan on 2021/4/27.
// Copyright © 2021 tigerAndBull. All rights reserved.
//

#import "UIView+TABAnimated.h"
#import <objc/runtime.h>

@implementation UIView (TABAnimated)

- (NSString *)tab_name {
return objc_getAssociatedObject(self, @selector(tab_name));
}

- (void)setTab_name:(NSString *)tab_name {
objc_setAssociatedObject(self, @selector(tab_name), tab_name, OBJC_ASSOCIATION_COPY);
}

@end
Loading

0 comments on commit 3a0418a

Please sign in to comment.