LoginSignup
1
1

More than 5 years have passed since last update.

UIButtonのタイトルを変更する時にアニメーションをオフにする(RubyMotion)

Last updated at Posted at 2015-02-02

viewDidLoadの中で一度セットしたUIButtonのタイトルをその後変更したいときの方法。

普通にsetTitle:forState:だけ使ってタイトルを変更すると、アニメーションが入ってボタンのタイトルが一度消えたような(ちらついたような)感じになってしまうので、ちょっとだけコードを加える。

test_view_controller.rb
class TestViewController < UIViewController
  def viewDidLoad
    super

    ...

    testView = TestView.setContent self

    ...

    self.view.addSubview testView
  end
end
test_view.rb
class TestView < UIScrollView
  attr_accessor :hogeButton

  def setContent(controller)
    self.alloc.initWithFrame(controller)
  end

  def initWithFrame(controller)
    super(controller.view.bounds)

    ...

    hogeButton = UIButton.buttonWithType(UIButtonTypeSystem).tap do |b|
      ...

      b.setTitle("hogehoge", forState: UIControlStateNormal)

      ...
    end
    self.hogeButton = hogeButton

    # ボタンのタイトルを変更
    changeButtonTitle

    ...
  end


  def changeButtonTitle
    Dispatch::Queue.concurrent.async do
      ...

      Dispatch::Queue.main.async do
        UIView.setAnimationsEnabled false

        self.hogeButton.setTitle("fugafuga", forState: UIControlStateNormal)
        self.hogeButton.layoutIfNeeded

        UIView.setAnimationsEnabled true
      end
    end
  end
end

タイトル変更時はsetAnimationsEnabled:でアニメーションをオフにしておく。
さらにlayoutIfNeededを呼んでレイアウトを変更する。

これで、ボタンのタイトルの文字の変更が、自然な感じになった。

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