如何在幻灯片放映中优雅地过渡图像?

时间:2012-08-07 22:25:10

标签: objective-c ios image uiview

经过一些研究,我找到了一个在iPhone应用程序中创建图像幻灯片放映的解决方案。一切正常,目前图片一个接一个地展示。

我的问题是,我可以让图像交叉溶解/淡化而不仅仅是出现,如果可以,我可以就此提出一些建议。

我的代码

的.m

 }
 int topIndex = 0, prevTopIndex = 1; 
 - (void)viewDidLoad
 {


imagebottom = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,160,240)];
[self.view addSubview:imagebottom];

imagetop = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,160,240)];
[self.view addSubview:imagetop];

imageArray = [NSArray arrayWithObjects:
              [UIImage imageNamed:@"image1.png"],
              [UIImage imageNamed:@"image2.png"],
              [UIImage imageNamed:@"image3.png"],
              [UIImage imageNamed:@"ip2.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:^
     {
         imagebottom.alpha = 0.0;
     }];
    imagetop.image = [imageArray objectAtIndex:prevTopIndex];
    imagetop.image = [imageArray objectAtIndex:topIndex];
}else{
    [UIView animateWithDuration:5.0 animations:^
     {
         imagetop.alpha = 1.0;
     }];
    imagetop.image = [imageArray objectAtIndex:topIndex];
    imagebottom.image = [imageArray objectAtIndex:prevTopIndex];
}
prevTopIndex = topIndex;
if(topIndex == [imageArray count]-1){
    topIndex = 0;
}else{
    topIndex++;
}

1 个答案:

答案 0 :(得分:2)

你有很多选择。如果你有一个“容器”视图,你可以使一个新的UIImageView透明(alpha = 0),然后使用UIView动画块将一个图像淡入淡出另一个图像(或者在两个图像上留下alpha = 1并从中滑入一个你想要的任何一方。

例如,您有自己的主视图self.view。你有一个UIImageView * oldView,现在是在rect(0,0,320,100),你想在滑动newView imageView时向右滑动。首先你将newView框架设置为(-320,0,320,100)然后[self .view addSubview newView]。为变化添加动画:

[UIView animateWithDuration:2 animations:^
  {
     oldView.frame = CGRectMake(320, 0, 320, 100);
     newView.frame = CGRectMake(0,0,320, 100);
  }
completion:^(BOOL finished)
  {
    [oldView removeFromSuperView];
  } ];

您还可以选择使用UiView的

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion

为您提供更多/不同的选项(而且它的工作量也减少了!)。例如,在第一个示例中使用相同的基本对象,但newView与oldView具有相同的框架:

transitionFromView:oldView toView:newView duration:2 options: UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished)) { /*whatever*/}];