Iphone 设置UIButton状态更改的动画
我使用的是一个UIButton,带有正常和高亮状态的图像。它们按预期工作,但我希望有一些衰减/合并过渡,而不仅仅是突然交换Iphone 设置UIButton状态更改的动画,iphone,objective-c,uibutton,Iphone,Objective C,Uibutton,我使用的是一个UIButton,带有正常和高亮状态的图像。它们按预期工作,但我希望有一些衰减/合并过渡,而不仅仅是突然交换 我怎样才能做到这一点呢?ui按钮继承自ui视图 然后你得到它的视图并调用beginAnimations:context: 然后从那里选择所有合适的setAnimation方法 UIView类的以下属性是可设置动画的: @属性框 @属性边界 @物业中心 @属性转换 @阿尔法属性 @属性背景色 @属性contentStretch 参考:为了完成这一点,我扩展了UIButto
我怎样才能做到这一点呢?
ui按钮
继承自ui视图
然后你得到它的视图并调用beginAnimations:context:
然后从那里选择所有合适的setAnimation方法
UIView
类的以下属性是可设置动画的:
- @属性框
- @属性边界
- @物业中心
- @属性转换
- @阿尔法属性
- @属性背景色
- @属性contentStretch
参考:为了完成这一点,我扩展了UIButton。添加了名为HilightDimage的新属性,其代码如下:
- (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];
}
这可以使用UIView的过渡动画来完成。
ishighlight
属性不可设置动画并不重要,因为它会转换整个视图
斯威夫特3
目标-C
这是一个自包含的解决方案,它还支持布尔
动画
标志
- (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();
}
}
@marián-Černý在Swift中回答: Swift 2
UIView.transitionWithView(button,
duration: 4.0,
options: .TransitionCrossDissolve,
animations: { button.highlighted = true },
completion: nil)
Swift 3、4、5
UIView.transition(with: button,
duration: 4.0,
options: .transitionCrossDissolve,
animations: { button.isHighlighted = true },
completion: nil)
使用CustomButton而不是
UIButton
,并重写ishighlight
属性对我有效
class KeypadButton: UIButton {
var highlightDuration: TimeInterval = 0.25
override var isHighlighted: Bool {
didSet {
if oldValue == false && isHighlighted {
highlight()
} else if oldValue == true && !isHighlighted {
unHighlight()
}
}
}
func highlight() {
animateBackground(to: highlightedBackgroundColor, duration: highlightDuration)
}
func unHighlight() {
animateBackground(to: normalBackgroundColor, duration: highlightDuration)
}
private func animateBackground(to color: UIColor?, duration: TimeInterval) {
guard let color = color else { return }
UIView.animate(withDuration: highlightDuration) {
self.backgroundColor = color
}
}
}
我认为突出显示的
属性不可设置动画。@Marty突出显示的属性不可设置动画,因此,必须使用+transitionWithView:duration:options:animations:completion:
,而不是+animateWithDuration:delay:options:animations:completion:
。如前所述,iHighlighted在中不是可设置动画的属性iOS@Jerland2如前所述,转换(使用:UIView…
不关心属性不可设置动画,因为它转换了整个视图。这里的秘密在于.transitionCrossDissolve
。其他动画样式不能可靠地设置动画@MariánČerný-我建议您扩展此示例,了解在何处执行此操作,例如在内部。。。isHighlighted{didSet{}
这不是一个子类,不是一个扩展吗?marty,你可以说“extended”是指“我创建了一个新类…”,就像在示例中一样。
UIView.transition(with: button,
duration: 4.0,
options: .transitionCrossDissolve,
animations: { button.isHighlighted = true },
completion: nil)
class KeypadButton: UIButton {
var highlightDuration: TimeInterval = 0.25
override var isHighlighted: Bool {
didSet {
if oldValue == false && isHighlighted {
highlight()
} else if oldValue == true && !isHighlighted {
unHighlight()
}
}
}
func highlight() {
animateBackground(to: highlightedBackgroundColor, duration: highlightDuration)
}
func unHighlight() {
animateBackground(to: normalBackgroundColor, duration: highlightDuration)
}
private func animateBackground(to color: UIColor?, duration: TimeInterval) {
guard let color = color else { return }
UIView.animate(withDuration: highlightDuration) {
self.backgroundColor = color
}
}
}