Permalink
Browse files

Added an example of how to use an NSValueTransformer constraint.

Also, reverted this commit:

http://github.com/davedelong/CHLayoutManager/commit/5719aff9ac293a53f9d216bab356f73ae120bfa0

When adding a constraint to a view, that constraint should be evaluated immediately, which I hadn't considered when I took it out.
  • Loading branch information...
1 parent 250c78f commit 99bc87cead31330c3b42d2b65c2553b82e22f9ca @davedelong committed Aug 15, 2010
View
6 CHLayoutManager.m
@@ -128,11 +128,15 @@ - (void) processView:(NSView *)aView {
}
[processedViews addObject:aView];
+ NSArray * viewConstraints = [self constraintsOnView:aView];
+ for (CHLayoutConstraint * constraint in viewConstraints) {
+ [constraint applyToTargetView:aView];
+ }
+
/**
ORDER OF OPERATIONS:
1. See if this view has any siblings with constraints to this view
2. See if this view has any children with constraints to superview
-
**/
//siblings constrained to this view
View
6 CHLayoutManager.xcodeproj/project.pbxproj
@@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
1DDD58160DA1D0A300B32029 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1DDD58140DA1D0A300B32029 /* MainMenu.xib */; };
256AC3DA0F4B6AC300CF3369 /* CHLayoutManagerAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 256AC3D90F4B6AC300CF3369 /* CHLayoutManagerAppDelegate.m */; };
+ 551F7B8F121867DF00A573EE /* SinTransformer.m in Sources */ = {isa = PBXBuildFile; fileRef = 551F7B8E121867DF00A573EE /* SinTransformer.m */; };
558686A31215C7910014B300 /* CHLayoutManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 558686A21215C7910014B300 /* CHLayoutManager.m */; };
558686A61215CBD40014B300 /* CHLayoutConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = 558686A51215CBD40014B300 /* CHLayoutConstraint.m */; };
558686C21215D0C40014B300 /* NSView+CHLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = 558686C11215D0C40014B300 /* NSView+CHLayout.m */; };
@@ -29,6 +30,8 @@
29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = "<absolute>"; };
29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
550C4453121663CC00E0002B /* CHLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHLayout.h; sourceTree = "<group>"; };
+ 551F7B8D121867DF00A573EE /* SinTransformer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SinTransformer.h; sourceTree = "<group>"; };
+ 551F7B8E121867DF00A573EE /* SinTransformer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SinTransformer.m; sourceTree = "<group>"; };
558686A11215C7910014B300 /* CHLayoutManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHLayoutManager.h; sourceTree = "<group>"; };
558686A21215C7910014B300 /* CHLayoutManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CHLayoutManager.m; sourceTree = "<group>"; };
558686A41215CBD40014B300 /* CHLayoutConstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHLayoutConstraint.h; sourceTree = "<group>"; };
@@ -63,6 +66,8 @@
558686A51215CBD40014B300 /* CHLayoutConstraint.m */,
256AC3D80F4B6AC300CF3369 /* CHLayoutManagerAppDelegate.h */,
256AC3D90F4B6AC300CF3369 /* CHLayoutManagerAppDelegate.m */,
+ 551F7B8D121867DF00A573EE /* SinTransformer.h */,
+ 551F7B8E121867DF00A573EE /* SinTransformer.m */,
);
name = Classes;
sourceTree = "<group>";
@@ -193,6 +198,7 @@
558686A31215C7910014B300 /* CHLayoutManager.m in Sources */,
558686A61215CBD40014B300 /* CHLayoutConstraint.m in Sources */,
558686C21215D0C40014B300 /* NSView+CHLayout.m in Sources */,
+ 551F7B8F121867DF00A573EE /* SinTransformer.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
2 CHLayoutManagerAppDelegate.h
@@ -35,6 +35,8 @@
IBOutlet NSButton * leftVerticalButton;
IBOutlet NSButton * rightVerticalButton;
+
+ IBOutlet NSButton * helpButton;
}
@property (assign) IBOutlet NSWindow *window;
View
13 CHLayoutManagerAppDelegate.m
@@ -25,6 +25,8 @@ of this software and associated documentation files (the "Software"), to deal
#import "CHLayoutManagerAppDelegate.h"
#import "CHLayout.h"
+#import "SinTransformer.h"
+
@implementation CHLayoutManagerAppDelegate
@@ -38,7 +40,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[button2 addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMaxY relativeTo:@"button1" attribute:CHLayoutConstraintAttributeMaxY]];
[button2 addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeWidth relativeTo:@"button1" attribute:CHLayoutConstraintAttributeWidth]];
- [progress startAnimation:nil];
+// [progress startAnimation:nil];
CHLayoutConstraint * centerHorizontal = [CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMidX relativeTo:@"superview" attribute:CHLayoutConstraintAttributeMidX];
CHLayoutConstraint * centerVertical = [CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMidY relativeTo:@"superview" attribute:CHLayoutConstraintAttributeMidY];
[progress addConstraint:centerHorizontal];
@@ -59,6 +61,15 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[rightVerticalButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMaxY relativeTo:@"leftVertical" attribute:CHLayoutConstraintAttributeMinY blockTransformer:transformer]];
#endif
+
+ [helpButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinX relativeTo:@"leftVertical" attribute:CHLayoutConstraintAttributeMinY scale:2.0 offset:0.0]];
+
+ SinTransformer * sinTransformer = [[SinTransformer alloc] init];
+ [helpButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinY relativeTo:@"button1" attribute:CHLayoutConstraintAttributeMinY valueTransformer:sinTransformer]];
+ [sinTransformer release];
+
+ [self sliderChanged:nil];
+// [[CHLayoutManager sharedLayoutManager] beginProcessingView:[window contentView]];
}
- (IBAction) sliderChanged:(id)sender {
View
74 English.lproj/MainMenu.xib
@@ -1318,7 +1318,7 @@
<object class="NSWindowTemplate" id="972006081">
<int key="NSWindowStyleMask">15</int>
<int key="NSWindowBacking">2</int>
- <string key="NSWindowRect">{{335, 390}, {480, 360}}</string>
+ <string key="NSWindowRect">{{335, 156}, {746, 594}}</string>
<int key="NSWTFlags">1954021376</int>
<string key="NSWindowTitle">CHLayoutManager</string>
<string key="NSWindowClass">NSWindow</string>
@@ -1356,7 +1356,7 @@
<object class="NSButton" id="433928903">
<reference key="NSNextResponder" ref="439893737"/>
<int key="NSvFlags">268</int>
- <string key="NSFrame">{{198, 122}, {96, 32}}</string>
+ <string key="NSFrame">{{198, 356}, {96, 32}}</string>
<reference key="NSSuperview" ref="439893737"/>
<bool key="NSEnabled">YES</bool>
<object class="NSButtonCell" key="NSCell" id="689515343">
@@ -1377,15 +1377,15 @@
<reference key="NSNextResponder" ref="439893737"/>
<int key="NSvFlags">1292</int>
<object class="NSPSMatrix" key="NSDrawMatrix"/>
- <string key="NSFrame">{{230, 57}, {32, 32}}</string>
+ <string key="NSFrame">{{230, 291}, {32, 32}}</string>
<reference key="NSSuperview" ref="439893737"/>
<int key="NSpiFlags">20490</int>
<double key="NSMaxValue">100</double>
</object>
<object class="NSButton" id="231800380">
<reference key="NSNextResponder" ref="439893737"/>
<int key="NSvFlags">268</int>
- <string key="NSFrame">{{428, 13}, {39, 38}}</string>
+ <string key="NSFrame">{{428, 247}, {39, 38}}</string>
<reference key="NSSuperview" ref="439893737"/>
<bool key="NSEnabled">YES</bool>
<object class="NSButtonCell" key="NSCell" id="529454957">
@@ -1409,7 +1409,7 @@
<object class="NSButton" id="542142850">
<reference key="NSNextResponder" ref="439893737"/>
<int key="NSvFlags">268</int>
- <string key="NSFrame">{{37, 13}, {39, 38}}</string>
+ <string key="NSFrame">{{37, 247}, {39, 38}}</string>
<reference key="NSSuperview" ref="439893737"/>
<bool key="NSEnabled">YES</bool>
<object class="NSButtonCell" key="NSCell" id="337752024">
@@ -1429,7 +1429,7 @@
<object class="NSSlider" id="806248811">
<reference key="NSNextResponder" ref="439893737"/>
<int key="NSvFlags">268</int>
- <string key="NSFrame">{{17, 11}, {21, 331}}</string>
+ <string key="NSFrame">{{17, 245}, {21, 331}}</string>
<reference key="NSSuperview" ref="439893737"/>
<bool key="NSEnabled">YES</bool>
<object class="NSSliderCell" key="NSCell" id="679383406">
@@ -1448,8 +1448,28 @@
<bool key="NSVertical">YES</bool>
</object>
</object>
+ <object class="NSButton" id="900184571">
+ <reference key="NSNextResponder" ref="439893737"/>
+ <int key="NSvFlags">268</int>
+ <string key="NSFrame">{{233, 250}, {25, 25}}</string>
+ <reference key="NSSuperview" ref="439893737"/>
+ <bool key="NSEnabled">YES</bool>
+ <object class="NSButtonCell" key="NSCell" id="616198472">
+ <int key="NSCellFlags">67239424</int>
+ <int key="NSCellFlags2">134217728</int>
+ <string key="NSContents"/>
+ <reference key="NSSupport" ref="442173890"/>
+ <reference key="NSControlView" ref="900184571"/>
+ <int key="NSButtonFlags">-2038415105</int>
+ <int key="NSButtonFlags2">161</int>
+ <string key="NSAlternateContents"/>
+ <string key="NSKeyEquivalent"/>
+ <int key="NSPeriodicDelay">200</int>
+ <int key="NSPeriodicInterval">25</int>
+ </object>
+ </object>
</object>
- <string key="NSFrameSize">{480, 360}</string>
+ <string key="NSFrameSize">{746, 594}</string>
<reference key="NSSuperview"/>
</object>
<string key="NSScreenRect">{{0, 0}, {1920, 1178}}</string>
@@ -2209,6 +2229,14 @@
</object>
<int key="connectionID">564</int>
</object>
+ <object class="IBConnectionRecord">
+ <object class="IBOutletConnection" key="connection">
+ <string key="label">helpButton</string>
+ <reference key="source" ref="976324537"/>
+ <reference key="destination" ref="900184571"/>
+ </object>
+ <int key="connectionID">567</int>
+ </object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
<object class="NSArray" key="orderedObjects">
@@ -2770,6 +2798,7 @@
<reference ref="338901439"/>
<reference ref="231800380"/>
<reference ref="542142850"/>
+ <reference ref="900184571"/>
</object>
<reference key="parent" ref="972006081"/>
</object>
@@ -3334,6 +3363,20 @@
<reference key="object" ref="679383406"/>
<reference key="parent" ref="806248811"/>
</object>
+ <object class="IBObjectRecord">
+ <int key="objectID">565</int>
+ <reference key="object" ref="900184571"/>
+ <object class="NSMutableArray" key="children">
+ <bool key="EncodedWithXMLCoder">YES</bool>
+ <reference ref="616198472"/>
+ </object>
+ <reference key="parent" ref="439893737"/>
+ </object>
+ <object class="IBObjectRecord">
+ <int key="objectID">566</int>
+ <reference key="object" ref="616198472"/>
+ <reference key="parent" ref="900184571"/>
+ </object>
</object>
</object>
<object class="NSMutableDictionary" key="flattenedProperties">
@@ -3569,6 +3612,8 @@
<string>56.IBPluginDependency</string>
<string>56.ImportedFromIB2</string>
<string>560.IBPluginDependency</string>
+ <string>565.IBPluginDependency</string>
+ <string>566.IBPluginDependency</string>
<string>57.IBEditorWindowLastContentRect</string>
<string>57.IBPluginDependency</string>
<string>57.ImportedFromIB2</string>
@@ -3733,9 +3778,9 @@
<integer value="1"/>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<integer value="1"/>
- <string>{{380, 496}, {480, 360}}</string>
+ <string>{{380, 262}, {746, 594}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
- <string>{{380, 496}, {480, 360}}</string>
+ <string>{{380, 262}, {746, 594}}</string>
<integer value="1"/>
<string>{{33, 99}, {480, 360}}</string>
<string>{3.40282e+38, 3.40282e+38}</string>
@@ -3833,6 +3878,8 @@
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<integer value="1"/>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
+ <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
+ <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>{{286, 129}, {275, 183}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<integer value="1"/>
@@ -3883,7 +3930,7 @@
</object>
</object>
<nil key="sourceID"/>
- <int key="maxID">564</int>
+ <int key="maxID">567</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -3929,6 +3976,7 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<string>button1</string>
<string>button2</string>
+ <string>helpButton</string>
<string>leftVerticalButton</string>
<string>progress</string>
<string>rightVerticalButton</string>
@@ -3940,6 +3988,7 @@
<string>NSButton</string>
<string>NSButton</string>
<string>NSButton</string>
+ <string>NSButton</string>
<string>NSProgressIndicator</string>
<string>NSButton</string>
<string>NSSlider</string>
@@ -3952,6 +4001,7 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<string>button1</string>
<string>button2</string>
+ <string>helpButton</string>
<string>leftVerticalButton</string>
<string>progress</string>
<string>rightVerticalButton</string>
@@ -3969,6 +4019,10 @@
<string key="candidateClassName">NSButton</string>
</object>
<object class="IBToOneOutletInfo">
+ <string key="name">helpButton</string>
+ <string key="candidateClassName">NSButton</string>
+ </object>
+ <object class="IBToOneOutletInfo">
<string key="name">leftVerticalButton</string>
<string key="candidateClassName">NSButton</string>
</object>
View
17 README.markdown
@@ -24,6 +24,14 @@ In order to use CHLayoutManager in your app, copy these 7 files into your projec
Then `#import "CHLayout.h"` in any .m file that needs to apply constraints to views.
+##Improvements over `CAConstraint`
+
+- `CHLayoutConstraints` support all the features of `CAConstraints`
+- `CHLayoutConstraints` allow you to specify an `NSValueTransformer` in order to do more complex transformations.
+- `CHLayoutConstraints` allow you to specify a block in order to do more complex transformations.
+
+(The included sample application shows how to use a `CHLayoutTransformer` block in order to apply constraints that are not possible via the normal mechanism.)
+
##Supported Platforms
- Mac OS X 10.5+
@@ -33,14 +41,7 @@ Then `#import "CHLayout.h"` in any .m file that needs to apply constraints to vi
- It's possible to set up circular dependencies on constraints. Do so at your own risk.
- In order to constrain a view to its superview, create a constraint with the `sourceName` of `@"superview"`.
- It's very easy to create a [retain cycle][retain-cycle] if you use a block transformer that references `self` in a constraint, and then have `self` retain the constraint. ([Blocks retain objects that they capture][block-retain])
-
-##Improvements over `CAConstraint` objects
-
-- `CHLayoutConstraints` support all the features of `CAConstraints`
-- `CHLayoutConstraints` allow you to specify an `NSValueTransformer` in order to do more complex transformations.
-- `CHLayoutConstraints` allow you to specify a block in order to do more complex transformations.
-
-(The included sample application shows how to use a `CHLayoutTransformer` block in order to apply constraints that are not possible via the normal mechanism.)
+- You may add as many constraints to a view as you like. If a view has multiple constraints on the same attributed, they will all be evaluated in the order they were added. Beware.
##License
View
16 SinTransformer.h
@@ -0,0 +1,16 @@
+//
+// SinTransformer.h
+// CHLayoutManager
+//
+// Created by Dave DeLong on 8/15/10.
+// Copyright 2010 Home. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface SinTransformer : NSValueTransformer {
+
+}
+
+@end
View
20 SinTransformer.m
@@ -0,0 +1,20 @@
+//
+// SinTransformer.m
+// CHLayoutManager
+//
+// Created by Dave DeLong on 8/15/10.
+// Copyright 2010 Home. All rights reserved.
+//
+
+#import "SinTransformer.h"
+
+
+@implementation SinTransformer
+
+- (id) transformedValue:(id)value {
+ CGFloat source = [value floatValue];
+ CGFloat output = fabs((50*sinf(source))) + 20;
+ return [NSNumber numberWithFloat:output];
+}
+
+@end

0 comments on commit 99bc87c

Please sign in to comment.