IOS UIView Animations

Source: Internet
Author: User

There are a lot of animation frameworks available for iOS now, including Apple's coreanimation Framework, Facebook POPs, and so on, which are really a tool for programmers. But what if we just want to make some relatively simple animations? How to kill a chicken with sledgehammer. We can use the UIView directly. Today we're going to have a good chat. UIView animation, written using Swift (you can see I sometimes use OC, sometimes with Swift, now the basic skills of iOS learning to see the OC code can write swift, according to Swift can write OC, haha). This sample code is uploaded to Https://github.com/chenyufeng1991/iOS-UIView-Animation.

The closure closure in Swift is often used in calls to this method, similar to block blocks in OC. Let me briefly explain that the closure is a parameter of a function, but in this parameter you can execute some statements and execute some logic. You can understand closures as an anonymous function.

()->void: Indicates that the parameter is NULL, the return value is void, the closure function must be implemented, ((Bool), void)?: Indicates that the parameter is of type Bool, the return value is void, followed by? Indicates that the closure function can be empty;


(1) Position animation

Positionanimation can realize the movement of the view, the simplest is the x axis, the y axis of movement. Here are some small squares to move.

Import Uikitclass Positionviewcontroller:uiviewcontroller {@IBOutlet weak var greensquare:uiview!  @IBOutlet weak var redsquare:uiview!    @IBOutlet weak var bluesquare:uiview! Override Func Viewdidload () {super.viewdidload ()} override func Viewdidappear (Animated:bool) {// The definition of the closure function;//Note that the animations,completion used in the method that invokes the animation is the closure function, which can be defined directly outside, which is called in, so that the code is clearer;//The closure needs to pass in the parameter is a bool value; The return value is null;/this is Callback after the animation execution is completed; Func completegreen (V:bool) {print ("Green completion")} func completered (v:bool) {print        ("Red completion")} Func Completeblue (v:bool) {print ("Blue completion")}//The closure parameter here is null; func Animgreen () {Self.greensqua re.center.x = self.view.bounds.width-self.greensquare.center.x} func animred () {SELF.REDSQUARE.CENTER.Y = Self.view.bounds.height-self.redsquare.center.y} func animblue () {self.bluesquare.center.y = Self.view . Bounds.height-self.bluesquare.center.y SElf.bluesquare.center.x = self.view.bounds.width-self.bluesquare.center.x}//The parameter delay indicates the delay, the first parameter indicates the animation time; /Note call closure function; uiview.animatewithduration (1, delay:0, Options: [], Animations:animgreen, Completion:completegreen) UI View.animatewithduration (1, delay:0.5, Options: [], animations:animred, completion:completered) uiview.animatewithdu Ration (1, delay:1, Options: [], Animations:animblue, Completion:completeblue)}}

(2) Transparency animation

Transparency animations can make a view's transparency change between 0~1. A transparency of 1 means full transparency and is invisible. A transparency of 1 is indicated in the normal case.

Import Uikitclass Opacityviewcontroller:uiviewcontroller {    @IBOutlet weak var greensquare:uiview!      Override Func Viewdidload () {        super.viewdidload ()    }      override func Viewdidappear (animated:bool) {        Super.viewdidappear (animated)              func Anim () {            Self.greenSquare.alpha = 0.2   //change transparency to 0.2        }        Uiview.animatewithduration (2, Animations:anim)//often 2s;    }  }

(3) Zoom animation

Scaling animations can change the size of a view and zoom out proportionally.

Import Uikitclass Scaleviewcontroller:uiviewcontroller {        @IBOutlet weak var greensquare:uiview!        Override Func Viewdidload () {        super.viewdidload ()    }        override func Viewdidappear (animated:bool) {        Super.viewdidappear (animated)                func Anim () {            self.greenSquare.transform = Cgaffinetransformmakescale (0.5, 0.5)//reduced to 0.5 times times the original;        }        uiview.animatewithduration (1, Animations:anim)}    }

(4) Color animation

Color animation allows a view to have a color gradient within a single time interval, making a color transition.

Import Uikitclass Colorviewcontroller:uiviewcontroller {    @IBOutlet weak var greensquare:uiview!    @IBOutlet weak var swifttext:uilabel!      Override Func Viewdidload () {        super.viewdidload ()    }    override func Viewdidappear (animated:bool) {        Super.viewdidappear (animated)                func Anim () {            self.greenSquare.backgroundColor = Uicolor.blackcolor ()            Self.swiftText.textColor = Uicolor.bluecolor ()        }        uiview.animatewithduration (2, Animations:anim)    }  }

(5) Rotate animation

Lets you rotate a view around a dot.

Import Uikitclass Rotationviewcontroller:uiviewcontroller {    @IBOutlet weak var wheel:uiimageview!      Override Func Viewdidload () {        super.viewdidload ()    }        func spin () {        uiview.animatewithduration (1), delay:0, Options:. Curvelinear, animations: {            self.wheel.transform = cgaffinetransformrotate (Self.wheel.transform, CGFloat (360))// The second argument is an angle;            }) {                //end still calls the spin () function,                (finished), Void in                self.spin ()        }            }//spin ()        override func Viewdidappear (animated:bool) {        super.viewdidappear (animated)                Self.spin ()}    }

(6) Repeat animation

The animation allows an animation process to execute repeatedly.

Import uikit//repeat animation; class Repeatviewcontroller:uiviewcontroller {@IBOutlet weak var bluesquare:uiview!  @IBOutlet weak var redsquare:uiview!    @IBOutlet weak var greensquare:uiview! Override Func Viewdidload () {super.viewdidload ()} override func Viewdidappear (animated:bool) {Super.view        Didappear (animated); Uiview.animatewithduration (1) {(), Void in self.bluesquare.center.x = Self.view.bounds.width-self.bluesquare. Center.x} uiview.animatewithduration (1, delay:0, options:UIViewAnimationOptions.Repeat, animations: {()-&gt ;      Void in self.redsquare.center.x = self.view.bounds.width-self.redsquare.center.x; }, Completion:nil)//Note the options here expression; Uiview.animatewithduration (1, delay:0, Options:[uiviewanimationoptio Ns. Repeat, Uiviewanimationoptions.autoreverse], animations: {(), Void in self.greensquare.center.x = Self.view.bou      nds.width-self.greensquare.center.x;  }, Completion:nil)}  } 

(7) Buffer animation

The effect of the beisaier curve is mainly used to change the rate effect of the view animation. Everyone can practice.

Import Uikitclass Easingviewcontroller:uiviewcontroller {@IBOutlet weak var bluesquare:uiview!  @IBOutlet weak var redsquare:uiview!    @IBOutlet weak var greensquare:uiview!      @IBOutlet weak var yellowsquare:uiview! Override Func Viewdidload () {super.viewdidload ()} override func Viewdidappear (animated:bool) {Super.view        Didappear (animated); Uiview.animatewithduration (1) {(), Void in self.bluesquare.center.x = Self.view.bounds.width-self.blues Quare.center.x} uiview.animatewithduration (1, delay:0, options:UIViewAnimationOptions.CurveEaseIn, animations : {(), Void in self.redsquare.center.x = self.view.bounds.width-self.redsquare.center.x}, completion:n IL) uiview.animatewithduration (1, delay:0, Options:UIViewAnimationOptions.CurveEaseOut, animations: {()-Vo        ID in self.greensquare.center.x = self.view.bounds.width-self.greensquare.center.x}, Completion:nil) Uiview.aNimatewithduration (1, delay:0, Options:UIViewAnimationOptions.CurveEaseInOut, animations: {(), Void in Self.y ellowsquare.center.x = self.view.bounds.width-self.yellowsquare.center.x}, Completion:nil)}}

(8) Spring animation

The animation performs a similar spring-like effect, and you can set the spring's damping and initial speed to achieve very realistic spring jitter.

Import Uikitclass Springviewcontroller:uiviewcontroller {@IBOutlet weak var bluesquare:uiview!  @IBOutlet weak var redsquare:uiview!    @IBOutlet weak var greensquare:uiview! Override Func Viewdidload () {super.viewdidload ()} override func Viewdidappear (animated:bool) {Uivi Ew.animatewithduration (1) {(), Void in self.bluesquare.center.x = Self.view.bounds.width-self.bluesquare.cent    er.x; }//The third parameter is damping, the fourth parameter is the initial velocity, and the fifth argument is the animation type; uiview.animatewithduration (1, delay:0, usingspringwithdamping:0.2, initial springvelocity:0, Options:UIViewAnimationOptions.TransitionNone, animations: {(), Void in Self.redSquare.cent      er.x = self.view.bounds.width-self.redsquare.center.x; }) {(Bool), Void in} uiview.animatewithduration (1, delay:0, usingspringwithdamping:0.2, Initialspringve  Locity:1, Options:UIViewAnimationOptions.TransitionNone, animations: {(), Void in self.greensquare.center.x = Self.view.bounds.width-self.greensquare.center.x; }) {(Bool), Void in}}}

The animations implemented here are very simple, and you can try it yourself by downloading the code. Later on I will give you more advanced cool animation. Please look forward to it.



GitHub home: https://github.com/chenyufeng1991. Welcome everyone to visit!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS UIView Animations

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.