Подтвердить что ты не робот

Анимация изменения состояния UIButton

Я использую UIButton с изображениями для нормальных и выделенных состояний. Они работают так, как ожидалось, но я хочу иметь некоторый переход затухания/слияния, а не просто внезапный обмен.

Как я могу это сделать?

4b9b3361

Ответ 1

Это можно сделать, используя анимацию перехода UIView.

Swift 3

UIView.transition(with: button,
                  duration: 4.0,
                  options: .transitionCrossDissolve,
                  animations: { button.isHighlighted = true },
                  completion: nil)

Objective-C

[UIView transitionWithView:button
                  duration:4.0
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{ button.highlighted = YES; }
                completion:nil];

Ответ 2

Чтобы это сделать, я расширил UIButton. добавлено новое свойство hilightedImage со следующим кодом:

- (void)setHilightImage:(UIImageView *)_hilightImage
{
    if (hilightImage != _hilightImage) {
        [hilightImage release];
        hilightImage = [_hilightImage retain];
    }
    [hilightImage setAlpha:0];
    [self addSubview:hilightImage];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.14];
    if(hilightImage){
        [hilightImage setAlpha:1];
    }
    [UIView commitAnimations];
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    self.highlighted = FALSE;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.14];
    if(hilightImage){
        [hilightImage setAlpha:0];
    }

    [UIView commitAnimations];
    [super touchesEnded:touches withEvent:event];
}

Ответ 3

@marián-Černý ответ в Swift:

UIView.transitionWithView(button, 
                          duration: 4.0, 
                          options: .TransitionCrossDissolve, 
                          animations: { button.highlighted = true },
                          completion: nil)

Ответ 4

UIButton наследует от UIView

Итак, вы получаете свое представление и вызываете beginAnimations:context:

Затем все подходящие методы setAnimation оттуда.

Следующие свойства класса UIView являются анимируемыми:

  • рамка @property
  • @property bounds
  • @property center
  • Преобразование @property
  • @property alpha
  • @property backgroundColor
  • @property contentStretch

Ссылка: Ссылка на класс UIView

Ответ 5

Вот автономное решение, которое также поддерживает флаг boolean animated.

- (void)setEnabled:(BOOL)enabled animated:(BOOL)animated
{
  if (_button.enabled == enabled) {
    return;
  }

  void (^transitionBlock)(void) = ^void(void) {
    _button.enabled = enabled;
  };

  if (animated) {
    [UIView transitionWithView:_button
                      duration:0.15
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                      transitionBlock();
                    }
                    completion:nil];
  } else {
    transitionBlock();
  }
}