0
0

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 でもハイライト効果を得たい

Posted at

UIButton.imageViewでアニメーションをさせていると、タップしてもハイライト効果が得られない・UIControlStateHighlightedの画像が表示されないのであれこれと試してみた。
最終的に TouchDown 時にアニメーションを止めて一旦別画像に差し替えてから、async で元の画像に戻すという暴挙にでることで対応。

もう少しスマートな対応方法はないものだろうか…。

- (IBAction)buttonTouchDown:(id)sender {
    // アニメーションしているボタンはハイライト効果が得られないため、
    // アニメーションを止めて画像を一旦差し替えて再描画を行うようにする。
    if ([self.button.imageView isAnimating]) {
        [self.button.imageView stopAnimating];
        
        // set other image
        __block UIImage *originalImage = [self.button imageForState:UIControlStateNormal];
        UIImage *otherImage = [UIImage imageNamed:@"other"];
        [self.button setImage:otherImage forState:UIControlStateNormal];

        dispatch_async(dispatch_get_main_queue(), ^(void) {
            // set original image
            [self.button setImage:originalImage forState:UIControlStateNormal];
        });
    }
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?