Uiimageview Stop Animating and Start Again

How To Make iOS Animations With UIImageView in Swift

  • Theoretical: Description of the public interface of UIImageView to create the animations.
  • Practical: A sample app to show how to create an animation using UIImageView.

Public Interface

  • 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. 0 ways infinite. The default value is 0.
  • 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.
          var image: UIImage?        

Case

Getting Started

  • 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

Gear up UI

          @IBOutlet private weak var chromecastImageView: UIImageView!
@IBOutlet individual weak var connectButton: UIButton!

Prepare Animation

          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
}

Trigger Animation

          enum CastState {
case connected
instance asunder
}
grade ViewController: UIViewController {

private var castState = CastState.asunder

// ...

          @IBAction private func onConnectButton(_ sender: Any) {
}
          @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 = fake

chromecastImageView.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
cocky.connectButton.isEnabled = truthful

// Updates UI
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
}

Conclusion

  1. Set up the property epitome to show the current Chromecast state (cast_connected/cast_disconnected).
  2. Run the Chromecast icon animation.
          let images: [UIImage] = [#imageLiteral(resourceName: "cast_0"), #imageLiteral(resourceName: "cast_1"), #imageLiteral(resourceName: "cast_2")]
chromecastImageView.image = UIImage.animatedImage(with: images, duration: 1)

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel