jQueryを使わずCSSのみで画像をキラッとさせるのswift版です
こんな感じ
ソース
キラッとするView
LightView
class LightView: UIView {
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private weak var light:UIView?
private var isStartAnimation:Bool = false
private let rotate:CGAffineTransform = CGAffineTransform.init(rotationAngle:45*CGFloat.pi / 180.0)
override var frame: CGRect{
didSet{
guard let light = self.light else { return }
light.frame = CGRect(x:0, y:0, width: self.frame.width * 0.4, height: self.frame.height)
light.center = CGPoint(x: 0, y: 0)
}
}
override init(frame: CGRect = .zero) {
super.init(frame: frame)
self.layer.masksToBounds = true
self.setup()
}
private func setup(){
let v = UIView()
v.backgroundColor = .white
self.addSubview(v)
self.light = v
}
private func resetAnimation(){
self.light?.transform = rotate
self.light?.alpha = 0
}
private func animateLight(){
guard self.isStartAnimation else {
self.resetAnimation()
return
}
let delay:Double = 2 //ここを乱数にするとランダムな間隔で光る
UIView.animate(withDuration: 0.2, delay: delay, options: UIViewAnimationOptions.curveEaseInOut, animations: {
self.light?.transform = self.rotate.concatenating(CGAffineTransform.init(scaleX: 0, y: 0))
self.light?.alpha = 0.5
}) { (success) in
UIView.animate(withDuration: 0.2, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
self.light?.transform = self.rotate.concatenating(CGAffineTransform.init(scaleX: 4, y: 4))
self.light?.alpha = 1
}) { (success) in
UIView.animate(withDuration: 0.2, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
self.light?.transform = self.rotate.concatenating(CGAffineTransform.init(scaleX: 50, y: 50))
self.light?.alpha = 0
}) { (success) in
UIView.animate(withDuration: 0.2, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
self.resetAnimation()
}) { (success) in
self.animateLight()
}
}
}
}
}
func startAnimation(){
self.isStartAnimation = true
self.animateLight()
}
func stopAnimation(){
self.isStartAnimation = false
}
}}
使う方
ViewController
import UIKit
class ViewController:UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .black
let lightView = LightView()
self.view.addSubview(lightView)
lightView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
lightView.center = CGPoint(x: self.view.frame.width/2, y: self.view.frame.height/2)
lightView.backgroundColor = .red
lightView.startAnimation()
}
}