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:
- Contains a property that randomly returns one of the 8 transition options
- 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