5

在这段代码中,我使用的是动画。但有时我还需要更改图像的 alpha 值。

-(void)animation
{
    CGPoint point = CGPointMake(imgView.frame.origin.x, imgView.frame.origin.y);
    imgView.layer.position = point;
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position.x"];
    anim.fromValue  = [NSValue valueWithCGPoint:point];
    anim.toValue    = [NSValue valueWithCGPoint:CGPointMake(point.x + 50, point.y)];
    anim.duration   = 10.0f;
    anim.repeatCount =1;
    anim.removedOnCompletion = YES;

    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    [imgView.layer addAnimation:anim forKey:@"position.x"];
    imgView.layer.position = point;
}
4

5 回答 5

6

您可以使用 的opacityCABasicAnimation来执行此操作:

CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
alphaAnimation.fromValue = [NSNumber numberWithFloat:1.0];
alphaAnimation.toValue = [NSNumber numberWithFloat:0.0];
[imgView.layer addAnimation:alphaAnimation forKey:@"opacity"];

编辑:

要通过指定值设置动画,您可以使用CAKeyframeAnimation

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    animation.duration = 10.0f;
    animation.repeatCount = 1;
    animation.values  =  [NSArray arrayWithObjects:
                          [NSNumber numberWithFloat:1.0,
                          [NSNumber numberWithFloat:0.5],
                          [NSNumber numberWithFloat:1.0],nil];
    animation.keyTimes = [NSArray arrayWithObjects:
                          [NSNumber numberWithFloat:0.0],
                          [NSNumber numberWithFloat:5.0],
                          [NSNumber numberWithFloat:10.0], nil];
    [imgView.layer addAnimation:animation forKey:@"opacity"];
于 2013-09-06T12:38:28.783 回答
1

为了在视图的 x 位置移动的同时/速率降低 alpha,只需将位置设置代码与 alpha 设置代码放在同一动画块中,如下所示:

CGPoint finalPoint = CGPointMake(500, imgView.center.y); //change for any final point you want
CGFloat finalAlpha = 0.0; //change the final alpha as you want
UIView animateWithDuration:1.0 animations:^{ //change the duration as you want
   imgView.alpha = finalAlpha; 
   imgView.center = finalPoint;
}];
于 2013-09-06T13:04:09.407 回答
0

这会在 500 毫秒内“消失为隐形” self.view

您在调用它之前所做的任何事情都将“立即”设置,您在“动画”块中设置的所有内容(如设置新的帧坐标)都将在指定的持续时间内内插。

        [UIView animateWithDuration:0.50
                              delay:0.0
                            options:(   UIViewAnimationOptionAllowUserInteraction
                                     |  UIViewAnimationOptionBeginFromCurrentState
                                     |  UIViewAnimationOptionCurveEaseInOut)

                         animations:^ {
                             self.view.alpha = 0.0f ;
                         }

                         completion: ^(BOOL){
                             [self.view removeFromSuperview] ;
                             self.view = nil ;
                         }
         ] ;
于 2013-09-06T12:38:59.927 回答
0
于 2013-09-06T12:39:52.467 回答
0
//your code of changing x position will be here 

现在实现我写在下面的代码。

 UIView animateWithDuration:1.0 animations:^{
imgView.alpha = 0.5;//must be in between 0 to 1
 }];

现在,当您的图像视图移动到您所说的下一个位置时,请编写以下代码

//你的下一个位置的imageview代码..现在实现下面的代码......

 UIView animateWithDuration:0.5 animations:^{
imgView.alpha = 1.0;//must be in between 0 to 1
 }];

让我知道它是否有效!!!!希望能帮助到你...

快乐编码!!!

于 2013-09-06T12:37:29.237 回答