水平滑动横幅图像

时间:2013-10-29 09:32:18

标签: ios

我正在使用此代码逐个加载横幅图片。

 NSArray *imagesArray;
 int photoCount;
 UIImageView *imageView;

-(void)setupImageView{
     photoCount = 0;
     [[NSArray alloc]initWithObjects:[UIImage imageNamed:@"imgA.png"] ,
                                     [UIImage imageNamed:@"imgB.png"] ,
                                     [UIImage imageNamed:@"imgC.png"] ,
                                     [UIImage imageNamed:@"imgD.png"] ,
                                     [UIImage imageNamed:@"imgE.png"] , 
                                     nil];
     imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
     imageView.image = [imagesArray objectAtIndex:photoCount];
     [self.view addSubview:imageView];
     [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(transitionPhotos) userInfo:nil repeats:YES];
 }

-(void)transitionPhotos{
    if (photoCount < [imagesArray count] - 1){
        photoCount ++;
    }else{
        photoCount = 0;
    }
    [UIView transitionWithView:self.imageView
                  duration:2.0
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{ imageView.image = [imagesArray objectAtIndex:photoCount]; }
                completion:NULL];    
}

但是我需要在一段时间间隔后一个接一个地水平滑动图像。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:5)

编辑:

创建一个名为“ImageSliderViewController”的新视图控制器,并在ImageSliderViewController.m文件中复制此代码,并在项目中添加附加的图像。

#import "ImageSliderViewController.h"

@interface ImageSliderViewController ()
{
    int intMaxLength,intStartPosition;
    UIScrollView *imageSlider;
    NSTimer *timerForImageSlider;
}
@end

@implementation ImageSliderViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    imageSlider = [[UIScrollView alloc]initWithFrame:CGRectMake(25, 20, 270, 180)];
    [self.view addSubview:imageSlider];

    [imageSlider setContentOffset:CGPointMake(0, 0) animated:YES];
    [imageSlider setShowsHorizontalScrollIndicator:NO];
    [imageSlider setScrollEnabled:NO];

    NSArray *imagesArray = [[NSArray alloc]initWithObjects:[UIImage imageNamed:@"1.png"] ,
                            [UIImage imageNamed:@"2.png"] ,
                            [UIImage imageNamed:@"3.png"] ,
                            [UIImage imageNamed:@"4.png"] ,
                            nil];

    [imageSlider setContentSize:CGSizeMake([imagesArray count]*270, 180)];

    intMaxLength = [imagesArray count] * 270;


    int xPosition = 0;

    for(int i = 0 ; i < [imagesArray count] ; i++)
    {
        UIImageView *ivImage = [[UIImageView alloc] initWithFrame:CGRectMake(xPosition, 0, 270, 180)];

        ivImage.image = [imagesArray objectAtIndex:i];

        [imageSlider addSubview:ivImage];

        xPosition += 270;
    }

    intStartPosition = 0;

    UISwipeGestureRecognizer *recognizer;

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [[self view] addGestureRecognizer:recognizer];

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [[self view] addGestureRecognizer:recognizer];

    timerForImageSlider = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(imageSlider) userInfo:nil repeats:YES];
}

-(IBAction)handleSwipeFrom:(UISwipeGestureRecognizer*)sender
{
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        if(intMaxLength <=intStartPosition+270 )
        {
            return;
        }
        else
        {
            intStartPosition += 270;
            [imageSlider setContentOffset:CGPointMake(intStartPosition,0) animated:YES];
        }

    }
    else if(sender.direction == UISwipeGestureRecognizerDirectionRight)
    {
        if(0 > intStartPosition-270 )
        {
            return;
        }
        else
        {
            intStartPosition -= 270;
            [imageSlider setContentOffset:CGPointMake(intStartPosition,0) animated:YES];
        }

    }
}
-(void)imageSlider
{
    if(intMaxLength <=intStartPosition+270 )
    {
        [imageSlider setContentOffset:CGPointMake(0,0) animated:YES];
        intStartPosition = 0;
    }
    else
    {
        intStartPosition += 270;
        [imageSlider setContentOffset:CGPointMake(intStartPosition,0) animated:YES];
    }

}
@end

图片在这里::::

enter image description here

enter image description here

enter image description here

enter image description here

答案 1 :(得分:0)

试试这个:

//in [self viewDidLoad]
height = [UIScreen mainScreen].bounds.size.height;
width = [UIScreen mainScreen].bounds.size.width;
.
.
.
void)transitionPhotos{
        if (photoCount < [imagesArray count] - 1){
        photoCount ++;
        }else{
        photoCount = 0;
        }
        yourImageView.frame = CGRectMake(width, 0, 0, height);
        [UIView beginAnimations:Nil context:Nil];
        [UIView setAnimationDuration:1];
        [UIView setAnimationDelegate:self];
        yourImageView.frame = CGRectMake(0, 0, width, height)];;
        [UIView commitAnimations];
}