Uiimageview Stop Animating and Start Again
How To Make iOS Animations With UIImageView in Swift
Original Link: https://marcosantadev.com/make-uiimageview-animations/
Introduction
When we speak near animations in iOS, we usually call up to the static method          animate          of          UIView          or transition animations. In addition to this,          UIKit          provides too the possibility to create animations with an          UIImageView          using a serial of images.
This article is split in two parts:
- Theoretical: Description of the public interface of            UIImageViewto create the animations.
- Practical: A sample app to show how to create an animation using            UIImageView.
Happy Reading!
Public Interface
          UIImageView          exposes some methods and backdrop to handle the animations:
-             open var animationImages: [UIImage]?
- This array contains the images to evidence in the animation. By default, it's            nil.
-             open var highlightedAnimationImages: [UIImage]?
- This assortment contains the images to bear witness in the blitheness when the view is highlighted. Past default, it's            cipher.
-             open up var animationDuration: TimeInterval
- The fourth dimension, in seconds, to complete a cycle of images. Past default, it's the number of images * 1/30th of a 2nd.
-             open var animationRepeatCount: Int
- Number of times we want to repeat the animation.            0ways infinite. The default value is0.
-             open func startAnimating()
- Method to kickoff the animation.
-             open func stopAnimating()
- Method to stop the blitheness.
-             open var isAnimating: Bool { get }
- Boolean information which indicates if the animation is running.
          UIImageView          shows the          animationImages          and          highlightedAnimationImages          but when the animation is running. If we don't run the animation, the          UIImageView          shows the default paradigm set in the property:
          var image: UIImage?                Case
At present, we are fix to make an awarding using the          UIImageView          for the animations.
In this case, we are going to use an          UIImageView          to make a Chromecast icon animation:
             
          
Getting Started
In the next section, nosotros are going to see all the steps to build the sample app. You lot can download the concluding project here.
The sample app is very evidently. It has ii          UIKit          elements:
-             UIImageView: We'll utilise this component to show the Chromecast icon and its animation.
-             UIButton: The activeness of this button will start the imitation connectedness and disconnection of a Chromecast device.
                      
             
          
Ready icons
Before starting coding, we need the icons to bear witness. You can detect the Chromecast icons set up here.
In one case we download the icons, we can add together them in the projection assets with the following names:
             
          
Gear up UI
The next pace is creating the UI inside the initial view controller of          Main.storyboard:
               
            
So, we can link the UI components to ii          IBOutlet          properties inside          ViewController:
          @IBOutlet private weak var chromecastImageView: UIImageView!
@IBOutlet individual weak var connectButton: UIButton!                Prepare Animation
At this point, nosotros have to prepare the          UIImageView          for the animation.
In          ViewController, we can setup          chromecastImageView          inside the method          viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad() setupImageViewAnimation()
}private func setupImageViewAnimation() {
chromecastImageView.animationImages = [#imageLiteral(resourceName: "cast_0"), #imageLiteral(resourceName: "cast_1"), #imageLiteral(resourceName: "cast_2")]
chromecastImageView.animationDuration = 1
}
With this configuration, nosotros accept an          UIImageView          which is ready to animate the images          cast_0,          cast_1          and          cast_2          in 1 2nd.
          If you don't know                                #imageLiteral                    , I would suggest you to have a expect at                    this article.        
Trigger Animation
The last step is triggering the blitheness. Get-go of all, we demand a belongings to shop the Chromecast state          connected/disconnected:
enum CastState {
case connected
instance asunder
} grade ViewController: UIViewController {private var castState = CastState.asunder
// ...
Then, nosotros must create a new          IBAction          for          connectButton          with the event impact up inside:
          @IBAction private func onConnectButton(_ sender: Any) {
}                Now, nosotros tin add the implementation to trigger the Chromecast icon blitheness:
@IBAction private func onConnectButton(_ sender: Any) {
switch castState {
instance .connected:
disconnect()
example .disconnected:
connect()
}
}individual func connect() {
// Disables the push to avoid user interaction when the animation is running
connectButton.isEnabled = fakechromecastImageView.startAnimating()
// Simulates a connection with a delay of 3 seconds
DispatchQueue.primary.asyncAfter(deadline: .now() + 3) {
self.chromecastImageView.stopAnimating()// Enables the button to allow user interaction
// Updates UI
cocky.connectButton.isEnabled = truthful
cocky.toggleCastState()
}
}private func disconnect() {
// Updates UI
toggleCastState()
}private func toggleCastState() {
// Updates electric current Chromecast country
castState = castState == .continued ? .disconnected : .connected// Updates button title
let buttonTitle = castState == .connected ? "Disconnect" : "Connect"
connectButton.setTitle(buttonTitle, for: .normal)// Updates `UIImageView` default image
let prototype = castState == .connected ? #imageLiteral(resourceName: "cast_connected") : #imageLiteral(resourceName: "cast_disconnected")
chromecastImageView.epitome = epitome
}
We perform the animation only when we connect a new Chromecast device.
Conclusion
In the sample app, we used the          UIImageView          for two purposes:
- Set up the property            epitometo show the current Chromecast state (cast_connected/cast_disconnected).
- Run the Chromecast icon animation.
In some scenarios, nosotros may not need the point 1. If we want to utilize          UIImageView          only to bear witness an blitheness, nosotros can assign to the property          image          an animation similar this:
          let images: [UIImage] = [#imageLiteral(resourceName: "cast_0"), #imageLiteral(resourceName: "cast_1"), #imageLiteral(resourceName: "cast_2")]
chromecastImageView.image = UIImage.animatedImage(with: images, duration: 1)                In this way,          UIImageView          shows an infinite blitheness automatically. We don't need to first it manually.
richardsonegglaity.blogspot.com
Source: https://medium.com/@marcosantadev/how-to-make-ios-animations-with-uiimageview-in-swift-74d372ad2837
0 Response to "Uiimageview Stop Animating and Start Again"
Post a Comment