IOS Core Animation specifically explains (iv) animations in AutoLayout

Source: Internet
Author: User

Original blog. Reprint please indicate the source
Blog.csdn.net/hello_hwc
Welcome to my iOS SDK specific explanations column
Http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html

Preface: AutoLayout defines the location of the view, that is, in project in auto layout, assuming that the constraint itself is not changed, when the view is drawn again. It will go back to where it started. The animations in AutoLayout are related to the position and size of the view.

First look at the effect

Implementation process

Drag a uiimageview on the storyboard. Set constraint to: Horizontal vertical Center, the size is very fixed 100*100

Drag imageview and constraint for outlet

Notice the drag of the Y-related constraint, which is the


corresponding Code

weakvarweakvar yConstraints: NSLayoutConstraint!

Set the initial state of ImageView in Viewdidload

 yConstraints.constant = yConstraints.constant - CGRectGetHeight(UIScreen.mainScreen().bounds)/2 self.imageview.alpha0.0; self.imageview.transform = CGAffineTransformMakeScale(0.10.1) self.view.layoutIfNeeded();

Creating animations in Viewwillappear

 yConstraints.constant = yConstraints.constant + CGRectGetHeight(UIScreen.mainScreen().bounds)/2 UIView.animateWithDuration(1.0in        self.imageview.alpha1.0        self.imageview.transform = CGAffineTransformIdentity        self.view.layoutIfNeeded() })
Principle

The principle is relatively simple. is to use the attribute constant in the change constraint nslayoutconstraint. It is then called layoutIfNeeded to implement the animation. Attention. Attributes multiplier (IOS 8.4) are still read-only and cannot be changed. But it is possible to convert through relationships view1.property = view2.property * multiplier + constant .

AutoLayout Animations with pure code

The above animations are implemented with pure code

Class Viewcontroller:uiviewcontroller {var imageview:uiimageview?    Weak var yconstraint:nslayoutconstraint? Override Func Viewdidload () {Super. Viewdidload()//join ImageView Let image = UIImage (named:"1_hello_hwc.jpg") ImageView = Uiimageview (image:image) Self. ImageView?

. Settranslatesautoresizingmaskintoconstraints(false) Self. View. Addsubview(Self. ImageView!) Create a constraint that defines the start of the position let HC = Nslayoutconstraint (item:self. View, Attribute:nslayoutattribute. CenterX, relatedby:nslayoutrelation. Equal, toitem:self. ImageView, Attribute:nslayoutattribute. CenterX, multiplier:1.0, Constant:0.0) Let VC = Nslayoutconstraint (item:self. View, Attribute:nslayoutattribute. CenterY, relatedby:nslayoutrelation. Equal, toitem:self. ImageView, Attribute:nslayoutattribute. CenterY, multiplier:1.0, Constant:0.0) Yconstraint = VC;yconstraint!. Constant= yconstraint!. Constant-Cgrectgetheight (UIScreen. Mainscreen(). Bounds)/2Let widthc = Nslayoutconstraint (item:self. ImageView!, Attribute:nslayoutattribute. Width, relatedby:nslayoutrelation. Equal, Toitem:nil, Attribute:nslayoutattribute. Notanattribute, multiplier:1.0, Constant: -) Let Widthh = Nslayoutconstraint (item:self. ImageView!, Attribute:nslayoutattribute. Height, relatedby:nslayoutrelation. Equal, Toitem:nil, Attribute:nslayoutattribute. Notanattribute, multiplier:1.0, Constant: -) Self. View. Addconstraints([HC,VC,WIDTHC,WIDTHH])//define the start state self. ImageView?

. Alpha = 0.0; Self. ImageView?

. Transform= Cgaffinetransformmakescale (0.1,0.1);Self. View. layoutifneeded()} override func Viewwillappear (animated:bool) {yconstraint!. Constant= yconstraint!. Constant+ Cgrectgetheight (uiscreen. Mainscreen(). Bounds)/2UIView. Animatewithduration(1.0, animations: {(), VoidinchSelf. ImageView!. Alpha=1.0Self. ImageView!. Transform= Cgaffinetransformidentity Self. View. layoutifneeded() }) }}

Positioning constraints

Set Property identifier

yConstraint.identifier"identifier"

And then in the filter. Defined to this constraints,

let constraint =  filter(self.view.constraints() as! [NSLayoutConstraint], { (constraint:NSLayoutConstraint) -> Bool in            return constraint.identifier == "identifier"        }).first

IOS Core Animation specifically explains (iv) animations in AutoLayout

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.