Swift-Automatic Layout library Snapkit use 3 (constraint precedence, constrain animation)

Source: Internet
Author: User

1, Constraint priority
When we use Snapkit, we can also define the precedence of the constraints. In this way, when the constraint conflicts, the precedence constraint overrides the low-priority constraint. The specific priority can be placed at the end of the constraint chain.

(1) The following priority levels can be set

Prioritylow (): Set low priority, priority 250
Prioritymedium (): Set priority, priority is 500 (this is also the default priority)
Priorityhigh (): Set high priority, priority 750
Priority (): You can set any precedence, and the accepted parameter is a number of 0-1000. For example: Priority (600)

(2) Examples of using precedence

Below we place a 100*100 Orange square in the center of the screen, giving it a default precedence constraint that has a length-width size less than or equal to the size of the screen. At the same time, each time you click on the screen, it will be updated to enlarge its size. However, since the precedence of this constraint is low, all blocks will not zoom in when they reach the edge of the screen.

Import UIKit
Import Snapkit

Class Viewcontroller:uiviewcontroller {

lazy var box = UIView ()

var scacle = 1.0

Override Func Viewdidload () {
Super.viewdidload ()

Click Listen
Let Tapsingle=uitapgesturerecognizer (target:self,action: #selector (Tapsingledid))
Tapsingle.numberoftapsrequired=1
Tapsingle.numberoftouchesrequired=1
Self.view.addGestureRecognizer (Tapsingle)

Box.backgroundcolor = Uicolor.orangecolor ()
Self.view.addSubview (Box)

box.snp_makeconstraints {(make), Void in
Center View
Make.center.equalTo (Self.view)
Initial width, height is 100 (priority low)
Make.width.height.equalTo (self.scacle). Prioritylow ();
Maximum size cannot exceed screen
Make.width.height.lessThanOrEqualTo (Self.view.snp_width)
Make.width.height.lessThanOrEqualTo (Self.view.snp_height)
}
}

Tap the screen
Func Tapsingledid () {
Self.scacle + = 0.5
self.box.snp_updateconstraints{(make), Void in
Zoomed-in view (lowest priority)
Make.width.height.equalTo (self.scacle). Prioritylow ();
}
}
}

2, with animation effect
With Uiview.animatewithduration, we can animate when the constraint changes.
Or the example above shows that there is not enough time to click on the screen when the Orange view zooms in when there is a transition, not a bit larger.

Import UIKit
Import Snapkit

Class Viewcontroller:uiviewcontroller {

lazy var box = UIView ()

var scacle = 1.0

Override Func Viewdidload () {
Super.viewdidload ()

Click Listen
Let Tapsingle=uitapgesturerecognizer (target:self,action: #selector (Tapsingledid))
Tapsingle.numberoftapsrequired=1
Tapsingle.numberoftouchesrequired=1
Self.view.addGestureRecognizer (Tapsingle)

Box.backgroundcolor = Uicolor.orangecolor ()
Self.view.addSubview (Box)

box.snp_makeconstraints {(make), Void in
Center View
Make.center.equalTo (Self.view)
Initial width, height is 100 (priority low)
Make.width.height.equalTo (self.scacle). Prioritylow ();
Maximum size cannot exceed screen
Make.width.height.lessThanOrEqualTo (Self.view.snp_width)
Make.width.height.lessThanOrEqualTo (Self.view.snp_height)
}
}

View Constraint update
Override Func Updateviewconstraints () {
self.box.snp_updateconstraints{(make), Void in
Enlarged size (Low priority)
Make.width.height.equalTo (self.scacle). Prioritylow ();
}

Super.updateviewconstraints ()
}

Tap the screen
Func Tapsingledid () {
Self.scacle + = 0.5
Tell Self.view the constraint needs to be updated
Self.view.setNeedsUpdateConstraints ()
Animation
Uiview.animatewithduration (0.3) {
Self.view.layoutIfNeeded ()
}
}
}

Swift-Automatic Layout library Snapkit use 3 (constraint precedence, constrain animation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.