找到你要的答案

Q:How to reduce the opacity of an UIButton gradually?

Q:如何降低一个UIButton透明度逐渐?

In my project i have an UIButton which on clicking zooms. Everything is fine till here but i require the opacity of the button to gradually decrease from 1 to 0. I mean it should decrease in 1,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0.0 sequence. My current code is

    -(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];
}

在我的项目中我有一个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];
}
answer1: 回答1:
[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!");
                     }];
}

Let's explain a little, in this animation block you say that the animation should take place in one second, during this second based on a linear curve (thus always equal increment/decrement) the button should zoom 50 times and alpha should be animate to 0.
I do not understand why you are using a more complicated CAKeyframeAnimation over that simple animation block.
Keyframe animation are useful when you want more power over timings and animate over a set of values.

[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!");
                     }];
}

Let's explain a little, in this animation block you say that the animation should take place in one second, during this second based on a linear curve (thus always equal increment/decrement) the button should zoom 50 times and alpha should be animate to 0.
I do not understand why you are using a more complicated CAKeyframeAnimation over that simple animation block.
Keyframe animation are useful when you want more power over timings and animate over a set of values.

answer2: 回答2:

UIView animateWithDuration:animations: should serve your purpose here.

[UIView animateWithDuration:1.0 animations:^{
    button.alpha = 0.0;
}];

Will gradually change the button's alpha to 0 over one second (...WithDuration:1.0...).

See the The UIView Class Reference for more information.

Hope this helps.

UIView animatewithduration:动画:在这里应该服务于你的目的。

[UIView animateWithDuration:1.0 animations:^{
    button.alpha = 0.0;
}];

将逐步改变按钮的α0在一个二(…期长:1…)。

看到UIView类的更多信息参考。

希望这有助于。

answer3: 回答3:

Try this

[button.layer removeAnimationForKey:@"opacity"];

Hope it helps.

试试这个

[button.layer removeAnimationForKey:@"opacity"];

希望它帮助。

ios  objective-c  iphone  uibutton  uianimation