21
21

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.

UIButton のアイコン部分だけをアニメーションさせる

Last updated at Posted at 2013-01-08

UIButton は背景とアイコンを別に画像指定出来るので、アイコンにあたる UIImageView の transform を使ってアイコン部分だけをアニメーションさせる、ということができる。
例えば、カメラの撮影ボタンの中身だけを画面回転に合わせて動かしたい時などに使える。

UIButton *btn;
[btn setImage:icon forState:UIControlStateNormal]; // アイコン
[btn setBackgroundImage:bg forState:UIControlStateNormal]; // 背景

// 追記: この行を UIViewAnimation で括るとアニメーションになる --
btn.imageView.transform = transform; // CGAffineTransform
// --

ただ、これだけだと transform 適用時にうまく描画されないことがあるので、以下2行を追加する:

btn.imageView.clipsToBounds = NO;
btn.imageView.contentMode = UIViewContentModeCenter;
21
21
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
21
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?