Pen Li 7 лет назад
Родитель
Сommit
2c8b1a979e

+ 4 - 0
common/LayoutConstraint/UIView+RAConstraint.h

@@ -41,6 +41,8 @@
 @property (nonatomic,strong,readonly) RAConstraint *ra_safeAreaRight API_AVAILABLE(ios(11.0),tvos(11.0));
 @property (nonatomic,strong,readonly) RAConstraint *ra_safeAreaBottom API_AVAILABLE(ios(11.0),tvos(11.0));
 
+@property (nonatomic,readonly,copy) void (^jk_modify)(CGFloat constant);
+
 @end
 
 #pragma mark - UIView
@@ -62,6 +64,8 @@
 @property (nonatomic,strong,readonly) RAConstraint *ra_safeAreaRight API_AVAILABLE(ios(11.0),tvos(11.0));
 @property (nonatomic,strong,readonly) RAConstraint *ra_safeAreaBottom API_AVAILABLE(ios(11.0),tvos(11.0));
 
+@property (nonatomic,copy,readonly) void (^jk_modifyConstraint)(NSString *identifier, CGFloat constant);
+
 - (void)ra_applyConstraints:(void(^)(RAConstraintMaker *maker))apply;
 
 /**

+ 81 - 10
common/LayoutConstraint/UIView+RAConstraint.m

@@ -36,6 +36,8 @@
 @property (nonatomic,copy) RAConstraint *(^ra_p_size_greaterThanOrEqualTo)(CGFloat size);
 @property (nonatomic,copy) RAConstraint *(^ra_p_size_lessThanOrEqualTo)(CGFloat size);
 
+@property (nonatomic,copy) void (^jk_p_modify)(CGFloat constant);
+
 @end
 
 @implementation RAConstraint
@@ -142,6 +144,19 @@
             
             return weakSelf;
         } copy];
+        
+        self.jk_p_modify = [^(CGFloat constant) {
+            
+            weakSelf.constant = constant;
+            if (weakSelf.constraint) {
+                weakSelf.constraint.constant = constant;
+                if ([weakSelf appliedViewIsView]) {
+                    UIView *appliedView = ((UIView *)weakSelf.appliedView);
+                    [appliedView.superview layoutIfNeeded];
+                }
+            }
+            
+        } copy];
     }
     return self;
 }
@@ -178,7 +193,11 @@
     return self.ra_p_size_greaterThanOrEqualTo;
 }
 
-- (void)jk_uninstall {
+- (void (^)(CGFloat))jk_modify {
+    return self.jk_p_modify;
+}
+
+- (void)ra_uninstall {
     
     if (self.appliedView && self.appliedViewIsView && self.validate && self.constraint) {
         
@@ -194,11 +213,12 @@
     
     [self.appendConstraints enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, RAConstraint * _Nonnull obj, BOOL * _Nonnull stop) {
        
-        [obj jk_uninstall];
+        [obj ra_uninstall];
     }];
     
     self.validate = NO;
     self.constraint = nil;
+    self.identifier = nil;
     
     self.toView = nil;
     self.relation = NSLayoutRelationEqual;
@@ -209,7 +229,7 @@
     [self.appendConstraints removeAllObjects];
 }
 
-- (void)jk_install {
+- (void)ra_install {
     
     if (self.appliedView && self.appliedViewIsView && self.validate && !self.constraint) {
         
@@ -223,6 +243,7 @@
                                                                        constant:self.constant];
         self.constraint = constraint;
         self.constraint.priority = self.priority;
+        self.constraint.identifier = self.identifier;
         
         if ((self.attribute == NSLayoutAttributeWidth || self.attribute == NSLayoutAttributeHeight) && !self.toView) {
             [appliedView addConstraint:self.constraint];
@@ -233,7 +254,7 @@
     
     [self.appendConstraints enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, RAConstraint * _Nonnull obj, BOOL * _Nonnull stop) {
         
-        [obj jk_install];
+        [obj ra_install];
     }];
 }
 
@@ -274,6 +295,8 @@
 
 @property (nonatomic,strong) NSMutableArray<RAConstraint *> *constraints;
 
+@property (nonatomic,copy) void (^jk_modifyConstraint)(NSString *identifier, CGFloat constant);
+
 @end
 
 @implementation RAConstraintMaker
@@ -292,10 +315,52 @@
                                                 self.width,
                                                 self.height
                                                 ]];
+        
+        __weak typeof(self) weakSelf = self;
+        self.jk_modifyConstraint = [^(NSString *identifier, CGFloat constant) {
+            
+            NSDictionary *constraintRes = [weakSelf findConstraintOfView:weakSelf.appliedView byIdentifier:identifier];
+            if (constraintRes) {
+                
+                UIView *view = constraintRes[@"view"];
+                NSLayoutConstraint *constraint = constraintRes[@"constraint"];
+                
+                constraint.constant = constant;
+                [view layoutIfNeeded];
+            }
+            
+        } copy];
     }
     return self;
 }
 
+- (NSDictionary *)findConstraintOfView:(UIView *)view byIdentifier:(NSString *)identifier {
+    if (!view || !identifier) {
+        return nil;
+    }
+    NSLayoutConstraint *result = nil;
+    
+    NSArray<NSLayoutConstraint *> *constraintArray = view.constraints;
+    for (NSLayoutConstraint *constraint in constraintArray) {
+        if (constraint.identifier && [constraint.identifier isEqualToString:identifier]) {
+            result = constraint;
+            break;
+        }
+    }
+    if (result) {
+        return @{
+                 @"view" : view,
+                 @"constraint" : result
+                 };
+    } else {
+        if (view.superview) {
+            return [self findConstraintOfView:view.superview byIdentifier:identifier];
+        } else {
+            return nil;
+        }
+    }
+}
+
 - (NSMutableArray<RAConstraint *> *)constraints {
     if (!_constraints) {
         _constraints = [NSMutableArray array];
@@ -422,21 +487,21 @@
     return _ra_safeAreaBottom;
 }
 
-- (void)jk_install {
+- (void)ra_install {
     if (self.appliedView) {
         
         for (RAConstraint *constraint in self.constraints) {
-            [constraint jk_install];
+            [constraint ra_install];
         }
     }
 }
 
-- (void)jk_uninstall {
+- (void)ra_uninstall {
     
     if (self.appliedView) {
         
         for (RAConstraint *constraint in self.constraints) {
-            [constraint jk_uninstall];
+            [constraint ra_uninstall];
         }
     }
 }
@@ -483,7 +548,7 @@ static const char RAMaker = '\a';
     
     if (maker) {
         [self setRA_Maker:maker];
-        [maker jk_install];
+        [maker ra_install];
         
         [self.superview layoutIfNeeded];
     }
@@ -491,7 +556,7 @@ static const char RAMaker = '\a';
 
 - (void)ra_remakeConstraints:(void(^)(RAConstraintMaker *maker))remake {
     
-    [[self ra_maker] jk_uninstall];
+    [[self ra_maker] ra_uninstall];
     
     [self ra_applyConstraints:remake];
 }
@@ -552,6 +617,12 @@ static const char RAMaker = '\a';
     return [self ra_maker].ra_safeAreaBottom;
 }
 
+// modify
+
+- (void (^)(NSString *, CGFloat))jk_modifyConstraint {
+    return [self ra_maker].jk_modifyConstraint;
+}
+
 @end
 
 #pragma mark - View Controller