objective-c - 如何在幻灯片放映中优雅地转换图像?

标签 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++;
}

最佳答案

您有很多选择。如果你有一个“容器” View ,你可以使一个新的 UIImageView 透明(alpha = 0),然后使用一个 UIView 动画 block 使一个图像淡入和另一个淡出(或者在两个上都保留 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

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

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

关于objective-c - 如何在幻灯片放映中优雅地转换图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11855000/

相关文章:

iphone - 将 UIView/UIWindow 中的特定触摸事件传递给下一个响应者(sendEvent :/hitTest: doesn't work )

iOS tableviewcell 覆盖

ios - 在主视图 Controller 中单击后更新外部 View Controller 中的 UITextField

ios - 如何在 MKMapView iOS 中显示多个注释?

javascript - 使用 merge-images.js 合并图像 1) 未正确放置,2) 在 chrome 上出现 "tainted"错误

c# - 内存不足 Image.FromFile

iphone - 如何正确设置 UIAccessibilityElement 的 accessibilityFrame?

ios - NSInvalidArgumentException',reas-[__NSPlaceholderDictionary initWithObjects :forKeys:count:]: attempt to insert nil object from objects[4]'

ios - 当用户按下图标而不是警报时处理本地通知

image - Symfony2 Assets : How to display uploaded image which path is stored in database?