xcode iphone 图像褪色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5618964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iphone fading of images
提问by kingston
wat i want is to show an image in image view and fade the image after 5 seconds and show the next image .the images are stored in an array objects...i was able to make a successful transition between images after 5 seconds.what i want is to fadeout the first image after 5 seconds and show the next one this got to happen in an infinite loop...below is the code and it works really good for normal transition between images..wat i want is fading of images...and were to add fading transition in the below code
我想要的是在图像视图中显示图像并在 5 秒后淡入图像并显示下一张图像。图像存储在数组对象中......我能够在 5 秒后在图像之间成功转换。什么我想要的是在 5 秒后淡出第一张图像并显示下一张图像,这将在无限循环中发生......下面是代码,它对于图像之间的正常过渡非常有效......我想要的是图像的褪色...并在下面的代码中添加淡入淡出过渡
- (void)viewDidLoad {
imgView.animationImages=[NSArray arrayWithObjects:
[UIImage imageNamed:@"lori.png"],
[UIImage imageNamed:@"miranda.png"],
[UIImage imageNamed:@"taylor.png"],
[UIImage imageNamed:@"ingrid.png"],
[UIImage imageNamed:@"kasey.png"],
[UIImage imageNamed:@"wreckers.png"], nil];
imgView.animationDuration=20.0;
imgView.animationRepeatCount=0;
[imgView startAnimating];
[self.view addSubview:imgView];
[imgView release];
[super viewDidLoad];
}
采纳答案by Sabobin
You can use a repeating NSTimer to call a special method which animates the image views alpha property from 0.0 to 1.0 continuously. Here is some code I have written which works:
您可以使用重复的 NSTimer 来调用一个特殊的方法,该方法将图像视图 alpha 属性从 0.0 连续动画到 1.0。这是我编写的一些有效的代码:
- (void)viewDidLoad {
imgView.animationImages=[NSArray arrayWithObjects:
[UIImage imageNamed:@"lori.png"],
[UIImage imageNamed:@"miranda.png"],
[UIImage imageNamed:@"taylor.png"],
[UIImage imageNamed:@"ingrid.png"],
[UIImage imageNamed:@"kasey.png"],
[UIImage imageNamed:@"wreckers.png"], nil];
imgView.animationDuration=20.0;
imgView.animationRepeatCount=0;
[imgView startAnimating];
[self.view addSubview:imgView];
[imgView release];
//The timers time interval is the imageViews animation duration devided by the number of images in the animationImages array. 20/5 = 4
NSTimer *timer = [NSTimer timerWithTimeInterval:4.0
target:self
selector:@selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer fire];
[super viewDidLoad];
}
//It is important that the animation durations within these animation blocks add up to 4
//(the time interval of the timer). If you change the time interval then the time intervals
//in these blocks must also be changed to refelect the amount of time an image is displayed.
//Failing to do this will mean your fading animation will go out of phase with the switching of images.
-(void)onTimer{
[UIView animateWithDuration:3.0 animations:^{
imgView.alpha = 0.0;
}];
[UIView animateWithDuration:1.0 animations:^{
imgView.alpha = 1.0;
}];
}
If you want the fade duration to last for 5 seconds instead of 4, you need to increase your image views animationDuration property to 25, and then increase the fade animation durations in the two blocks such that the sum fade time = 5.
如果您希望淡入淡出持续时间持续 5 秒而不是 4 秒,您需要将图像视图 animationDuration 属性增加到 25,然后增加两个块中的淡入淡出动画持续时间,使淡入淡出时间总和 = 5。
回答by Sabobin
@user652878 This answeres your question regarding fading seemlessly from one image to the next:
@user652878 这回答了您关于从一个图像无缝地淡入到下一个图像的问题:
Create the following properties in your view controllers header:
在您的视图控制器标头中创建以下属性:
UIImageView *imageViewBottom, *imageViewTop;
NSArray *imageArray;
Then add this code to your implimentaion file:
然后将此代码添加到您的实现文件中:
int topIndex = 0, prevTopIndex = 1;
- (void)viewDidLoad {
imageViewBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:imageViewBottom];
imageViewTop = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:imageViewTop];
imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:@"lori.png"],
[UIImage imageNamed:@"miranda.png"],
[UIImage imageNamed:@"taylor.png"],
[UIImage imageNamed:@"ingrid.png"],
[UIImage imageNamed:@"kasey.png"],
[UIImage imageNamed:@"wreckers.png"], nil];
NSTimer *timer = [NSTimer timerWithTimeInterval:5.0
target:self
selector:@selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer fire];
[super viewDidLoad];
}
-(void)onTimer{
if(topIndex %2 == 0){
[UIView animateWithDuration:5.0 animations:^
{
imageViewTop.alpha = 0.0;
}];
imageViewTop.image = [imageArray objectAtIndex:prevTopIndex];
imageViewBottom.image = [imageArray objectAtIndex:topIndex];
}else{
[UIView animateWithDuration:5.0 animations:^
{
imageViewTop.alpha = 1.0;
}];
imageViewTop.image = [imageArray objectAtIndex:topIndex];
imageViewBottom.image = [imageArray objectAtIndex:prevTopIndex];
}
prevTopIndex = topIndex;
if(topIndex == [imageArray count]-1){
topIndex = 0;
}else{
topIndex++;
}
}
回答by serk01
Here's what worked for me when I wanted to animate a set of messages as images to the user. No matter how many images you have this code will rotate through them.
当我想将一组消息动画化为用户的图像时,这对我有用。无论您拥有多少张图像,此代码都会在它们之间旋转。
[self.messageBottom = [[UIImageView alloc] initWithFrame:CGRectMake(289, 262, 171, 38)];
[self addSubview:messageBottom];
[messageBottom setAlpha:0.0];
[messageBottom release];
self.messageTop = [[UIImageView alloc] initWithFrame:CGRectMake(289, 262, 171, 38)];
[self addSubview:messageTop];
[messageTop setAlpha:1.0];
[messageTop release];
self.messageImgsArray =[NSArray arrayWithObjects:
[UIImage imageNamed:@"image1.png"],
[UIImage imageNamed:@"image2.png"],
[UIImage imageNamed:@"image3.png"],
[UIImage imageNamed:@"image4.png"],
[UIImage imageNamed:@"image5.png"],nil];
topIndex = 0, bottomIndex = 1;
[messageTop setImage:[messageImgsArray objectAtIndex:topIndex]];
//The timers time interval is the imageViews animation duration devided by the number of images in the animationImages array. 20/5 = 4
NSTimer *timer = [NSTimer timerWithTimeInterval:2.0
target:self
selector:@selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
-(void)onTimer{
[UIView animateWithDuration:1 animations:^{
//set the image of the image that is not currently visible
if (messageTop.alpha == 0) {
[messageTop setImage:[messageImgsArray objectAtIndex:topIndex]];
}
if (messageBottom.alpha == 0) {
[messageBottom setImage:[messageImgsArray objectAtIndex:bottomIndex]];
}
//make sure the images rotate
[messageTop setAlpha:messageTop.alpha == 0 ? 1 : 0];
[messageBottom setAlpha:messageBottom.alpha == 0 ? 1 : 0];
//make sure the images play in a loop
topIndex = topIndex < [messageImgsArray count]-1 ? bottomIndex+1 : 0;
bottomIndex = bottomIndex < [messageImgsArray count]-1 ? bottomIndex+1 : 0;
}];
}