如何在ios sdk中使用动画增加和减少图像alpha值? [英] how to increase and decrease the image alpha value with animation in ios sdk?

查看:195
本文介绍了如何在ios sdk中使用动画增加和减少图像alpha值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In this code i am using animation.But at a time i need to change the image alpha value also.

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


推荐答案

您可以使用不透明度 CABasicAnimation 的键用于执行此操作:

You can use the opacity key of CABasicAnimation for doing this:

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

修改:

要通过指定值制作动画,您可以使用 CAKeyframeAnimation

For animating through specified values you can use 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"];

这篇关于如何在ios sdk中使用动画增加和减少图像alpha值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆