Automatic layout, third party framework, -pureLayout learning

  • The Attributes
    Automatic layout, third party framework, -pureLayout learning
    PureLayout-CommonAttributes.png is the five parameter often used in pureLayout (There are 5 specific attribute types, which are used throughout most of the API

    ) 1.ALEdge 3.ALAxis
    4.ALMargin available in iOS and higher only
    5.ALMarginAxis available 8 in iOS 8 and higher only all of the above parameters are a member of the ALAttribute enumeration. Take ALEdge
    ALAttribute-> ALEdgeLeft
    and ALEdge-&gt ALEdgeLeft;
    ALEdgeLeft = NSLayoutAttributeLeft Additionally can know according to the framework of the source code, there is one generic attribute type, ALAttribute
    , which is effectively a union of all the specific types. You can think of this as the “supertype” of all of the specific attribute types which, means that it is always safe to cast a specific type to the generic ALAttribute
    type. (Note that the reverse is not true a generic ALAttribute to a casting specific attribute type is unsafe!)

  • Object method for using method UIView
AutoSetContent (CompressionResistance|Hugging) PriorityForAxis: - autoCenterInSuperview (Margins) Margins variant iOS 8.0+ / only - autoAlignAxisToSuperview (Margin) Axis: Margin variant iOS 8.0+ / only - autoPinEdgeToSuperview (Edge:|Margin:) (withInset:) Margin variant iOS 8.0+ / only - autoPinEdgesToSuperview (Edges|Margins) (WithInsets:) (excludingEdge:) / Margins variant iOS 8.0+ only - autoPinEdge:toEdge:ofView: (withOffset:) - autoAlignAxis:toSameAxisOfView: (withOffset:|withMultiplier:) - autoMatchDimension:toDimension:ofView: (withOffset:|withMultiplier:) - autoSetDimension (s) ToSize: - autoConstrainAttribute:toAttribute:ofView: (withOffset:|withMultiplier:) - autoPinTo (Top|Bottom) LayoutGuideOfViewController:withInset: / iOS only
  • Demo
    view is located in the blue parent view center, and 50pt// Blue view is centered is on screen, with size {50 Pt pt} [self.blueView autoCenterInSuperview] [self.blueView, 50; autoSetDimensionsToSize:CGSizeMake (50, 50)]; red View and blue view at the top of the bottom, the left and the right position of the same blue
    width and blue view, Red view is positioned height 40pt// at the bottom right corner of the blue view with the, same width, and a height of 40 Pt [self.redView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.blueView]; [self.redView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.blueView] [self.redView autoMatchDimension:ALDimensionWidth toDimension: ALDimensionWidt; H ofView:self.blueView]; [self.redView autoSetDimension:ALDimensionHeight toSize:40.0]; the top yellow view and red view +10pt at the bottom of the same location, height of 25pt, from the parent control were 25pt// Yellow view is positioned 10 Pt below the red view, extending across the screen with 20 Pt insets from the edges, and with a fixed height / of 25 Pt [self.yellowView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.redView withOffset:10.0]; [self.yellowView autoSetDimension:ALDimensionHeight toSize:25.0]; [self.yellowView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20.0]; [self.yellowView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:20.0]; view top with yellow green space at the bottom of the view 10, and the father of view vertically, the height is yellow Two times the height of view, width of 150// Green view is positioned 10 Pt below the Yellow view, aligned to the vertical axis of its superview its height twice the / / with height of the Yellow view and its width fixed to Pt [self.greenView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.yellowView 150 withOffset:10.0]; [self.greenView autoAlignAxisToSuperviewAxis:ALAxisVertical]; [self.greenView autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.yellowView withMultiplier:2.0]; [self.greenView autoSetDimension:ALDimensionWidth toSize:150.0]; effect:
Automatic layout, third party framework, -pureLayout learning
screen snapshot 2016-04-14 PM 6.01.20.png

-2. is object of some UIView, this is demo method of object array, the array has two kinds, one is the UIView array, a constraint array, first look at an array of UIView

Of Views / / Arrays - autoAlignViewsToEdge: - autoAlignViewsToAxis: - autoMatchViewsDimension: - autoSetViewsDimension:toSize: - autoSetViewsDimensionsToSize: - autoDistributeViewsAlongAxis:alignedTo:withFixedSpacing: (insetSpacing:) (matchedSizes:) - autoDistributeViewsAlongAxis:alignedTo:withFixedSize: (insetSpacing:)

This is a demo used

Once height is red view and yellow view is 50, the height of 70 Blue and green, the author wrote the following comments on the method statement (Note: in the array must have at least one view method, the return value is an added constraint array)

Sets all of the views / * * in this array to a given size. Note: This array must contain at least @param size The size 1 view. to set each view's dimensions to. @return An array of constraints added..
A fixed height / / Apply of 50 PT to two views at once, and a fixed height of to another two views 70 Pt [@[self.redView, self.yellowView] autoSetViewsDimension:ALDimensionHeight toSize:50.0] self.greenView] autoSetViewsDimension:ALDimensionHeight toSize:70.0]; [@[self.blueView, NSArray; *views = @[self.redView, self.blueView, self.yellowView, self.greenView];

Create an array that contains four view, so that the width of the view in the array is consistent, and that the top of the red view is 20 to the controller’s view navigation bar

NSArray *views = @[self.redView, self.blueView, self.yellowView, self.greenView]; Match the widths of all the / views [views / / Pin the red view autoMatchViewsDimension:ALDimensionWidth]; from the top layout 20 Pt guide of the view controller [self.redView autoPinToTopLayoutGuideOfViewController:self withInset:20.0];

The first view array to the left of the parent view adhesion to the left, each view traversal in the array sequentially adhesive on the left side of the view, after the completion of traversal, the last view in the array on the right side of the adhesive parent view right, because before setting off an array of every view wide, so complete the effect

Over the / / Loop views attaching the left edge to the previous view's right edge the top edge to / / and the previous view's bottom edge [[views firstObject] autoPinEdgeToSuperviewEdge:ALEdgeLeft] UIView; *previousView = nil; for (UIView *view in views if (previousView)) {{[view autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:previousView] [view autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:previousView]; previousView = view;} [[views lastObject] autoPinEdgeToSuperviewEdge:ALEdgeRight];}; self.didSetupConstraints = YES;}


Automatic layout, third party framework, -pureLayout learning
screen snapshot 2016-04-14 PM 5.59.56.png

-3. uses pureLayout to achieve animation effects

When view appears, the animation function animateLayoutWithRegularAnimation is started and a property is used to record the animation execution status self.isAnimatingToEndState = YES;

- (void) viewDidAppear: (BOOL animated) {[super viewDidAppear:animated]; Start the animation when the / * * view appears. Note that the first initial constraint setup and layout pass has already occurred at this point. To switch between spring animation and regular animation comment & uncomment the two lines; below (Don't uncomment both lines! One at a time!) * / self.isAnimatingToEndState = YES; [self animateLayoutWithSpringAnimation]; uncomment to use spring animation / / [self / / animateLayoutWithRegularAnimation] / / uncomment to use regular animation;}

By setting the constraint attribute on the animation status attribute, this place needs to be aware of the constraint of the constant attribute. As for the reason, the author makes it clear that it is convenient to update the existing constraints without removing the existing constraints and adding new constraints.

- (void) updateViewConstraints static const CGFloat kBlueViewInitialHeight {static const CGFloat = 40; kBlueViewEndHeight = 100; / / Remember, this code is just the initial constraint setup which only happens the first time this method is called if [self.blueView autoPinToTopLayoutGuideOfViewController:self (self.didSetupConstraints!) {withInset:20.0]; [self.blueView autoAlignAxisToSuperviewAxis:ALAxisVertical]; [self.blueView autoSetDimension:ALDimensionWidth toSize:50.0]; self.blueViewHeightConstraint = [self.blueView autoSetDimension:ALDimensionHeight toSize:kBlueViewInitialHeight]; [self.redView autoSetDimension:ALDimensionHeight toSize:50.0]; [self.redView autoMatc HDimension:ALDimensionWidth toDimension:ALDimensionHeight ofView:self.blueView withMultiplier:1.5]; [self.redView autoAlignAxisToSuperviewAxis:ALAxisVertical]; self.didSetupConstraints = YES;} / / Unlike the code above, this is code that will execute every time this method is called. the `constant` property of / / Updating a constraint is very efficient and can be done without removing/recreating the constraint. other changes will require / / Any you to remove and re-add new constraints. Make sure to remove constraints before you create new ones! [self.redViewEdgeConstraint autoRemove]; if (self.isAnimatingToEndState) constraints to be {/ / Adjust in the end state for the animation se Lf.blueViewHeightConstraint.constant = kBlueViewEndHeight; self.redViewEdgeConstraint = [self.redView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:150.0];} else {/ / Adjust constraints to be in the initial state for the animation self.blueViewHeightConstraint.constant = kBlueViewInitialHeight; self.redViewEdgeConstraint = [self.redView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.blueView withOffset:20.0] [super updateViewConstraints];}};

The animation function, it should be noted that in the animation blockUIView animationWithDuration… The completion, change the animation execution state and once again call the function, but also on the formation of the repeat function.

See the comments in viewDidAppear: above. / * * * / - (void) animateLayoutWithSpringAnimation These 2 lines will {/ / cause -[updateViewConstraints] to be called again on this view controller, where the constraints will be adjusted to the new state self.view setNeedsUpdateConstraints] [self.view [[UIView animateWithDuration:1.0; updateConstraintsIfNeeded]; delay:0.0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:0 animations:^{[self.view layoutIfNeeded] this is what actually causes; / / the views to animate to their new layout (BOOL finished) {completion:^} Run the animation again in the / other direction self.isAnimatingToEndState = self.isAnimatingToEndState; if! (self.navigationController) will be nil {/ / this if this view controller is no longer in the navigation stack (stops animation when this view controller is no longer onscreen [self animateLayoutWithSpringAnimation]}]);};}

Finally, the effect diagram:

Automatic layout, third party framework, -pureLayout learning