4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UIViewをキラッとさせる

Last updated at Posted at 2018-02-04

jQueryを使わずCSSのみで画像をキラッとさせるのswift版です

こんな感じ

demo

ソース

キラッとする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()
  } 
}
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?