Ios 在XCode 6中实现淡入淡出幻灯片&;损失7

Ios 在XCode 6中实现淡入淡出幻灯片&;损失7,ios,xcode,ipad,ios7,xcode6,Ios,Xcode,Ipad,Ios7,Xcode6,我在开发一个应用程序的过程中,我正在寻找一种方法来添加一个淡入淡出的幻灯片类型的元素到应用程序中 我尝试了一些通过google和Stack Exchange找到的用户建议和答案,但是其中有很多都很容易出错或者不符合我的需要 我非常喜欢我在GitHub上看到的一个名为JBKenBurns的项目,但是当我尝试集成它时,我得到了以下崩溃日志: 2014-12-31 23:36:17.164 Sir John A.[686:60b] -[UIView animateWithImages:transiti

我在开发一个应用程序的过程中,我正在寻找一种方法来添加一个淡入淡出的幻灯片类型的元素到应用程序中

我尝试了一些通过google和Stack Exchange找到的用户建议和答案,但是其中有很多都很容易出错或者不符合我的需要

我非常喜欢我在
GitHub
上看到的一个名为
JBKenBurns
的项目,但是当我尝试集成它时,我得到了以下崩溃日志:

2014-12-31 23:36:17.164 Sir John A.[686:60b] -[UIView animateWithImages:transitionDuration:initialDelay:loop:isLandscape:]: unrecognized selector sent to instance 0x176ae980
2014-12-31 23:36:17.170 Sir John A.[686:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView animateWithImages:transitionDuration:initialDelay:loop:isLandscape:]: unrecognized selector sent to instance 0x176ae980'

*** First throw call stack:
(0x2de14f4b 0x385f16af 0x2de188e7 0x2de171cb 0x2dd664d8 0xe6f81 0x305a212b 0x30651eb3 0x305a212b 0x3060802f 0x30607fb9 0x3058020f 0x2dde01cd 0x2ddddb71 0x2ddddeb3 0x2dd48c27 0x2dd48a0b 0x32a29283 0x305ec049 0xe9f3d 0x38af9ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
我已经测试了他们的演示,效果很好,但是当我根据他们的指令实现它时,我得到了崩溃日志

我不打算使用这个解决方案,但如果能让它正常工作就好了

我还尝试将
UIImageView
添加到
UIViewController
并使用以下方法:

self.imagePlaceholder.animationImages = [NSArray arrayWithObjects:
                                 [UIImage imageNamed:@"1.png"],
                                 [UIImage imageNamed:@"2.png"],
                                 [UIImage imageNamed:@"3.png"], nil];
self.imagePlaceholder.animationDuration = 2.00; //1 second
self.imagePlaceholder.animationRepeatCount = 0; //infinite

[self.imagePlaceholder startAnimating]; //start the animation
这个解决方案看起来很有希望,但是这些图像不会相互消失,只是每隔一秒左右就会发生剧烈的变化


如果有人能帮我让JBKenBurns解决方案工作,或者帮我为每个图像添加平滑淡入淡出,那就太棒了。

通过在动画块中调用setImage,您可以将淡入淡出的图像与transitionWithView:duration:options:animations:completion:交叉。这样应该行得通

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pics = @[@"img1.JPG", @"img2.JPG", @"img3.JPG", @"img4.JPG", @"img5.JPG", @"img6.JPG", @"img7.JPG"];
    [self changeImage];
}


-(void)changeImage {
    static int counter = 0;
    [UIView transitionWithView:self.imageView
                      duration:1.0
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        UIImage *img = [UIImage imageNamed:self.pics[counter]];
                        [self.imageView setImage:img];
                        counter ++;

                    } completion:^(BOOL finished) {
                        if (counter != self.pics.count) {
                            [self performSelector:@selector(changeImage) withObject:nil afterDelay:2];
                        }
                    }];
}

幻灯片放映让我们尝试一下,你也可以通过使用基本动画来执行动画,例如,在计时器的帮助下,当你想停止动画时,你可以像下面这样做,只是使计时器无效

注意:这只是另一种设置动画的方法

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.

  _imagePlaceholder = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width, self.view.bounds.size.height)]; //this is image view
  _imagesArray = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"image_1.jpg"],[UIImage imageNamed:@"image_2.jpg"],[UIImage imageNamed:@"image_3.jpg"],[UIImage imageNamed:@"image_4.jpg"], nil]; //images array to hold images

  [self.view addSubview:_imagePlaceholder];

  //next set up timer to change image at each intervel
  imageIndex = 0; //initially image index will be 0
  _imagePlaceholder.image = [_imagesArray objectAtIndex:imageIndex]; //initially image view contains first image
  _animTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];

 }

 - (void)changeImage {

  //get new index and change it
  if(imageIndex < [_imagesArray count] - 1)
  {
     imageIndex++;
  }
  else
  {
     imageIndex = 0;
  }

  UIImage *nextImage = [_imagesArray objectAtIndex:imageIndex];

  CABasicAnimation *crossFadeAnimation = [CABasicAnimation animationWithKeyPath:@"contents"];
   crossFadeAnimation.duration = 2.0; //this is lesser than timer duration 
  _imagePlaceholder.layer.contents = (id)[nextImage CGImage];
  [_imagePlaceholder.layer removeAnimationForKey:@"animateImages"];
  [_imagePlaceholder.layer addAnimation:crossFadeAnimation forKey:@"animateImages"];
  _imagePlaceholder.image = nextImage;
 }
-(void)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
_imagePlaceholder=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height)];//这是图像视图
_imagesArray=[[NSMutableArray alloc]initWithObjects:[UIImage ImageName:@“image_1.jpg],[UIImage ImageName:@“image_2.jpg”],[UIImage ImageName:@“image_3.jpg”],[UIImage ImageName:@“image_4.jpg”],nil];//用于保存图像的图像数组
[self.view addSubview:_imagePlaceholder];
//下一步设置定时器以在每个间隔更改图像
imageIndex=0;//最初图像索引将为0
_imagePlaceholder.image=[\u imagesArray objectAtIndex:imageIndex];//最初图像视图包含第一个图像
_animTimer=[NSTimer scheduledTimerWithTimeInterval:3目标:自选择器:@selector(changeImage)userInfo:nil repeats:YES];
}
-(无效)更改图像{
//获取新索引并更改它
如果(图像索引<[\u图像射线计数]-1)
{
imageIndex++;
}
其他的
{
imageIndex=0;
}
UIImage*nextImage=[\u imagesArray objectAtIndex:imageIndex];
CABasicAnimation*crossFadeAnimation=[CABasicAnimation animationWithKeyPath:@“内容”];
crossFadeAnimation.duration=2.0;//这小于计时器持续时间
_imagePlaceholder.layer.contents=(id)[nextImage CGImage];
[_imagePlaceholder.layer removeAnimationForKey:@“animateImages”];
[_imagePlaceholder.layer addAnimation:crossFadeAnimation forKey:@“animateImages”];
_imagePlaceholder.image=nextImage;
}

UIImageView.animationImages用于类似精灵的动画图像循环。这就是为什么这些变化是残酷的。你需要肯·伯恩斯效应吗?还是只是褪色?如果你需要的只是淡入淡出,只需使用两个ImageView并在UIView动画循环中更改它们的alpha值。Rakeshbs,这现在更有意义了。我对iOS开发还比较陌生,所以有很多事情我还不知道。我希望我已经把事情说清楚了。试着阅读苹果的文档,效果非常好!我不得不在.h文件中做一些更改,但这显然是意料之中的。有没有办法设置动画完成所需的持续时间?现在图像会褪色,但转换只需要大约八分之一秒。如果可能的话,我想把持续时间增加到1-2秒左右。编辑:我可以通过添加“crossfade.duration=1.0”来增加过渡的持续时间;u可以设置动画持续时间,但计时器持续时间必须大于动画持续时间。。任何方式编辑的持续时间