Launch Image Not Uploaded Xcode Swift 4

iOS

Creating Simple View Animations in Swift


Following the release of iOS vii (and iOS 8 every bit well), animation and motility effects became central to the pattern of apps from both Apple and 3rd party developers. iOS 7 introduced a apartment, minimal design to apps which inevitably resulted in some apps having a similar UI. To distinguish their apps from other apps, developers employed such features as animations and motion furnishings to make their apps stand out.

Not only are animations used to ready your app apart, they tin improve the overall user feel of your application. For great examples of how animations are used to improve UX, you should look at how Apple tree uses animations in their apps. For case, in the Photos app, when you select a photograph from a collection, the photo expands out from the selected one and on endmost it, it shrinks dorsum to the selected photo. This adds to the navigation of the app in that it lets y'all know exactly where yous were, if browsing many pictures.

view-animations-featured

Facebook's Newspaper also employs animations beautifully to add to the overall user experience of the app. You select an article to read past flipping information technology up. This action, the fact that the article expands out from its thumbnail version, suggests that in doing the opposite, i.e. flipping the article downwards, would shrink it back to its thumbnail. Here, animation is used to convey how the app works and even a first time user of the app would soon exist able to brand assumptions on its use and figure out its navigation without needing a tutorial.

Non simply do animations add to the user experience of the app, but they could be what delights and impresses your users guaranteeing echo usage instead of uninstalling the app for a better one from the App Shop.

There are numerous ways of including animations in your apps, some of them being past use of UIKit Dynamics, layer animations, view controller transitions or by using a third political party library like the Facebook Pop library or JNWSpringAnimation framework.

In this tutorial we are going to wait at simple view animations. You lot can download the starter project which is a Single View Application with a tabular array view that lists the examples presented in the tutorial tutorial. I will be using this when nosotros go to the examples portion of the tutorial, instead of starting from scratch.

The tutorial starts off with a summary of APIs used to animate views, and ends with some examples which show the use of some of the APIs in an app.

Basic View Animations

Creating animations on your views is a matter of changing backdrop on them and letting UIKit animate them automatically. The properties we alter are the ones marked Animatable.

The following list shows the animatable properties.

  • eye
  • blastoff
  • frame
  • bounds
  • transform
  • backgroundColor
  • contentStretch

You will find that all animations involve a modify of one or more of the above properties.

For simple view animations, UIKit provides the following APIs that can be used to animate views on the screen.

  • UIView.animateWithDuration(_:, animations:)
  • UIView.animateWithDuration(_:, animations:, completion:)
  • UIView.animateWithDuration(_:, delay:, options:, animations:, completion:)

The first 1 takes ii parameters – a value for the duration in seconds of the animation and a closure where yous specify the properties you want changed. UIKit will accept the original state of the view and create a smooth transition from that state to the end state according to what you lot specified in the animations closure.

The other two APIs are like to the start, just they accept actress parameters that add together more than configuration to the animation. The second takes a completion closure which y'all tin use to either specify another animation that you want done after the first or you can do some cleanup of the UI, for example, removing a view from the view hierarchy once another view is animated onto the scene.

The third API takes an boosted 2 parameters – the delay, which is the fourth dimension to wait before the blitheness starts and options, a UIViewAnimationOptions abiding, which indicates how you want to perform the animations. The following shows the options available.

anim_image001

Spring Animations

Leap animations try to model the behaviour of a real life spring, in that, when a view is moved from one point to another, it will bounce/oscillate towards the stop before settling downwards to position.

Below is the method cake nosotros use for bound animations.

  • UIView.animateWithDuration(_:, delay:, usingSpringWithDamping:, initialSpringVelocity:, options:, animations:, completion:)

The to a higher place is similar to the methods we looked at earlier except for two new parameters – usingSpringWithDamping and initialSpringVelocity. Damping is a value from 0 to 1 that determines how much the view bounces towards the end of the animation. The closer to i the value is, the less bouncy it volition be. initialSpringVelocity as the name says, determines the initial velocity of the animation. This determines how potent the blitheness starts off. If you lot want it to start vigorously, so ready a larger value, if you want a smooth animation, and then you can set the value to 0.

Keyframe Animations

Keyframe animations enable you to set dissimilar stages of an animation. You tin grouping dissimilar animations together that share some common backdrop, but yet be able to command them separately.

Instead of an animation that just moves along a path, UIKit will execute the dissimilar stages of the animation.

The Keyframe animation APIs are as follows.

  • UIView.animateKeyframesWithDuration(_:, filibuster:, options:, animations:)
  • UIView.addKeyframeWithRelativeStartTime(_:, relativeDuration:)

The higher up two methods are used together, the second getting nested in the first's animations closure.

The showtime method sets the overall configuration of the animation, like how long information technology takes, delay and its options. You lot then define one or more of the second method(the frames) inside the animations closure to set the unlike stages of the animation.

The relative outset fourth dimension and relative duration of each frame is a value between 0 and i that expresses the per centum time within the total duration of the blitheness.

View Transitions

View transitions are used when you desire to add together a new view to your view bureaucracy or remove a view from the view hierarchy.

The APIs that are used to create view transitions are

  • UIView.transitionWithView(_:, duration:, options:, animations:, completion:)
  • UIView.transitionFromView(_:, toView:, duration:, options:, completion:)

Yous employ the first ane to introduce a view to the view hierarchy. The method takes similar parameters as we have seen in the other animation methods.

The second i is used to take one view from the view hierarchy and and identify a new view in its place.

Examples

Nosotros'll now await at a few examples that utilise some of the above API calls to animate views in the given starter project.

Example I

If you lot run the project, you'll see a table view that lists the examples we'll work through. Select Case I in the listing and you should see a Login screen of an app with the username and password fields and login push button.

We desire these to be blithe onto the screen when the app starts.

To get started we'll hide the views from sight when the view first appears. Before Motorcar Layout, this would accept been a simple matter of changing the specific view'southward position in code, but since we set up auto layout constraints on the views in the storyboard file, we'll have to modify the constraints in lawmaking, which volition change the view's position.

First, we need to get a reference of the constraints that we will change. Open the storyboard file. Locate the post-obit constraints in the Instance I Scene.

anim_image002

Open the Assistant Editor, and brand sure information technology is the ExampleIViewController.swift that appears next to the storyboard on the separate screen. Elevate from the Center X Alignment – View – Username constraint to the ExampleIViewController class. Create an outlet named centerAlignUsername. Exercise the aforementioned for the Eye X Alignment – View – Password and fix its name to centerAlignPassword. Besides create an outlet for the login push button named loginButton and an action for the same push button and name it login. Make sure you ready the Blazon of the action to UIButton. You should have the post-obit in lawmaking.

In the ExampleIViewController.swift add the following method which is chosen before the view is presented on screen.

This moves the username and password fields just out of view and sets the alpha value of the push to 0 which makes it invisible.

Add the following method which is chosen right when the view appears.

Here nosotros use the UIView.animateWithDuration() method that nosotros saw earlier. We include the UIViewAnimationOptions.CurveEaseOut option which makes the animation start fast then slow down at the end. You can experiment with different options here. Command-click on UIViewAnimationOptions to meet all the options bachelor.

The animation lasts for 0.5 seconds and starts immediately. Yous have liberty over the duration, but yous shouldn't set such a large number every bit to annoy your users when animations on your app seem to take also long. Normally, the duration is set betwixt 0.v and 0.7 seconds, but as I said, this isn't set in stone and you take liberty to ready information technology to any feels right to you.

The blitheness does the exact opposite of what we did in viewWillAppear(). layoutIfNeeded() is used to lay out the views immediately they are changed. If y'all don't include it, you lot will not see them become animated onto the screen, instead they will simply exist shown in their final position. Run the app, and y'all should see the following.

video001

The above looks more than interesting than a static presentation, but the views getting animated at the same fourth dimension, doesn't create that neat an consequence. Modify the method as shown below.

Run the app and the resulting animation has the views animating on screen at unlike times. Looking at the lawmaking, you can see that subsequently the first blitheness block, we set a delay on subsequent ones.

video002

In login screens, when login fails, there is usually an animation that indicates to the user that login has failed. This is sometimes done equally a shake of the text fields or the login push, and a message that lets the user know that login has failed. We'll add such an consequence on the login button using springs. Modify the login() function equally shown.

The above changes the size of the login button when pressed and animates the activity with a jump animation which will cause the button's width to expand and bounciness a piddling at the end before settling.

video003

Play around with the damping value. If you fix information technology to 1, the push button will expand, with no bouncing at the terminate. You can likewise use the same method on the username and countersign fields. Instead of having them come up onto the screen and just stop in place at the end, have them be bound-like and bounce a footling before settling down.

Instance Two

In your app, y'all might need to supervene upon the groundwork image of a view automatically when some activity happens. Yous could do it in a style where an image immediately replaces another on the view, or you could slowly fade it in to create a nice transition. We'll create this consequence here. Open ExampleIIViewController.swift and add the following.

Hither we create an image view and add together information technology to the main view. We and then call imageFadeIn() which creates a 2d view with a unlike image. Nosotros add together this view in a higher place the kickoff image view and set up its alpha to 0. In the blitheness cake, we animate its alpha value, making information technology visible. We then use a completion closure to gear up the image view'southward image to the 2nd epitome and nosotros remove the second prototype view from the view hierarchy since it is no longer needed. I've added a long delay so that the animation doesn't happen right at the moment we select Example Ii from the table view. The duration is also a scrap long so we can run across what's going on in the demo.

The following is the upshot.

videoeg02

Example Three

Side by side we'll look at keyframe animations. Open ExampleIIIViewController.swift and add the post-obit variable and functions to the file. I've commented the code to explain each step.

In viewDidLoad() call the createView() function. Add the post-obit to the bottom of viewDidLoad().

On running the app and selecting Example 3 from the table view, you lot should see a crimson view with an epitome view at the top and a push in the middle.

anim_image003

We want to have the view(which I'll refer to every bit the alertView from now onwards) to shrink in size and fall downwards out of view.

When we created the push, we added a listener to information technology. When the button is tapped, dismissAlert() is called. Modify the role every bit shown.

In the above code, we create frames that correspond what we want for the two stages of animating the view. smallFrame shrinks to half the size of alertView, maintaining the middle point and finalFrame has a position at the bottom of the screen, out of view.

Nosotros use a Keyframe blitheness with two keyframes. The first sets alertView's frame to smallFrame and the second to finalFrame. The consequence will be that the alertView volition shrink to half its size and and so autumn out of view. Notice I take put such a large number for the duration – iv seconds. You lot can alter this, I just wanted the animation running in slow-motion for the demo. Run the app and select Instance III.

video004

The animation isn't quite what we expected. You can see the red alertView breathing as expected, but the scale of its children doesn't modify. Changing the parent's frame, doesn't automatically change its children's frames.

Nosotros'll employ a feature introduced in iOS 7 chosen UIView snapshots to prepare the animation. This allows y'all to have a snapshot of a UIView together with its hierarchy and render information technology into a new UIView.

In dismissAlert() add together the following right before the Keyframe animation code.

Here, we create a snapshot view and add it to the chief view. We then remove the alertView from view, as the snapshot volition replace it.

Supersede the Keyframe blitheness with the following.

Run the application and on tapping Dismiss, the view(snapshot, actually) should animate as expected.

video005

Example IV

We've looked at how you can breathing uncomplicated views. In this example we'll look at how y'all can animate a table view.

If you select Instance IV from the table view, you volition detect another tabular array view with a list of items in information technology. When the table view is presented, the list items are already positioned onto the table. We desire to animate them onto the view to create a more interesting effect.

video006

Open ExampleIVViewController.swift and add the following methods.

Here, when the view appears, the animateTable() function is chosen. We reload the table view data and loop through the cells that are currently visible on the screen and motility each of them to the bottom of the screen. We and so iterate over all the cells that we moved to the bottom of the screen and animate them back to position with a spring blitheness.

Run the app and you should see the post-obit.

video007

Conclusion

Nosotros have looked at diverse APIs that y'all tin can utilise to add animations to your app's views. This is not an exhaustive guide on animation in iOS just by using the simple APIs we have looked at, you tin can create diverse animations in your apps. To dive farther into iOS animations, you could await at UIKit Dynamics, layer animations, view controller transitions and fifty-fifty motility effects. You could too look at third party libraries like Facebook Pop and JNWSpringAnimation framework which could help you build intricate animations more than easily.

You tin download the completed project here.

As always, leave me annotate and share your idea about the tutorial.

Note: This tutorial is also available in Chinese. Nosotros're going to back up other languages shortly. If y'all want to join our translation team, please contact us.

lynchdeplas.blogspot.com

Source: https://www.appcoda.com/view-animation-in-swift/

0 Response to "Launch Image Not Uploaded Xcode Swift 4"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel