Оживленное изменение состояния UIButton - PullRequest
44 голосов
/ 10 мая 2011

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

Ответы [ 5 ]

115 голосов
/ 19 августа 2012

Это можно сделать с помощью анимации перехода UIView.Не имеет значения, что свойство isHighlighted не является анимируемым, потому что оно перемещает весь вид.

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];
3 голосов
/ 02 июня 2011

Для этого я расширил 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];
}
2 голосов
/ 27 марта 2016

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

UIView.transitionWithView(button, 
                          duration: 4.0, 
                          options: .TransitionCrossDissolve, 
                          animations: { button.highlighted = true },
                          completion: nil)
2 голосов
/ 29 октября 2014

Вот автономное решение, которое также поддерживает логический флаг 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();
  }
}
1 голос
/ 10 мая 2011

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

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

Затем все соответствующие методы setAnimation оттуда.

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

  • @ property frame
  • @ границы свойств
  • @ property center
  • @ property transform
  • @ property alpha
  • @ property backgroundColor
  • @ property contentStretch

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...