Я использую UIButton с изображениями для нормальных и выделенных состояний. Они работают так, как ожидалось, но я хочу иметь некоторый переход затухания/слияния, а не просто внезапный обмен.
Как я могу это сделать?
Я использую UIButton с изображениями для нормальных и выделенных состояний. Они работают так, как ожидалось, но я хочу иметь некоторый переход затухания/слияния, а не просто внезапный обмен.
Как я могу это сделать?
Это можно сделать, используя анимацию перехода UIView.
UIView.transition(with: button,
duration: 4.0,
options: .transitionCrossDissolve,
animations: { button.isHighlighted = true },
completion: nil)
[UIView transitionWithView:button
duration:4.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{ button.highlighted = YES; }
completion:nil];
Чтобы это сделать, я расширил 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];
}
@marián-Černý ответ в Swift:
UIView.transitionWithView(button,
duration: 4.0,
options: .TransitionCrossDissolve,
animations: { button.highlighted = true },
completion: nil)
UIButton
наследует от UIView
Итак, вы получаете свое представление и вызываете beginAnimations:context:
Затем все подходящие методы setAnimation оттуда.
Следующие свойства класса UIView
являются анимируемыми:
Ссылка: Ссылка на класс UIView
Вот автономное решение, которое также поддерживает флаг 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();
}
}