如何增加和减less图像的alpha值与animation在iOS SDK?
在这个代码中,我使用的是animation。但是,我一次也需要改变图像的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; }
你可以使用CABasicAnimation
的opacity
键来做到这一点:
CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; alphaAnimation.fromValue = [NSNumber numberWithFloat:1.0]; alphaAnimation.toValue = [NSNumber numberWithFloat:0.0]; [imgView.layer addAnimation:alphaAnimation forKey:@"opacity"];
编辑:
为了通过指定的值animation,您可以使用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"];
为了减less你的视图的x位置相同的时间/速率alpha,只需将位置设置代码和alpha设置代码放在同一个animation块中,如下所示:
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; }];
这在500ms内“隐形” self.view
。
你在调用之前所做的任何事情都将被设置为“立即”,在“animation”块中设置的所有内容(例如设置新的帧坐标)将在指定的时间内进行插值。
[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 ; } ] ;
你可以使用一个CABasicAnimation
对象(一个不同的对象,因为你将animation一个不同的KeyPath,也就是alpha
),并使用fromValue
和fromValue
NSNumbers
。
另一种(也是最简单的)方法是使用专用于animation的UIView
辅助方法( animateWithDuration:…
和all),特别是如果必须同时为多个属性设置animation效果(可以使用相同的animation为frame
和alpha
animation效果如果它适合您的需求并简化您的代码,则会阻止)。
你也可以混合使用CABasicAnimation
作为框架和[UIView animateWithDuration:…]
,不pipe你需要什么样的组合,取决于你的animation是否复杂,需要自定义(自定义定时function,非线性animation等)。
//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 }];
让我知道它是否工作! 希望能帮助到你…
快乐编码!