Micro-extension of Uikit view animations

Source: Internet
Author: User
Tags uikit

Panda Pig • Patty original or translated works. Welcome reprint, Reprint please indicate the source.
If you feel that the writing is not good please more advice, if you feel good please support a lot of praise. Thank you! Hopy;)

Haven't written blog for a long time, also hurry to write ...;]

We know that there are several ways to animate in Uikit, one is animation, and the other is transition, which produces animations based on the properties of the object, while the latter is done through the operation of the view.

We can look at the Uiviewanimationoptions class, where there are 8 options associated with transition:

 Public Static vartransitionnone:uiviewanimationoptions {Get}//Default     Public Static vartransitionflipfromleft:uiviewanimationoptions {Get} Public Static vartransitionflipfromright:uiviewanimationoptions {Get} Public Static vartransitioncurlup:uiviewanimationoptions {Get} Public Static vartransitioncurldown:uiviewanimationoptions {Get} Public Static vartransitioncrossdissolve:uiviewanimationoptions {Get} Public Static vartransitionflipfromtop:uiviewanimationoptions {Get} Public Static vartransitionflipfrombottom:uiviewanimationoptions {Get}

Below we uiviewanimationoptions the structure to write an extension, this extension does 2 things:

    1. Contains a property that randomly returns one of the 8 transition options
    2. Returns the name that corresponds to the transition option
ImportFoundationImportUIKitImportGameplaykit@available(IOS9.0, *) Private Let Randomgen: Gkshuffleddistribution = Gkshuffleddistribution (Fordiewithsidecount: UIViewAnimationOptions.allDirectionOptions.count)@available(IOS4.0, *) extension uiviewanimationoptions{static Let alldirectionoptions: [Uiviewanimationoptions] = [. Transitionnone,. Transitionflipfromleft,. Transitionflipfromright,. Transitioncurlup,. Transitioncurldown,. Transitioncrossdissolve,. Transitionflipfromtop,. Transitionflipfrombottom]var Optionname: string{SwitchSelf.rawvalue { CaseUiviewanimationoptions.transitionnone.RawValue:return "Transitionnone"         CaseUiviewanimationoptions.transitionflipfrombottom.RawValue:return "Transitionflipfrombottom"         CaseUiviewanimationoptions.transitionflipfromtop.RawValue:return "Transitionflipfromtop"         CaseUiviewanimationoptions.transitioncrossdissolve.RawValue:return "Transitioncrossdissolve"         CaseUiviewanimationoptions.transitioncurldown.RawValue:return "Transitioncurldown"         CaseUiviewanimationoptions.transitioncurlup.RawValue:return "Transitioncurlup"         CaseUiviewanimationoptions.transitionflipfromright.RawValue:return "Transitionflipfromright"         CaseUiviewanimationoptions.transitionflipfromleft.RawValue:return "Transitionflipfromleft"        default:return "Nonename"}    }    /*@available(IOS9.0, *) Private lazyvar Randomgen: Gkshuffleddistribution = {returnGkshuffleddistribution (Fordiewithsidecount: Alldirectionoptions.count)} () */staticvar randomdirectionoption: uiviewanimationoptions {if #available (IOS 9.0, *) {             LetIDX = Randomgen.nextint ()-1            Print("Gen is \ (Randomgen), IDX is \ (idx)")returnALLDIRECTIONOPTIONS[IDX]}Else{return. Transitionnone}}}

First we create a random number generator in the file scope, we do not use the general method, but use a new method after the iOS9, the random number generator, as the name of the return of the random number will not be repeated, unless the selection of all optional random number. Why not put it in the structure extension? Because I don't want each read the Randomdirectionoption property to generate a new random number generator.

Let's test the above extensions in a practical example:

Let option = Uiviewanimationoptions. RandomdirectionoptionUIView. Transitionwithview(Status, Duration:0.33, Options: [. Curveeaseout, option], animations: {self. Status. Hidden= False Self. Label. Text= option. OptionnameSelf. Transitionname. Text= option. OptionnameSelf. Transitionname. SizeToFit() Self. Transitionname. Center= Cgpoint (x: Self. Statusposition. x,y: Self. Transitionname. Center. Y)}, Completion: {_inchDelay (seconds:2.0) {If Index < self. Messages. Count-1{Self. Removemessage(Index:index)} else{//reset Form}})

To compile the link app, the effect is as follows:

Micro-extension of Uikit view 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.