Skip to content

Commit 69e856e

Browse files
committed
follow operation reworked
1 parent 60f70ae commit 69e856e

File tree

1 file changed

+118
-40
lines changed

1 file changed

+118
-40
lines changed

LayoutOps/LayoutOps/LayoutOps.swift

Lines changed: 118 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public func Combine(layoutOperations: [LayoutOperation]) -> LayoutOperation {
8888

8989

9090
public enum PutIntention {
91-
91+
9292
/**
9393
1. (view: v weight: x) - view with size calculated from weights
9494
2. (view: nil weight: x) - empty space with size calculated from weights
@@ -102,11 +102,11 @@ public enum PutIntention {
102102
2. (view: nil value: x) - empty space with fixed size
103103
3. (view: v value: nil) - keep current size of view
104104
4. (view: nil value: nil) - do nothing, nop
105-
105+
106106
*/
107107
case FixIntention(view: UIView?, value: CGFloat?)
108108

109-
func when(condition: (Void) -> Bool) -> PutIntention {
109+
public func when(condition: (Void) -> Bool) -> PutIntention {
110110
if condition() {
111111
return self
112112
} else {
@@ -136,8 +136,8 @@ public func Fix(view: UIView?, _ value: CGFloat?) -> PutIntention {
136136
return .FixIntention(view: view, value: value)
137137
}
138138

139-
public func Fix(weight: CGFloat) -> PutIntention {
140-
return Fix(nil, weight)
139+
public func Fix(value: CGFloat) -> PutIntention {
140+
return Fix(nil, value)
141141
}
142142

143143
public func Fix(view: UIView?) -> PutIntention {
@@ -352,6 +352,14 @@ private struct DirectLayoutOperation<T:DirectLayoutAction> : LayoutOperation
352352
}
353353
}
354354

355+
public func SetX(view: UIView?, value: CGFloat) -> LayoutOperation {
356+
return SetLeft(view, value: value)
357+
}
358+
359+
public func SetY(view: UIView?, value: CGFloat) -> LayoutOperation {
360+
return SetTop(view, value: value)
361+
}
362+
355363
public func SetWidth(view: UIView?, value: CGFloat) -> LayoutOperation {
356364
return DirectLayoutOperation<WidthDirectLayoutAction>(view: view, value: value)
357365
}
@@ -385,6 +393,15 @@ public func SetSize(view: UIView?, width: CGFloat, height: CGFloat) -> LayoutOpe
385393
])
386394
}
387395

396+
public func SetFrame(view: UIView?, x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) -> LayoutOperation {
397+
return Combine( [
398+
SetLeft(view, value: width),
399+
SetTop(view, value: width),
400+
SetWidth(view, value: width),
401+
SetHeight(view, value: height)
402+
])
403+
}
404+
388405
//MARK: size to fit
389406

390407
public enum SizeToFitIntention {
@@ -616,7 +633,7 @@ public func HFillVFit(view: UIView) -> LayoutOperation {
616633

617634
public protocol Anchor {
618635
func valueForRect(rect: CGRect) -> CGFloat
619-
func setValueForRect(value: CGFloat, rect: CGRect, inset: CGFloat) -> CGRect
636+
func setValueForRect(value: CGFloat, rect: CGRect) -> CGRect
620637

621638
var view: UIView? {get}
622639
}
@@ -625,86 +642,150 @@ public protocol Anchor {
625642

626643
public enum HAnchor : Anchor {
627644

628-
case Left(UIView?)
629-
case Right(UIView?)
645+
case Left(UIView?, CGFloat)
646+
case Center(UIView?, CGFloat)
647+
case Right(UIView?, CGFloat)
630648

631649
public func valueForRect(rect: CGRect) -> CGFloat {
632650
switch self {
633-
case .Left(_):
634-
return CGRectGetMinX(rect)
635-
case .Right(_):
636-
return CGRectGetMaxX(rect)
651+
case .Left(_, let inset):
652+
return CGRectGetMinX(rect) + inset
653+
case .Right(_, let inset):
654+
return CGRectGetMaxX(rect) + inset
655+
case .Center(_, let inset):
656+
return CGRectGetMidX(rect) + inset
637657
}
638658
}
639659

640-
public func setValueForRect(value: CGFloat, rect: CGRect, inset: CGFloat) -> CGRect {
660+
public func setValueForRect(value: CGFloat, rect: CGRect) -> CGRect {
641661
var result = rect
642662
switch self {
643-
case .Left(_):
644-
result.origin.x = value + inset
645-
case .Right(_):
663+
case .Left(_, let inset):
664+
result.origin.x = value - inset
665+
case .Right(_, let inset):
646666
result.origin.x = value - result.size.width - inset
667+
case .Center(_, let inset):
668+
result.origin.x = value - result.size.width/2 - inset
647669
}
648670

649671
return result
650672
}
651673

652674
public var view: UIView? {
653675
switch self {
654-
case .Left(let v):
676+
case .Left(let v, _):
677+
return v
678+
case .Right(let v, _):
655679
return v
656-
case .Right(let v):
680+
case .Center(let v, _):
657681
return v
658682
}
659683
}
660684
}
661685

686+
public func LeftAnchor(view: UIView?, inset: CGFloat) -> HAnchor {
687+
return HAnchor.Left(view, inset)
688+
}
689+
690+
public func LeftAnchor(view: UIView?) -> HAnchor {
691+
return LeftAnchor(view, inset: 0)
692+
}
693+
694+
public func RightAnchor(view: UIView?, inset: CGFloat) -> HAnchor {
695+
return HAnchor.Right(view, inset)
696+
}
697+
698+
public func RightAnchor(view: UIView?) -> HAnchor {
699+
return RightAnchor(view, inset: 0)
700+
}
701+
702+
703+
public func HCenterAnchor(view: UIView?, inset: CGFloat) -> HAnchor {
704+
return HAnchor.Center(view, inset)
705+
}
706+
707+
public func HCenterAnchor(view: UIView?) -> HAnchor {
708+
return HCenterAnchor(view, inset: 0)
709+
}
710+
711+
662712
//MARK: vanchor
663713

664714
public enum VAnchor : Anchor {
665-
case Top(UIView?)
666-
case Bottom(UIView?)
715+
case Top(UIView?, CGFloat)
716+
case Bottom(UIView?, CGFloat)
717+
case Center(UIView?, CGFloat)
667718

668719
public func valueForRect(rect: CGRect) -> CGFloat {
669720
switch self {
670-
case .Top(_):
671-
return CGRectGetMinY(rect)
672-
case .Bottom(_):
673-
return CGRectGetMaxY(rect)
721+
case .Top(_, let inset):
722+
return CGRectGetMinY(rect) + inset
723+
case .Bottom(_, let inset):
724+
return CGRectGetMaxY(rect) + inset
725+
case .Center(_, let inset):
726+
return CGRectGetMidY(rect) + inset
674727
}
675728
}
676729

677-
public func setValueForRect(value: CGFloat, rect: CGRect, inset: CGFloat) -> CGRect {
730+
public func setValueForRect(value: CGFloat, rect: CGRect) -> CGRect {
678731

679732
var result = rect
680733

681734
switch self {
682-
case .Top(_):
683-
result.origin.y = value + inset
684-
case .Bottom(_):
735+
case .Top(_, let inset):
736+
result.origin.y = value - inset
737+
case .Bottom(_, let inset):
685738
result.origin.y = value - result.size.height - inset
739+
case .Center(_, let inset):
740+
result.origin.y = value - result.size.height/2 - inset
686741
}
687742

688-
return rect
743+
return result
689744
}
690745

691746
public var view: UIView? {
692747
switch self {
693-
case .Top(let v):
748+
case .Top(let v, _):
694749
return v
695-
case .Bottom(let v):
750+
case .Bottom(let v, _):
751+
return v
752+
case .Center(let v, _):
696753
return v
697754
}
698755
}
699756
}
700757

758+
public func TopAnchor(view: UIView?, inset: CGFloat) -> VAnchor {
759+
return VAnchor.Top(view, inset)
760+
}
761+
762+
public func TopAnchor(view: UIView?) -> VAnchor {
763+
return TopAnchor(view, inset: 0)
764+
}
765+
766+
public func BottomAnchor(view: UIView?, inset: CGFloat) -> VAnchor {
767+
return VAnchor.Bottom(view, inset)
768+
}
769+
770+
public func BottomAnchor(view: UIView?) -> VAnchor {
771+
return BottomAnchor(view, inset: 0)
772+
}
773+
774+
775+
public func VCenterAnchor(view: UIView?, inset: CGFloat) -> VAnchor {
776+
return VAnchor.Center(view, inset)
777+
}
778+
779+
public func VCenterAnchor(view: UIView?) -> VAnchor {
780+
return VCenterAnchor(view, inset: 0)
781+
}
782+
701783
//MARK: follow anchor
702784

703785
private struct FollowOperation<T: Anchor> : LayoutOperation {
704786

705787
let anchorToFollow: T
706788
let followerAnchor: T
707-
let inset: CGFloat
708789

709790
func calculateLayouts(inout layouts: [UIView : CGRect]) {
710791

@@ -716,23 +797,20 @@ private struct FollowOperation<T: Anchor> : LayoutOperation {
716797

717798
let anchorToFollowFrame = frameForView(toFollowView, layouts: &layouts)
718799
let followerAnchorFrame = frameForView(followerView, layouts: &layouts)
719-
720-
layouts[followerView] = followerAnchor.setValueForRect(anchorToFollow.valueForRect(anchorToFollowFrame), rect: followerAnchorFrame, inset: inset)
800+
801+
layouts[followerView] = followerAnchor.setValueForRect(anchorToFollow.valueForRect(anchorToFollowFrame), rect: followerAnchorFrame)
721802
}
722803

723-
init(anchorToFollow: T, followerAnchor: T, inset: CGFloat) {
804+
init(anchorToFollow: T, followerAnchor: T) {
724805
self.anchorToFollow = anchorToFollow
725806
self.followerAnchor = followerAnchor
726-
self.inset = inset
727807
}
728808

729809
}
730810

731-
public func Follow<T: Anchor>(anchor: T, withAnchor: T, inset: CGFloat) -> LayoutOperation {
732-
return FollowOperation(anchorToFollow: anchor, followerAnchor: withAnchor, inset: inset)
733-
}
811+
// anchor.value + inset = withAnchor.value + inset
734812

735813
public func Follow<T: Anchor>(anchor: T, withAnchor: T) -> LayoutOperation {
736-
return Follow(anchor, withAnchor: withAnchor, inset: 0)
814+
return FollowOperation(anchorToFollow: anchor, followerAnchor: withAnchor)
737815
}
738816

0 commit comments

Comments
 (0)