IOS custom page switching animation and Interactive Animation, ios custom page Switching

Source: Internet
Author: User

IOS custom page switching animation and Interactive Animation, ios custom page Switching

Before iOS7, developers sought to customize the Push/Pop animation of the Navigation Controller. They could only subclass A UINavigationController or overwrite it with a custom animation. However, with the advent of iOS7, Apple has released a new tool for developers to manage UIViewController switching in a more flexible manner.


Effect preview:


Custom Push/Pop animation in the navigation bar

To perform Custom Animation Switching Based on UINavigationController, first create a simple project. The rootViewController of this project is a UINavigationController, the rootViewController of UINavigationController is a simple UIViewController (called the home page). A Button on the UIViewController can be used to enter the next UIViewController (called the details page ), we first implement two protocols on the ViewController on the home page: UINavigationControllerDelegate andUIViewControllerAnimatedTransitioningIn ViewDidLoad, set the navigationController's delegate to self. In this way, we will know when pushing and Pop in the navigation bar, and then use an attribute to write down whether it is Push or Pop, just like this:

func navigationController(navigationController: UINavigationController!, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController!, toViewController toVC: UIViewController!) -> UIViewControllerAnimatedTransitioning! {    navigationOperation = operation    return self}
This is a new method of iOS7, which requires you to provide a UIViewControllerAnimatedTransitioning UIViewControllerAnimatedTransitioningWhat is it?

UIViewControllerAnimatedTransitioning is a new protocol added by Apple. Its purpose is to use Custom Animation without affecting other attributes of the view, so that you can focus on the animation implementation, then, you can write Custom Animation code in the callback of this Protocol, that is, "What should happen during the switchover", which is responsible for the specific content of the switchover, any object that implements this protocol is calledAnimation controller.You can use the Protocol to implement this feature by any object, so as to encapsulate various animation effects into different classes, as long as it is convenient to use and manage, you can play all the means. I can also enable the animation controller on the home page, because it is the rootViewController in the navigation bar and will always exist, I only need to write custom Push and Pop animation code in it:

// UIViewControllerTransitioningDelegatefunc transitionDuration (transitionContext: UIViewControllerContextTransitioning !) -> NSTimeInterval {return 0.4} func animateTransition (transitionContext: UIViewControllerContextTransitioning !) {Let containerView = transitionContext. containerView () let toViewController = transitionContext. viewControllerForKey (response) let fromViewController = transitionContext. viewControllerForKey (response) var destView: UIView! Var destTransform: CGAffineTransform! If navigationOperation = UINavigationControllerOperation. push {containerView. insertSubview (toViewController. view, aboveSubview: fromViewController. view) destView = toViewController. view destView. transform = CGAffineTransformMakeScale (0.1, 0.1) destTransform = CGAffineTransformMakeScale (1, 1)} else if navigationOperation = UINavigationControllerOperation. pop {containerView. insertSubview (toViewController. view, belowSubview: fromViewController. view) destView = fromViewController. view // If the IDE is Xcode6 Beta4 + iOS8SDK, the animation will not be executed (not sure where the Bug is) destTransform = CGAffineTransformMakeScale (0.1, 0.1 )} UIView. animateWithDuration (transitionDuration (transitionContext), animations: {destView. transform = destTransform}, completion: ({completed in transitionContext. completeTransition (true )}))}

The first method above returns the animation duration, and the following method is the specific place to implement the animation. The UIViewControllerAnimatedTransitioning protocol contains an object: transitionContext, which can be used to obtain context information during the switchover, for example, from which VC to which VC. We get containerView from transitionContext. This is a special container, and the animation during the switch will be carried out in this container. The keys and UITransitionContextToViewControllerKey are the VC from which to switch to which, which is easy to understand, you can also directly obtain the UITransitionContextFromViewKey and UITransitionContextToViewKey of the view.

I simply divided the animation by Push and Pop. The scale increases from small to small during Push, and the scale decreases from large to small during Pop. The view layers of toViewController vary with different operations. Finally, callCompleteTransitionTells transitionContext that your animation has ended. This is a very important method,Required. The child view of containerView is not cleared at the end of the animation (for example, the view of fromViewController is removed) Because transitionContext is automatically cleared, so we do not need to perform any additional operations.

Note that the Interactive Return effect of the original navigation bar is lost. If you want to use the original Interactive Return effect, return nil in the delegate method of the returned animation controller, for example:

if operation == UINavigationControllerOperation.Push {    navigationOperation = operation    return self}return nil
Then in viewDidLoad, Objective-C directly self. navigationController. interactivepopgsturerecognizer. delegat = self. In addition to navigationController, Swift only needs. interactivepopgsturerecognizer. in addition to delegate = self, the UIGestureRecognizerDelegate protocol must be declared on self, although you have not actually implemented it.

A simple Push/Pop animation for the custom navigation bar is complete.


The custom Modal Present/Dismiss animations the custom Modal Present and Dismiss animations are similar to the previous ones. We need to provide an animation manager. We use the details page to display a Modal page, the details page is used as the animation Manager:
func transitionDuration(transitionContext: UIViewControllerContextTransitioning!) -> NSTimeInterval {    return 0.6}func animateTransition(transitionContext: UIViewControllerContextTransitioning!) {    let containerView = transitionContext.containerView()        let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)    let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)        var destView: UIView!    var destTransfrom = CGAffineTransformIdentity    let screenHeight = UIScreen.mainScreen().bounds.size.height        if modalPresentingType == ModalPresentingType.Present {        destView = toViewController.view        destView.transform = CGAffineTransformMakeTranslation(0, screenHeight)        containerView.addSubview(toViewController.view)    } else if modalPresentingType == ModalPresentingType.Dismiss {        destView = fromViewController.view        destTransfrom = CGAffineTransformMakeTranslation(0, screenHeight)        containerView.insertSubview(toViewController.view, belowSubview: fromViewController.view)    }        UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0,    options: UIViewAnimationOptions.CurveLinear, animations: {        destView.transform = destTransfrom    }, completion: {completed in        transitionContext.completeTransition(true)    })}
The animation part uses an iOS7 spring animation. The smaller the value of usingSpringWithDamping, the more obvious it is. The rest of the animation is similar to the previous one, the difference is that in addition to the animation manager on the home page, the UINavigationControllerDelegate protocol is also implemented, because we are the animation of the custom navigation bar, and here we need to implement a custom Modal animation to implement another protocol: UIViewControllerTransitioningDelegateThis Protocol is similar to the previous UINavigationControllerDelegate protocol and returns an animation manager. There are four iOS7 methods in total, and there are two interactive ones. We only need to implement the other two:
func animationControllerForPresentedController(presented: UIViewController!, presentingController presenting: UIViewController!, sourceController source: UIViewController!) -> UIViewControllerAnimatedTransitioning! {    modalPresentingType = ModalPresentingType.Present    return self}func animationControllerForDismissedController(dismissed: UIViewController!) -> UIViewControllerAnimatedTransitioning! {    modalPresentingType = ModalPresentingType.Dismiss    return self}
I also use an attribute to write down whether it is Present or Dismiss, and then return self. Because I use Storyboard, you need to set transitionDelegate IN THE prepareForSegue method:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {    let modal = segue.destinationViewController as UIViewController    modal.transitioningDelegate = self}
Set the transitionDelegate attribute for the VC that needs to execute the Custom Animation. In this way, a custom animation for modal VC is also completed.
The Interactive Animation in the Custom navigation bar is similar to the animation controller. UIViewControllerInteractiveTransitioningThe object of the Protocol is called Interactive ControllerThe most common method is to apply the interaction controller to the Back gesture returned in the navigation bar. To implement a custom Interactive Animation, we can do this in two ways: implement an interactive controller, or use the UIPercentDrivenInteractiveTransition class provided by iOS as the interactive controller.
Here we use UIPercentDrivenInteractiveTransition to complete Interactive Animation in the navigation bar. Let's take a look at the definition of UIPercentDrivenInteractiveTransition:

In fact, this class is an interactive controller that implements the UIViewControllerInteractiveTransitioning protocol. We can use it to easily add an interactive animation to the animation controller. Call updateInteractiveTransition: update progress; call cancelInteractiveTransition to cancel interaction and return to the status before switch; call finishInteractiveTransition to notify that context interaction has been completed, just like completeTransition. We apply the interactive animation to the Back-to-home page on the details page. Since the role of the previous animation manager is on the home page, the Navigation Controller's delegate can only have one at a time, here, the role of the Interaction controller is also played on the home page. First, add a gesture reader:

let popRecognizer = UIScreenEdgePanGestureRecognizer(target: self, action: Selector("handlePopRecognizer:"))popRecognizer.edges = UIRectEdge.Leftself.navigationController.view.addGestureRecognizer(popRecognizer)
UIScreenEdgePanGestureRecognizer inherits from UIPanGestureRecognizer. It can detect gestures sliding from the edge of the screen and set edges to the left side of left detection. Then implement handlePopRecognizer:

func handlePopRecognizer(popRecognizer: UIScreenEdgePanGestureRecognizer) {    var progress = popRecognizer.translationInView(navigationController.view).x / navigationController.view.bounds.size.width    progress = min(1.0, max(0.0, progress))        println("\(progress)")    if popRecognizer.state == UIGestureRecognizerState.Began {        println("Began")        self.interactivePopTransition = UIPercentDrivenInteractiveTransition()        self.navigationController.popViewControllerAnimated(true)    } else if popRecognizer.state == UIGestureRecognizerState.Changed {        self.interactivePopTransition?.updateInteractiveTransition(progress)        println("Changed")    } else if popRecognizer.state == UIGestureRecognizerState.Ended || popRecognizer.state == UIGestureRecognizerState.Cancelled {        if progress > 0.5 {            self.interactivePopTransition?.finishInteractiveTransition()        } else {            self.interactivePopTransition?.cancelInteractiveTransition()        }        println("Ended || Cancelled")        self.interactivePopTransition = nil    }}
I used an instance variable to reference UIPercentDrivenInteractiveTransition. This class is created only when needed. Otherwise, even if the click operation does not recognize the gesture during normal Push/Pop operations, it will also enter the interaction (you can also make some judgments when asking you to return the interaction controller, and block them by returning nil, but this is obviously too troublesome ). When a gesture is identified, pop is called. When a user's gesture changes, update is called to update. Whether it is end or cancel, the system determines whether to enter the next page or return the previous page, after all this is done, clear the interaction controller.

Now that we have an interactive Controller object, we only need to inform it to the Navigation Controller. Another way to implement UINavigationControllerDelegate is as follows:

func navigationController(navigationController: UINavigationController!, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning!) -> UIViewControllerInteractiveTransitioning! {    return self.interactivePopTransition}
We have finished returning the details page to the previous page through a custom interactive animation.

Demo of using UIPercentDrivenInteractiveTransition


I mentioned earlier that UIPercentDrivenInteractiveTransition is actually implemented. UIViewControllerInteractiveTransitioningProtocol, as long as the object implementing this Protocol can be called an interactive controller, if we want to more accurately manage the animation and understand the details of the processing, we need to implement it by ourselves. UIViewControllerInteractiveTransitioningProtocol. The UIViewControllerInteractiveTransitioning Protocol has a total of three methods, of which startInteractiveTransition: is a required method, in which we initialize the animation status:
func startInteractiveTransition(transitionContext: UIViewControllerContextTransitioning!) {    self.transitionContext = transitionContext        let containerView = transitionContext.containerView()    let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)    let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)        containerView.insertSubview(toViewController.view, belowSubview: fromViewController.view)        self.transitingView = fromViewController.view}
Animation is not involved here. You only need to add the view to be switched to the context. The animation part is consistent with the previously used UIPercentDrivenInteractiveTransition interface. add several methods:
func updateWithPercent(percent: CGFloat) {    let scale = CGFloat(fabsf(Float(percent - CGFloat(1.0))))    transitingView?.transform = CGAffineTransformMakeScale(scale, scale)    transitionContext?.updateInteractiveTransition(percent)}func finishBy(cancelled: Bool) {    if cancelled {        UIView.animateWithDuration(0.4, animations: {            self.transitingView!.transform = CGAffineTransformIdentity        }, completion: {completed in            self.transitionContext!.cancelInteractiveTransition()            self.transitionContext!.completeTransition(false)        })    } else {        UIView.animateWithDuration(0.4, animations: {            print(self.transitingView)            self.transitingView!.transform = CGAffineTransformMakeScale(0, 0)            print(self.transitingView)        }, completion: {completed in            self.transitionContext!.finishInteractiveTransition()            self.transitionContext!.completeTransition(true)        })    }}
UpdateWithPercent: The method is used to update the transform attribute of the view. finishBy: The method is mainly used to determine whether to enter the next page or return to the previous page, and inform transitionContext of the current status, and the final animation of the current scale view. Here the transitionContext and transitingView can be obtained in the previous code for processing Gesture Recognition. I updated the code in it and changed it to the following:
func handlePopRecognizer(popRecognizer: UIScreenEdgePanGestureRecognizer) {    var progress = popRecognizer.translationInView(navigationController.view).x / navigationController.view.bounds.size.width    progress = min(1.0, max(0.0, progress))        println("\(progress)")    if popRecognizer.state == UIGestureRecognizerState.Began {        println("Began")        isTransiting = true          //self.interactivePopTransition = UIPercentDrivenInteractiveTransition()        self.navigationController.popViewControllerAnimated(true)    } else if popRecognizer.state == UIGestureRecognizerState.Changed {          //self.interactivePopTransition?.updateInteractiveTransition(progress)        updateWithPercent(progress)        println("Changed")    } else if popRecognizer.state == UIGestureRecognizerState.Ended || popRecognizer.state == UIGestureRecognizerState.Cancelled {          //if progress > 0.5 {          //    self.interactivePopTransition?.finishInteractiveTransition()          //} else {          //    self.interactivePopTransition?.cancelInteractiveTransition()          //}        finishBy(progress < 0.5)        println("Ended || Cancelled")        isTransiting = false          //self.interactivePopTransition = nil    }}
In addition, an extra Boolean variable isTransiting is used to identify whether the current Gesture Recognition is in progress. This is to prevent self from being returned when an interactive controller is returned:
func navigationController(navigationController: UINavigationController!, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning!) -> UIViewControllerInteractiveTransitioning! {    if !self.isTransiting {        return nil    }    return self}
This completes the Custom interaction controller. It can be found that the basic process is consistent with the use of UIPercentDrivenInteractiveTransition. UIPercentDrivenInteractiveTransition mainly encapsulates the initialization of transitionContext and calls to it, but the animation must be further processed.
Use the Demo of the Custom interaction controller (it always fails to upload to my resource page, so it can only be uploaded to GitHub)

The following figure is attached. It is easier to distinguish the protocols with similar names:




What is the format of Interactive Animation displayed on the webpage of a mobile phone?

The game needs to rely on others such as flash or c, and interactive demonstration can use html5 functions. For specific examples, I have never been familiar with ios, but interactive animations on the regular website can be implemented by using some of the html functions. simple games can also be achieved, but the effect is unsatisfactory.

How to add materials to a Custom Animation in a ppt

I. View changes
1. The general view of PowerPoint 2003 is divided into two modes: "outline" and "slide". The new "slide" mode allows us to see the full picture of the slides, this gives you a clear grasp of the overall structure when operating a slide.
2. It is more convenient and convenient to insert a new slide: In "slide" view mode, you only need to click the Enter key to add a new slide under the edited slide.
3. In slide show mode, the second icon in the lower left corner can change the cursor pointer to a ballpoint pen, felt tip pen, or highlighter. These pen types can be circled on the playback slides, it can be used as a pointer, And the pen tip color can be changed to save the handwriting.
Ii. Changes in operation objects
1. All objects are edited with an extra rotation point, which can be rotated at will.
2. clip Art: A small image library provided by the PowerPoint software. PowerPoint 2003 provides more image materials. We can use the "clip art" Panel (on the right ), you can quickly search for your image in the clip art by entering search text, search range, and result type.
3. Create a new album: PowerPoint 2003 provides the album creation function, which can insert a large number of images into the slides conveniently and quickly, and is suitable for image teaching.
Operation Method:
① Click "file"> "new" in the menu bar"
② In the "new presentation" Panel popped up on the right, select "album"
③ In the pop-up "album" dialog box, locate the image location, select multiple images to be entered, and click "insert"
④ In the "album" dialog box, you can delete unnecessary images and adjust the order, direction, brightness, and contrast of the images. In the "album layout", you can adjust the Image Layout and frame shape, you can also add design templates for albums.
⑤ You can click "format"> "album" in the menu bar to adjust and modify the "album format.
4. Insert icons: These icons make the description of some processes and relationships more intuitive, and you do not need to use a custom image for a 1.1-point combination!
Operation Method: click "insert"> "icon" in the menu bar. In the displayed dialog box, select a suitable relationship diagram.
A. Organization Chart-used to display hierarchical relationships;
B. cycle chart: used to display the continuous cycle process;
C. Ray graph: used to display the relationship between core elements;
D. pyramid chart: used to display basic relationships;
E. Wayne diagram: used to display overlapping areas between elements;
F. Target chart-used to display the steps for achieving the target.
You can use the graphic toolbar to edit and manage the graphic, including "insert graphics, layout, selection, graphic style, and adapt to text.
In addition to the organization chart, other diagrams can be converted.
3. Different operations on slides
When you apply a design template in a slide, whether you operate from the "insert" menu, the tool button, or the shortcut key "Ctrl + M" when using PowerPoint 2000 ", select the appropriate layout from the pop-up layout. You can directly select the "slide layout" from the application design module in the slide design panel in 2003. You do not have to delete the inappropriate layout, replace the corresponding layout directly.
Operation Method: click "format" in the menu bar and select "slide layout" or "slide design"
1. slide layout: the slide layout is placed on the right side of the normal View window (float Panel can be dragged). It can be divided into four categories: Text layout, content layout, text and content layout, and other layout, this makes it easier to create slides.
2. Slide design: the slide design is placed on the right of the normal View window, which can be divided into design templates, color schemes, and animation schemes.
(1) design template: In the past, only one design template was used for each presentation. Instead of directly applying the template to a separate slide, this restriction was not applied in PowerPoint 2003, we can apply the mode to the dashboard, all slides, selected slides, and new presentations, so that any template can be applied to a presentation.
Operation Method:
① Apply to all slides: Click the drop-down arrow on the right of the template and select apply to all slides.
② Apply to selected slides: select one or more slides first, and then click the remaining full text>

Related Article

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.