Parcourir la source

Activate/deactivate methods fallback to install/uninstall if NSLayoutConstraint activate property is not supported by iOS

Pavel Mazurin il y a 10 ans
Parent
commit
c743b558cc

+ 12 - 0
Masonry/MASCompositeConstraint.m

@@ -149,6 +149,18 @@
 
 #pragma mark - MASConstraint
 
+- (void)activate {
+    for (MASConstraint *constraint in self.childConstraints) {
+        [constraint activate];
+    }
+}
+
+- (void)deactivate {
+    for (MASConstraint *constraint in self.childConstraints) {
+        [constraint deactivate];
+    }
+}
+
 - (void)install {
     for (MASConstraint *constraint in self.childConstraints) {
         constraint.updateExisting = self.updateExisting;

+ 13 - 2
Masonry/MASConstraint.h

@@ -108,12 +108,12 @@
 - (MASConstraint *)with;
 
 /**
- *	optional semantic property which has no effect but improves the readability of constraint
+ *	Optional semantic property which has no effect but improves the readability of constraint
  */
 - (MASConstraint *)and;
 
 /**
- *	creates a new MASCompositeConstraint with the called attribute and reciever
+ *	Creates a new MASCompositeConstraint with the called attribute and reciever
  */
 - (MASConstraint *)left;
 - (MASConstraint *)top;
@@ -171,6 +171,17 @@
 @property (nonatomic, copy, readonly) MASConstraint *animator;
 #endif
 
+/**
+ *  Activates an NSLayoutConstraint if it's supported by an OS. 
+ *  Invokes install otherwise.
+ */
+- (void)activate;
+
+/**
+ *  Deactivates previously installed/activated NSLayoutConstraint.
+ */
+- (void)deactivate;
+
 /**
  *	Creates a NSLayoutConstraint and adds it to the appropriate view.
  */

+ 4 - 0
Masonry/MASConstraint.m

@@ -234,6 +234,10 @@
 
 #endif
 
+- (void)activate { MASMethodNotImplemented(); }
+
+- (void)deactivate { MASMethodNotImplemented(); }
+
 - (void)install { MASMethodNotImplemented(); }
 
 - (void)uninstall { MASMethodNotImplemented(); }

+ 34 - 19
Masonry/MASViewConstraint.m

@@ -110,7 +110,10 @@ static char kInstalledConstraintsKey;
 - (BOOL)isActive {
     BOOL active = YES;
 #ifdef __IPHONE_8_0
-    active = [self supportsActiveProperty] && [self.layoutConstraint isActive];
+    if ([self supportsActiveProperty])
+    {
+        active = [self.layoutConstraint isActive];
+    }
 #endif
     return active;
 }
@@ -285,20 +288,39 @@ static char kInstalledConstraintsKey;
 
 #pragma mark - MASConstraint
 
+- (void)activate {
+#if defined(__IPHONE_8_0)
+    if ([self supportsActiveProperty] && self.layoutConstraint) {
+        if (self.hasBeenInstalled) {
+            return;
+        }
+        self.layoutConstraint.active = YES;
+        [self.firstViewAttribute.view.mas_installedConstraints addObject:self];
+    } else
+#endif
+    {
+        [self install];
+    }
+}
+
+- (void)deactivate {
+#if defined(__IPHONE_8_0)
+    if ([self.layoutConstraint respondsToSelector:@selector(setActive:)]) {
+        self.layoutConstraint.active = NO;
+        [self.firstViewAttribute.view.mas_installedConstraints removeObject:self];
+    } else
+#endif
+    {
+        [self uninstall];
+    }
+}
+
 - (void)install {
     if (self.hasBeenInstalled) {
         return;
     }
     
     MAS_VIEW *firstLayoutItem = self.firstViewAttribute.view;
-
-#ifdef __IPHONE_8_0
-    if ([self supportsActiveProperty] && self.layoutConstraint) {
-        self.layoutConstraint.active = YES;
-        [firstLayoutItem.mas_installedConstraints addObject:self];
-        return;
-    }
-#endif
     
     NSLayoutAttribute firstLayoutAttribute = self.firstViewAttribute.layoutAttribute;
     MAS_VIEW *secondLayoutItem = self.secondViewAttribute.view;
@@ -372,16 +394,9 @@ static char kInstalledConstraintsKey;
 }
 
 - (void)uninstall {
-#ifdef __IPHONE_8_0
-    if ([self.layoutConstraint respondsToSelector:@selector(setActive:)]) {
-        self.layoutConstraint.active = NO;
-    } else
-#endif
-    {
-        [self.installedView removeConstraint:self.layoutConstraint];
-        self.layoutConstraint = nil;
-        self.installedView = nil;
-    }
+    [self.installedView removeConstraint:self.layoutConstraint];
+    self.layoutConstraint = nil;
+    self.installedView = nil;
     
     [self.firstViewAttribute.view.mas_installedConstraints removeObject:self];
 }

+ 22 - 0
Tests/Specs/MASCompositeConstraintSpec.m

@@ -152,6 +152,28 @@ SpecBegin(MASCompositeConstraint) {
     expect(superview.constraints).to.haveCountOf(0);
 }
 
+- (void)testActivateDeactivate
+{
+    NSArray *children = @[
+                          [[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_leading],
+                          [[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_trailing]
+                          ];
+    composite = [[MASCompositeConstraint alloc] initWithChildren:children];
+    composite.delegate = delegate;
+    MAS_VIEW *newView = MAS_VIEW.new;
+    [superview addSubview:newView];
+    
+    //first equality statement
+    composite.equalTo(newView);
+    [composite install];
+    
+    expect(superview.constraints).to.haveCountOf(2);
+    [composite deactivate];
+    expect(superview.constraints).to.haveCountOf(0);
+    [composite activate];
+    expect(superview.constraints).to.haveCountOf(2);
+}
+
 - (void)testAttributeChainingShouldCallDelegate {
     NSArray *children = @[
         [[MASViewConstraint alloc] initWithFirstViewAttribute:view.mas_left],