2013-10-29 78 views
0

我正在使用此代碼逐個加載橫幅圖像。水平滑動橫幅圖像

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];  
} 

但我需要在一段時間間隔之後水平地一個接一個地水平滑動圖像。我怎樣才能做到這一點?

回答

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

+0

thankyou iBhavesh..this是第二次你幫我:)謝謝你非常多 – rakesh

+0

你已經幫我在這個主題「如何通過從ios中的數據庫獲取數據填充數據到UItableView [暫停] 「 – rakesh

+0

okey :) ....... – rakesh

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]; 
} 
+0

它將水平滑動?? ...你嘗試過這?? – rakesh

+0

不,它會看起來像控制器呈現(類似於水平滑動)滑動實際效果的動畫,使用屏幕的單元大小製作全屏集合視圖,並在屏幕上使用簡單的pageControll ...並啓用分頁collectionView – nickAtStack

+0

okey thankyou非常多 – rakesh