Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private let colorArray = ["FBDA75", "FFCCAC", "FFD4E4", "DCDEF9"]
private let imageArray = ["image-1", "image-2", "image-3", "image-4"]
private var timer: Timer?
private var iconsInterval: Int = 0
// for image animation
private var imageView1: UIImageView!
private var imageView2: UIImageView!
private var imageViewChangeflag = 1
private var initalPosition: CGPoint!
private var centerPosition: CGPoint!
private var finalPosition: CGPoint!
private var imageSize: CGFloat = 80
private func setupViews() {
imageBackgroundView.layer.cornerRadius = imageBackgroundView.frame.size.width/2
imageBackgroundView.backgroundColor = UIColor(hexString: colorArray.first)
let initialFrame = CGRect(x: (self.imageBackgroundView.bounds.width - imageSize)/2, y: imageBackgroundView.bounds.height + 20, width: imageSize, height: imageSize)
let centerFrame = CGRect(x: (self.imageBackgroundView.bounds.width - imageSize)/2, y: (self.imageBackgroundView.bounds.height - imageSize)/2, width: imageSize, height: imageSize)
imageView1 = UIImageView(frame: centerFrame)
imageView1.contentMode = .scaleAspectFill
imageBackgroundView.addSubview(imageView1)
imageView2 = UIImageView(frame: initialFrame)
imageView2.contentMode = .scaleAspectFill
imageBackgroundView.addSubview(imageView2)
initalPosition = self.imageView2.layer.position
centerPosition = self.imageView1.layer.position
finalPosition = CGPoint(x: imageView1.layer.position.x, y: imageView1.layer.position.y - imageBackgroundView.bounds.height - 20)
}
func configure() {
setDashImage(forImageId: imageArray[1], onImageView: imageView1)
self.iconsInterval = 1
setDashImage(forImageId: imageArray[1], onImageView: imageView2)
self.iconsInterval = 2
}
private func setDashImage(forImageId imageId: String, onImageView imageView: UIImageView) {
imageView.image = UIImage(named: imageId)
}
private func addCardAnimation() {
DispatchQueue.main.async { [weak self] in
guard let selfRef = self else {
return
}
selfRef.timer?.invalidate()
selfRef.timer = Timer.scheduledTimer(timeInterval: 3.0, target: selfRef, selector: #selector(selfRef.startImageAnimation), userInfo: nil, repeats: true)
}
}
@objc private func startImageAnimation() {
self.startBackColorAnimation()
if imageViewChangeflag == 1 {
addAnimation(firstImageView: imageView1, secondImageView: imageView2)
} else {
addAnimation(firstImageView: imageView2, secondImageView: imageView1)
}
imageViewChangeflag = -imageViewChangeflag
}
private func addAnimation(firstImageView: UIImageView, secondImageView: UIImageView) {
let basicAnimation1 = getBasicAnimation(withInitialPostion: centerPosition, finalPos: finalPosition)
firstImageView.layer.add(basicAnimation1, forKey: "position")
let basicAnimation2 = self.getBasicAnimation(withInitialPostion: self.initalPosition, finalPos: self.centerPosition)
secondImageView.layer.add(basicAnimation2, forKey: "position")
self.addNextImage(forImageView: firstImageView)
}
private func getBasicAnimation(withInitialPostion initialPos: CGPoint, finalPos: CGPoint) -> CABasicAnimation {
let basicAnimation = CABasicAnimation(keyPath: "position")
basicAnimation.fromValue = NSValue(cgPoint: initialPos)
basicAnimation.toValue = NSValue(cgPoint: finalPos)
basicAnimation.duration = 1
basicAnimation.isRemovedOnCompletion = false
basicAnimation.fillMode = CAMediaTimingFillMode.forwards
basicAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
return basicAnimation
}
private func addNextImage(forImageView imageView: UIImageView ) {
let iconIndex = self.iconsInterval % self.imageArray.count
let imageId = imageArray[iconIndex]
setDashImage(forImageId: imageId, onImageView: imageView)
iconsInterval += 1
}
private func startBackColorAnimation() {
var colorIndex = (self.iconsInterval-1) % self.imageArray.count
var prevIndex = (self.iconsInterval-2) % self.imageArray.count
if colorIndex > colorArray.count - 1 {
colorIndex = 0
}
if prevIndex > colorArray.count - 1 {
prevIndex = 0
}
let basicAnimation = CABasicAnimation(keyPath: "backgroundColor")
basicAnimation.fromValue = UIColor(hexString: colorArray[prevIndex])?.cgColor
basicAnimation.toValue = UIColor(hexString: colorArray[colorIndex])?.cgColor
basicAnimation.duration = 0.5
basicAnimation.isRemovedOnCompletion = false
basicAnimation.fillMode = CAMediaTimingFillMode.forwards
imageBackgroundView.layer.add(basicAnimation, forKey: "backgroundColor")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment