像幻灯片一样从右到左动画图像

时间:2014-09-03 06:50:40

标签: ios image ipad slideshow

我已经完成了简单的动画,但想要像从滑块一样从左到右添加动画,我有图像数组,如何以编程方式设置动画,

  -(void) slideImages
{
    NSMutableArray *imagesArrays= [[NSMutableArray alloc]init];
    for (SliderDC *slider in imagesArray) {
        sliderImageName = slider.sliderImagePath;

     NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:@"%@%@",KWURL ,sliderImageName]]];

       UIImage *imgOne = [UIImage imageWithData:imageData];

    if(imgOne)
        [imagesArrays addObject:imgOne];
}

// after adding all image in array then add animation.
UIImageView* animatedImageView;
 if ([[UIScreen mainScreen] bounds].size.height == 568){
      animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, 320, 150)];
 }
 else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
 {
    animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 98, 768, 228)];
 }
else
{
 animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, 320, 118)];
}
animatedImageView.animationImages = imagesArrays;
animatedImageView.animationDuration = 20.0f;
animatedImageView.animationRepeatCount = 0;
[self.view addSubview: animatedImageView];
[animatedImageView startAnimating];

}

3 个答案:

答案 0 :(得分:2)

您只需使用DRDynamicSlideShow

即可

创建漂亮,动画,分页UIScrollViews。在UIScrollView被分页时轻松为视图设置动画。这非常适合iOS应用程序中的欢迎屏幕和介绍视图。

功能

每个动画

1行代码。

任何类型的值都可以设置动画。

块驱动。

答案 1 :(得分:2)

试试这个..

 -(void) slideImages
  {
    NSMutableArray *imagesArrays= [[NSMutableArray alloc]init];
    for (SliderDC *slider in imagesArray) {
        sliderImageName = slider.sliderImagePath;

     NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:@"%@%@",KWURL ,sliderImageName]]];

       UIImage *imgOne = [UIImage imageWithData:imageData];

    if(imgOne)
        [imagesArrays addObject:imgOne];
  }


- (IBAction)startAnimation:(UIButton *)sender
{
    slideTransition = [CATransition animation]; // CATransition * slideTransition; instance variable 
    slideTransition.duration = 0.35;
    slideTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    slideTransition.type = kCATransitionPush;
    slideTransition.delegate = self;
    NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:0.50 target:self selector:@selector(slideShow) userInfo:nil repeats:YES];
    [timer fire];
}
-(void)slideShow
{
    slideTransition.subtype =kCATransitionFromLeft; // or kCATransitionFromRight
    [animatedImageView.layer addAnimation:slideTransition forKey:nil];

    if (index < imagesArrays.count-1) // NSUInteger  index; instance variable
    {
        index++;

    }
    else
    {
        index=0;
    }
    animatedImageView.image =[imagesArrays objectAtIndex:index];

}

答案 2 :(得分:1)

-(void) slideImages
{
NSMutableArray *imagesArrays= [[NSMutableArray alloc]init];
for (SliderDC *slider in imagesArray) {
    sliderImageName = slider.sliderImagePath;

 NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:@"%@%@",KWURL ,sliderImageName]]];

   UIImage *imgOne = [UIImage imageWithData:imageData];

if(imgOne)
    [imagesArrays addObject:imgOne];
}



UIImageView* animatedImageView;
 if ([[UIScreen mainScreen] bounds].size.height == 568){
  animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, 320, 150)];
 }
 else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
 {
  animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 98, 768, 228)];
 }
 else
 {   
 animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, 320, 118)];
 }

[self.view addSubview: animatedImageView];

self.animatedImageView.animationImages = imagesArrays;

self.animatedImageView.animationDuration=1.0;
self.animatedImageView.animationRepeatCount=1;
self.animatedImageView.image = [self.imageViewAnimation.animationImages lastObject]; //to stop animation at last image.
[self.animatedImageView startAnimating];