LoginSignup
3
3

More than 5 years have passed since last update.

ローディング中のぐるぐる機能つきの UIImageView

Posted at

ネットワークから画像を読み込むときとか、読み込み中は UIActivityIndicatorView を出しておきたかったので作ってみました。

クラス名がいいものが思いつかなかったです ><
なんかキラキラした名前下さい!!

class LoadableImageView < UIImageView
  attr_accessor :loading, :loading_view

  def initWithFrame(rect)
    super
    @loading = false
    @loading_view = UIActivityIndicatorView.alloc.initWithActivityIndicatorStyle(UIActivityIndicatorViewStyleWhite).tap do |lv|
      lv.color = UIColor.blackColor
    end
    self
  end

  def loading=(loading)
    if loading
      self.image = nil
      addSubview(@loading_view)
      @loading_view.startAnimating
    else
      @loading_view.stopAnimating
    end
  end

  def layoutSubviews
    super
    w = 30
    h = 30
    x = (self.frame.size.width/2.0 - w/2)
    y = (self.frame.size.height/2.0 - h/2)
    @loading_view.frame = [[x, y], [w, h]]
  end
end

使い方

image_view = LoadableImageView.new
# ぐるぐる出す
image_view.loading = true
# ぐるぐるやめる
image_view.loading = false

ここまで書いて思ったのですが、imageプロパティが nil のときに自動的にぐるぐるした方が使いやすいかもですね。

3
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
3
3