Use a very simple scenario as an example: on storyboard, you have a UINavigationController
string of two UIViewController
. These two controllers should jump to each other, a->b, B->a. Jump when the default push to push the effect you feel very silly x, so want to change an effect. For example, less than two of the fade in/out effect.
Many examples will say write a Cusom UIStoryboardSegue
, and then write one in this UIView.animationWithDuration
to achieve this effect. don't do this! starting with iOS7, there is a more convenient and concise way to achieve this effect.
Let's start by introducing this very bull X method. First create a single view of the project. The name can be called transitionDemo
. The relationship between the controllers is this:
One UINavigationController
as the start controller, string up one UITableViewController
(Root controller) and one UIViewController
. On Prototype Cells , Ctrl+drag to the View controller and select show.
The following are related to the table View Controller creation class TDTableViewController
for the view controller after the class is created, TDViewController
respectively, in storyboard. Set the Identitifer of Segue from table View Controller to view controller to TDViewController
.
Segue
Next TDTableViewController
, add the data source:
Class Tdtableviewcontroller:uitableviewcontroller {var tableviewdatasource: [String]? Override Func Viewdidload () { super.viewdidload () CreateDataSource () } func CreateDataSource () { if Let _ = self.tableviewdatasource { return } self.tableviewdatasource = [String] () for I in 0..<100 { self.tableviewdatasource!. Append ("Item:-[\ (i)]") } } //...}
Open Storyboard, TDViewController
add a label in, add a constraint to the label, any constraint can be as long as it is correct. Then add the label's outlet to the controller and associate it.
TDViewController
Add the data attribute to the code and viewDidLoad
assign the text of the label in the method:
Class Tdviewcontroller:uiviewcontroller { @IBOutlet weak var datalabel:uilabel! var singledata:string! Override Func Viewdidload () { super.viewdidload () self.dataLabel.text = Self.singledata }}
Using the default segue, here is now the show mode, jump. and pass data from the table view Controller to the View controller:
Use segue jump override func TableView (Tableview:uitableview, Didselectrowatindexpath Indexpath:nsindexpath) { Self.dataitem = self.tableviewdatasource! [Indexpath.row] Self.performseguewithidentifier ("Tdviewcontroller", Sender:nil) }//passing Data override Func Prepareforsegue ( Segue:uistoryboardsegue, Sender:anyobject?) { if Segue.identifier! = "Tdviewcontroller" { return } let Destinationcontroller = Segue.destinationviewcontroller as! Tdviewcontroller destinationcontroller.singledata = self.dataitem! }
run a hand to see:
Segue and Transitioncustom Segue
Directly in the code to implement the two methods, respectively, instead of the default implementation method used above.
First get a custom segue. To develop a custom segue, you first need to create a class and inherit from it UIStoryboardSegue
, and we name the class DetailStoryboardSegue
. The key to this class is the implementation method perform()
.
import uikitclass detailstoryboardsegue:uistoryboardsegue {override Func Perform () {Let Sourceview = self.sourceViewController.view//1 Let Destview = Self.destinationviewcontroll Er.view let window = (uiapplication.sharedapplication (). Delegate as! appdelegate). window window?. Insertsubview (Destview, Abovesubview:sourceview)//2 Destview.alpha = 0.0 uiview.animatewithduration (0.3, Animations: {//3 Destview.alpha = 1.0}}}}
self.sourceViewController
And self.destinationViewController
are all UIStoryboardSegue
attributes of the class itself. Jump from a controller to B controller, then source is a,destination B. Our fade in/out effect is achieved through the switching and Alpha of the source and destination controller's view.
- Make a switch between source and destination view on window. Overlay the destination view on the source view.
- The Alpha of Destination view is set to 0 in the previous step, which is completely transparent. In the animation, the destination view's alpha is set back to completely opaque, and the view is rendered in front of the user to achieve fade in.
When the implementation is complete, set the Segue Kind to Custom in storyboard, and then set the class to classes DetailStoryboardSegue
.
Actually very simple, run up and look.
You will see that the result of the run is a bit of a problem. The label, which was displayed in the correct position, appears at the top of the screen. This is because the front TDViewController
is navigation controller's push out, the top of the screen has a navigation bar, so the label's top constraint is valid. The navigation controller's push does not exist in the switchover of our custom segue. Instead of a simple view overlay replace, no navigation bar. So Labe's top constraint fails and is displayed directly at the top.
This error actually raises a question, but here we do not discuss it in depth for the time being. First look at how the transitioning animtion animation works, and then we discuss this serious issue.
Custom Transitioning Animation
Implementing a custom toggle animation requires UIViewControllerAnimatedTransitioning
that this protocol be implemented. We customize a class DetailTransitioningAnimator
to implement this protocol.
This protocol has two methods that must be implemented, func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval
and func animateTransition(transitionContext: UIViewControllerContextTransitioning)
. Look at the code:
Import Uikitclass Detailtransitioninganimator:nsobject, uiviewcontrolleranimatedtransitioning {Let Durationtimeinterval:nstimeinterval//1 init (duration:nstimeinterval) {durationtimeinterval = Duration } func transitionduration (transitioncontext:uiviewcontrollercontexttransitioning?), Nstimeinterval {retur N Self.durationtimeinterval}//2 func animatetransition (transitioncontext:uiviewcontrollercontexttransitioning) {Let Containerview = Transitioncontext.containerview () Let Sourcecontroller = Transitioncontext.viewcontro Llerforkey (uitransitioncontextfromviewcontrollerkey) Let Destcontroller = Transitioncontext.viewcontrollerforkey (U Itransitioncontexttoviewcontrollerkey) Let Sourceview = sourcecontroller!. View Let Destview = destcontroller!. View Containerview?. Addsubview (Destview)//3 Destview.alpha = 0.0 uiview.animatewithduration (0.3, animations: {dest View.alpha = 1.0}, Completion: {completed in Let cancelled = transitioncontext.transitionwascancelled () tr Ansitioncontext.completetransition (!cancelled)})} func animationended (Transitioncompleted:bool) {}}
- This
init
method is not required, but it can be added in order to customize the execution time of the animation.
- Transitioning how animations are executed. In this method we achieve the effect we achieved in our previous custom segue.
- Note here, with the
let containerView = transitionContext.containerView()
container view that is obtained. Instead of the window that was used before.
The following applies the transitioning animation to the code. It needs to be implemented when it is used UINavigationControllerDelegate
. Create a class NavigationControllerDelegate
to implement this protocol.
Import Uikitclass Navigationcontrollerdelegate:nsobject, uinavigationcontrollerdelegate { func Navigationcontroller (Navigationcontroller:uinavigationcontroller, animationcontrollerforoperation operation: Uinavigationcontrolleroperation, Fromviewcontroller Fromvc:uiviewcontroller, Toviewcontroller ToVC: Uiviewcontroller, uiviewcontrolleranimatedtransitioning? { return Detailtransitioninganimator (duration:0.3) }}
There's not much to talk about here. Just know that this is the way to go.
The class is applied to the storyboard in the following section NavigationControllerDelegate
UINavigationController
.
- Drag the object to the only navigation controller that exists in the project.
- Set the custom class to the object you just started
NavigationControllerDelegate
.
Just focus on the settings on the right side of the class.
- The proxy for navigation controller is set to the delegate just dragged up.
The custom segue code above is removed and slightly modified to run.
There is a problem with the wood, and there are no errors.
The question is not important, but it is still a matter of discussion: Why is custom segue a problem, and transitoning animation is not a problem? This is because the transitioning animation is actually changed on the basis of the navigation controller. The transitioning animation only modifies the navigation controller's push animation to the fade in effect, while the other mechanisms of the navigation controller have not been modified. Custom Segue is a completely animated two view. So, here's a question for the reader to modify the custom segue code above to make this navigation bar appear.
But under what circumstances with custom segue, under what circumstances with transition animation? Transitioning animation more flexible, unlike the custom segue is in the storyboard dead. You can set your own animation according to different situations.
Custom segue is used to invoke methods such as: presentViewController
and dismissViewController
the like. When these methods are called, the conversion animation between the two view controllers is done using the method above. Their duties are different.
Finally, add one point. It is also a place to play the role of custom segue. Custom segue can be used in multiple storyboard cases. Step: (Suppose you have created another storyboard, called Another.storyboard)
- Drag a storyboard reference into your current storyboard.
- Click on this storyboard reference, and in the right sidebar storyboard fill in another storyboard name, here is Another.storyboard.
- Ctrl+drag, a view connected from a button in a view controller to the storyboard reference you just added. (Make sure your other storyboard view controller is set to start by default)
- If you are starting a particular view controller for another storyboard, then you can write a custom segue. In this custom segue, initialize another storyboard, get the view controller you want to start, and complete the last step of the controller jump in the Perform method of custom segue.
To be Continued ...
Swift: Is it a custom segue or a transition animation?