Permalink
Browse files

Added useful CALayer and CATransaction extensions from bitswift/Velvet

  • Loading branch information...
1 parent 001a7d8 commit 9f491228bcdc81dbd982558f1127cab4acc7cd96 @jspahrsummers jspahrsummers committed Jul 17, 2012
@@ -275,12 +275,18 @@
D0C7650515B6156A00E7AC2C /* TUIHostView.h in Headers */ = {isa = PBXBuildFile; fileRef = D0C7650415B6156A00E7AC2C /* TUIHostView.h */; settings = {ATTRIBUTES = (Public, ); }; };
D0C7650615B6156A00E7AC2C /* TUIHostView.h in Headers */ = {isa = PBXBuildFile; fileRef = D0C7650415B6156A00E7AC2C /* TUIHostView.h */; settings = {ATTRIBUTES = (Public, ); }; };
D0C7650715B6156A00E7AC2C /* TUIHostView.h in Headers */ = {isa = PBXBuildFile; fileRef = D0C7650415B6156A00E7AC2C /* TUIHostView.h */; settings = {ATTRIBUTES = (Public, ); }; };
- D0C7651615B61E5A00E7AC2C /* TUIBridgedScrollView.h in Headers */ = {isa = PBXBuildFile; fileRef = D0C7651515B61E5900E7AC2C /* TUIBridgedScrollView.h */; settings = {ATTRIBUTES = (Public, ); }; };
- D0C7651715B61E5A00E7AC2C /* TUIBridgedScrollView.h in Headers */ = {isa = PBXBuildFile; fileRef = D0C7651515B61E5900E7AC2C /* TUIBridgedScrollView.h */; settings = {ATTRIBUTES = (Public, ); }; };
- D0C7651815B61E5A00E7AC2C /* TUIBridgedScrollView.h in Headers */ = {isa = PBXBuildFile; fileRef = D0C7651515B61E5900E7AC2C /* TUIBridgedScrollView.h */; settings = {ATTRIBUTES = (Public, ); }; };
D0C7651115B6189D00E7AC2C /* TUINSHostView.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7650D15B6189D00E7AC2C /* TUINSHostView.m */; };
D0C7651215B6189D00E7AC2C /* TUINSHostView.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7650D15B6189D00E7AC2C /* TUINSHostView.m */; };
D0C7651315B6189D00E7AC2C /* TUINSHostView.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7650D15B6189D00E7AC2C /* TUINSHostView.m */; };
+ D0C7651615B61E5A00E7AC2C /* TUIBridgedScrollView.h in Headers */ = {isa = PBXBuildFile; fileRef = D0C7651515B61E5900E7AC2C /* TUIBridgedScrollView.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ D0C7651715B61E5A00E7AC2C /* TUIBridgedScrollView.h in Headers */ = {isa = PBXBuildFile; fileRef = D0C7651515B61E5900E7AC2C /* TUIBridgedScrollView.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ D0C7651815B61E5A00E7AC2C /* TUIBridgedScrollView.h in Headers */ = {isa = PBXBuildFile; fileRef = D0C7651515B61E5900E7AC2C /* TUIBridgedScrollView.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ D0C7652715B6232100E7AC2C /* CALayer+TUIExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7652115B6232100E7AC2C /* CALayer+TUIExtensions.m */; };
+ D0C7652815B6232100E7AC2C /* CALayer+TUIExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7652115B6232100E7AC2C /* CALayer+TUIExtensions.m */; };
+ D0C7652915B6232100E7AC2C /* CALayer+TUIExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7652115B6232100E7AC2C /* CALayer+TUIExtensions.m */; };
+ D0C7652D15B6232100E7AC2C /* CATransaction+TUIExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7652315B6232100E7AC2C /* CATransaction+TUIExtensions.m */; };
+ D0C7652E15B6232100E7AC2C /* CATransaction+TUIExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7652315B6232100E7AC2C /* CATransaction+TUIExtensions.m */; };
+ D0C7652F15B6232100E7AC2C /* CATransaction+TUIExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7652315B6232100E7AC2C /* CATransaction+TUIExtensions.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -427,9 +433,13 @@
CBB74C9013BE6E1900C85CB5 /* TUIViewNSViewContainer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TUIViewNSViewContainer.m; sourceTree = "<group>"; };
D0C764EA15B611C200E7AC2C /* TUIBridgedView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TUIBridgedView.h; sourceTree = "<group>"; };
D0C7650415B6156A00E7AC2C /* TUIHostView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TUIHostView.h; sourceTree = "<group>"; };
- D0C7651515B61E5900E7AC2C /* TUIBridgedScrollView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TUIBridgedScrollView.h; sourceTree = "<group>"; };
D0C7650C15B6189D00E7AC2C /* TUINSHostView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TUINSHostView.h; sourceTree = "<group>"; };
D0C7650D15B6189D00E7AC2C /* TUINSHostView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TUINSHostView.m; sourceTree = "<group>"; };
+ D0C7651515B61E5900E7AC2C /* TUIBridgedScrollView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TUIBridgedScrollView.h; sourceTree = "<group>"; };
+ D0C7652015B6232100E7AC2C /* CALayer+TUIExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "CALayer+TUIExtensions.h"; sourceTree = "<group>"; };
+ D0C7652115B6232100E7AC2C /* CALayer+TUIExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "CALayer+TUIExtensions.m"; sourceTree = "<group>"; };
+ D0C7652215B6232100E7AC2C /* CATransaction+TUIExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "CATransaction+TUIExtensions.h"; sourceTree = "<group>"; };
+ D0C7652315B6232100E7AC2C /* CATransaction+TUIExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "CATransaction+TUIExtensions.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -576,6 +586,10 @@
CBB74C3C13BE6E1900C85CB5 /* CoreText+Additions.m */,
884E8F591538809C000F7A8D /* CAAnimation+TUIExtensions.h */,
884E8F5A1538809C000F7A8D /* CAAnimation+TUIExtensions.m */,
+ D0C7652015B6232100E7AC2C /* CALayer+TUIExtensions.h */,
+ D0C7652115B6232100E7AC2C /* CALayer+TUIExtensions.m */,
+ D0C7652215B6232100E7AC2C /* CATransaction+TUIExtensions.h */,
+ D0C7652315B6232100E7AC2C /* CATransaction+TUIExtensions.m */,
);
name = Support;
path = lib/Support;
@@ -720,7 +734,6 @@
D0C764ED15B611C200E7AC2C /* TUIBridgedView.h in Headers */,
D0C7650715B6156A00E7AC2C /* TUIHostView.h in Headers */,
D0C7651815B61E5A00E7AC2C /* TUIBridgedScrollView.h in Headers */,
- D0C7651015B6189D00E7AC2C /* TUINSHostView.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -785,7 +798,6 @@
D0C764EB15B611C200E7AC2C /* TUIBridgedView.h in Headers */,
D0C7650515B6156A00E7AC2C /* TUIHostView.h in Headers */,
D0C7651615B61E5A00E7AC2C /* TUIBridgedScrollView.h in Headers */,
- D0C7650E15B6189D00E7AC2C /* TUINSHostView.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -806,7 +818,6 @@
D0C764EC15B611C200E7AC2C /* TUIBridgedView.h in Headers */,
D0C7650615B6156A00E7AC2C /* TUIHostView.h in Headers */,
D0C7651715B61E5A00E7AC2C /* TUIBridgedScrollView.h in Headers */,
- D0C7650F15B6189D00E7AC2C /* TUINSHostView.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1010,6 +1021,8 @@
884E8F5715387E11000F7A8D /* TUIPopover.m in Sources */,
884E8F601538809C000F7A8D /* CAAnimation+TUIExtensions.m in Sources */,
D0C7651315B6189D00E7AC2C /* TUINSHostView.m in Sources */,
+ D0C7652915B6232100E7AC2C /* CALayer+TUIExtensions.m in Sources */,
+ D0C7652F15B6232100E7AC2C /* CATransaction+TUIExtensions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1077,6 +1090,8 @@
30D399C9156D8ADD006ECDAE /* TUIProgressBar.m in Sources */,
88D81D001577EF0D009D453B /* TUIStyledView.m in Sources */,
D0C7651115B6189D00E7AC2C /* TUINSHostView.m in Sources */,
+ D0C7652715B6232100E7AC2C /* CALayer+TUIExtensions.m in Sources */,
+ D0C7652D15B6232100E7AC2C /* CATransaction+TUIExtensions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1150,6 +1165,8 @@
884E8F5615387E11000F7A8D /* TUIPopover.m in Sources */,
884E8F5F1538809C000F7A8D /* CAAnimation+TUIExtensions.m in Sources */,
D0C7651215B6189D00E7AC2C /* TUINSHostView.m in Sources */,
+ D0C7652815B6232100E7AC2C /* CALayer+TUIExtensions.m in Sources */,
+ D0C7652E15B6232100E7AC2C /* CATransaction+TUIExtensions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,39 @@
+//
+// CALayer+TUIExtensions.h
+//
+// Created by Josh Vera on 11/26/11.
+// Copyright (c) 2011 Bitswift. All rights reserved.
+//
+
+#import <QuartzCore/QuartzCore.h>
+
+/**
+ * Additional geometry conversions and geometrical functions for CALayer.
+ */
+@interface CALayer (TUIExtensions)
+/**
+ * Converts a rectangle from the receiver's coordinate system to that of a given
+ * layer, taking into account any layer clipping between the two.
+ *
+ * This will traverse the layer hierarchy, finding a common ancestor between the
+ * receiver and 'layer' to use as a base for geometry conversion. If any layers
+ * along the way (including the receiver, 'layer', and the common ancestor) have
+ * masksToBounds set to YES, the rectangle takes into account their clipping
+ * paths, such that the final result represents a rectangle that would actually
+ * be visible on screen.
+ *
+ * The receiver and 'layer' must have a common ancestor.
+ */
+- (CGRect)tui_convertAndClipRect:(CGRect)rect toLayer:(CALayer *)layer;
+
+/**
+ * Converts a rectangle from the coordinate system of 'layer' to the
+ * receiver's, taking into account any layer clipping between the two.
+ *
+ * This will call -tui_convertAndClipRect:toLayer:> on 'layer' with the receiver
+ * as the argument.
+ *
+ * The receiver and 'layer' must have a common ancestor.
+ */
+- (CGRect)tui_convertAndClipRect:(CGRect)rect fromLayer:(CALayer *)layer;
+@end
@@ -0,0 +1,55 @@
+//
+// CALayer+TUIExtensions.m
+//
+// Created by Josh Vera on 11/26/11.
+// Copyright (c) 2011 Bitswift. All rights reserved.
+//
+
+#import "CALayer+TUIExtensions.h"
+
+static CGRect convertAndClipRectFromSuperlayers (CGRect rect, CALayer *layer);
+
+static CGRect convertAndClipRectFromSuperlayers (CGRect rect, CALayer *layer) {
+ CALayer *superlayer = layer.superlayer;
+ if (superlayer) {
+ rect = convertAndClipRectFromSuperlayers(rect, superlayer);
+ if (CGRectIsNull(rect))
+ return CGRectNull;
+
+ rect = [layer convertRect:rect fromLayer:superlayer];
+ }
+
+ if (layer.masksToBounds) {
+ rect = CGRectIntersection(rect, layer.visibleRect);
+ }
+
+ return rect;
+}
+
+@implementation CALayer (TUIExtensions)
+- (CGRect)tui_convertAndClipRect:(CGRect)rect toLayer:(CALayer *)layer {
+ CALayer *clippingLayer = self.superlayer;
+ CALayer *lastLayer = self;
+ while (clippingLayer) {
+ if (lastLayer.masksToBounds) {
+ rect = CGRectIntersection(rect, lastLayer.visibleRect);
+ if (CGRectIsNull(rect))
+ return CGRectNull;
+ }
+
+ rect = [clippingLayer convertRect:rect fromLayer:lastLayer];
+
+ lastLayer = clippingLayer;
+ clippingLayer = clippingLayer.superlayer;
+ }
+
+ // 'rect' is in the coordinate system of the root layer, and has been
+ // clipped accordingly
+ return convertAndClipRectFromSuperlayers(rect, layer);
+}
+
+- (CGRect)tui_convertAndClipRect:(CGRect)rect fromLayer:(CALayer *)layer {
+ return [layer tui_convertAndClipRect:rect toLayer:self];
+}
+
+@end
@@ -0,0 +1,22 @@
+//
+// CATransaction+TUIExtensions.h
+//
+// Created by James Lawton on 11/23/11.
+// Copyright (c) 2011 Bitswift. All rights reserved.
+//
+
+#import <QuartzCore/QuartzCore.h>
+
+/**
+ * Extends CATransaction with useful block-based features.
+ */
+@interface CATransaction (TUIExtensions)
+
+/**
+ * Executes a block with actions disabled.
+ *
+ * This will have the effect of suppressing animation.
+ */
++ (void)tui_performWithDisabledActions:(void(^)(void))block;
+
+@end
@@ -0,0 +1,22 @@
+//
+// CATransaction+TUIExtensions.m
+//
+// Created by James Lawton on 11/23/11.
+// Copyright (c) 2011 Bitswift. All rights reserved.
+//
+
+#import "CATransaction+TUIExtensions.h"
+
+@implementation CATransaction (TUIExtensions)
++ (void)tui_performWithDisabledActions:(void(^)(void))block {
+ if ([self disableActions]) {
+ // actions are already disabled
+ block();
+ } else {
+ [self setDisableActions:YES];
+ block();
+ [self setDisableActions:NO];
+ }
+}
+
+@end

0 comments on commit 9f49122

Please sign in to comment.