I’m trying to create a progress bar animation in Swift. I designed a patterned image to simulate progress but can’t directly apply this animation to a UIProgressView. Instead, I placed a UIView on top to mimic this behavior.
I made a symmetric pic: green background, with dark green horizontal lines.
As I understood, since I cannot attach it on UIProgressBar, I added a UIView to mimic ProgressBar.
The idea is to have something like this:
Here’s my current setup:
import UIKit
class SideMenuButtons: UIViewController {
var progressOverlayView: UIView!
@IBOutlet weak var energyReloadingIndicator: UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
private func setupUI() {
progressOverlayView = UIView()
if let patternImage = UIImage(named: "striped-pattern-repeated") {
progressOverlayView.backgroundColor = UIColor(patternImage: patternImage)
} else {
progressOverlayView.backgroundColor = .red
}
energyReloadingIndicator.addSubview(progressOverlayView)
updateProgressOverlayViewWidth()
}
private func updateProgressOverlayViewWidth() {
let progress = CGFloat(energyReloadingIndicator.progress)
let maxWidth = energyReloadingIndicator.frame.width
let overlayWidth = maxWidth * progress
let overlayHeight = energyReloadingIndicator.frame.height
progressOverlayView.frame = CGRect(x: 0, y: 0, width: overlayWidth, height: overlayHeight)
}
}
How can I animate the pattern in progressOverlayView to achieve a continuous moving effect?
2
Answers
Basically, we can add gif image in image view, and achieve animation.
1.
Like this answer
GIF Image to add
As you said, take a timer
2.
timerForIncreaseProgress = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(increaseProgress), userInfo: nil, repeats: true)
Hope, it would help you
Let’s look at one approach that does not require an animated gif, or a pattern image..
We can use a
UIBezierPath
with aCAShapeLayer
to create the pattern in a custom view subclass:and we can set the background color of the view with the "between the lines" color:
Now we can use
CABasicAnimation(keyPath: "position.x")
to "slide" that layer to the left. We create the lines path to be wider than the view so we don’t see a gap on the right (ignore the "hiccup" in this animation, it’s only there because it’s a partial capture):Your original image also appears to have a slight gradient, so we can overlay a translucent gradient for that appearance:
Now we can create a custom "progress" view by embedding that animated shape view in a "container" view, and adjust the width of the container to reflect the progress:
Here is some sample code…
AnimatedPatternView: all of the size, color, speed, etc properties are at the top to make it easy to adjust them to your liking:
EnergyProgressView: view subclass that uses the
AnimatedPatternView
and a container view to simulate a progress bar:ViewController: example view controller with the custom progress bar and a slider to update the progress:
Please Note: this is Example Code Only!!! It should be used as a learning tool, and should not be considered "Production Ready."