如何逐渐降低 UIButton 的不透明度?
How to reduce the opacity of an UIButton gradually?
在我的项目中,我有一个 UIButton,单击它会缩放。到这里为止一切都很好,但我要求按钮的不透明度从 1 逐渐降低到 0。我的意思是它应该按 1,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0.0 顺序降低。我当前的代码是
-(void)roundButtonAnimation
{
self.buttonZoom = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
self.buttonZoom.duration = 0.8f;
self.buttonZoom.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(2,2,2)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(3,3,3)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(5,5,5)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(6,6,6)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(7,7,7)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(9,9,9)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(10,10,10)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(11,11,11)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(12,12,12)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(13,13,13)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(14,14,14)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(15,15,15)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(16,16,16)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(17,17,17)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(18,18,18)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(50,50,50)]];
self.buttonZoom.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
self.roundButton.transform = CGAffineTransformMakeScale(0,0);
self.roundButton.alpha = 0.3;
[self.roundButton.layer addAnimation:self.buttonZoom forKey:@"buttonScale"];
[self.CLSpinnerView1 stopAnimating];
//[self performSelector:@selector(secondView) withObject:self afterDelay:0.3];
[self performSelector:@selector(postNotification) withObject:self afterDelay:0.6];
}
UIView animateWithDuration:animations:
应该能满足您的需求。
[UIView animateWithDuration:1.0 animations:^{
button.alpha = 0.0;
}];
将在一秒内逐渐将按钮的 alpha 更改为 0 (...WithDuration:1.0...
)。
有关详细信息,请参阅 The UIView
Class Reference。
希望这对您有所帮助。
试试这个
[button.layer removeAnimationForKey:@"opacity"];
[UIView animateWithDuration:1.0
delay:0.0
options: UIViewAnimationCurveLinear
animations:^{
self.roundButton.transform = CGAffineTransformMakeScale(50.0f, 50.0f);
self.roundButton.alpha = 0.0;
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
}
让我们稍微解释一下,在这个动画块中你说动画应该在一秒钟内发生,在这一秒钟内基于线性曲线(因此总是等于 increment/decrement)按钮应该缩放 50 倍并且 alpha 应该动画为 0。
我不明白你为什么要在那个简单的动画块上使用更复杂的 CAKeyframeAnimation
。
当您想要更强大的计时功能和一组值的动画时,关键帧动画很有用。
在我的项目中,我有一个 UIButton,单击它会缩放。到这里为止一切都很好,但我要求按钮的不透明度从 1 逐渐降低到 0。我的意思是它应该按 1,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0.0 顺序降低。我当前的代码是
-(void)roundButtonAnimation
{
self.buttonZoom = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
self.buttonZoom.duration = 0.8f;
self.buttonZoom.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(2,2,2)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(3,3,3)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(5,5,5)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(6,6,6)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(7,7,7)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(9,9,9)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(10,10,10)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(11,11,11)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(12,12,12)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(13,13,13)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(14,14,14)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(15,15,15)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(16,16,16)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(17,17,17)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(18,18,18)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(50,50,50)]];
self.buttonZoom.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
self.roundButton.transform = CGAffineTransformMakeScale(0,0);
self.roundButton.alpha = 0.3;
[self.roundButton.layer addAnimation:self.buttonZoom forKey:@"buttonScale"];
[self.CLSpinnerView1 stopAnimating];
//[self performSelector:@selector(secondView) withObject:self afterDelay:0.3];
[self performSelector:@selector(postNotification) withObject:self afterDelay:0.6];
}
UIView animateWithDuration:animations:
应该能满足您的需求。
[UIView animateWithDuration:1.0 animations:^{
button.alpha = 0.0;
}];
将在一秒内逐渐将按钮的 alpha 更改为 0 (...WithDuration:1.0...
)。
有关详细信息,请参阅 The UIView
Class Reference。
希望这对您有所帮助。
试试这个
[button.layer removeAnimationForKey:@"opacity"];
[UIView animateWithDuration:1.0
delay:0.0
options: UIViewAnimationCurveLinear
animations:^{
self.roundButton.transform = CGAffineTransformMakeScale(50.0f, 50.0f);
self.roundButton.alpha = 0.0;
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
}
让我们稍微解释一下,在这个动画块中你说动画应该在一秒钟内发生,在这一秒钟内基于线性曲线(因此总是等于 increment/decrement)按钮应该缩放 50 倍并且 alpha 应该动画为 0。
我不明白你为什么要在那个简单的动画块上使用更复杂的 CAKeyframeAnimation
。
当您想要更强大的计时功能和一组值的动画时,关键帧动画很有用。