2016-03-15 55 views
2

在我的應用程序中使用UIControlpage。加載圖像併成功顯示在頁面控制中。但我想顯示圖像動畫像從右到左移動。使用定時器調用頁面控制並增加陣列數量。UIpagecontrol需要動畫Rigth到左邊

////使用NSTimer調用函數來顯示動畫。

[NSTimer scheduledTimerWithTimeInterval:5.0 
            target:self 
            selector:@selector(loadNextController) 
            userInfo:nil 
            repeats:YES]; 

ScrollViewPage = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, frameX, frameY)]; 
ScrollViewPage.pagingEnabled = YES; 
ScrollViewPage.backgroundColor=[UIColor redColor]; 
ScrollViewPage.delegate = self; 
ScrollViewPage.backgroundColor = [UIColor clearColor]; 
ScrollViewPage.contentSize = CGSizeMake(frameX, frameY); 
ScrollViewPage.showsHorizontalScrollIndicator = NO; 


ScrollViewPage.pagingEnabled = YES; 
pageControl = [[UIPageControl alloc] init]; 
pageControl.frame = CGRectMake(100,self.view.frame.size.height-100,self.view.frame.size.width-200,100); 
pageControl.numberOfPages = [_pageImages count]; 
pageControl.currentPage = 0; 

[self.view addSubview: ScrollViewPage]; 

for(int i = 0; i < [_pageImages count]; i++) 
    { 

     NSURL *url = [NSURL URLWithString:[_pageImages objectAtIndex:i]]; 
     NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 
     UIImage *tmpImage = [UIImage imageWithData:data]; 
     _backgroundImageView = [[UIImageView alloc] initWithImage:tmpImage]; 
     _backgroundImageView.frame = CGRectMake(frameX * i, 0.0, frameX, frameY); 
     [ScrollViewPage addSubview:_backgroundImageView]; 


    } 

ScrollViewPage.contentSize = CGSizeMake(frameX*[_pageImages count], frameY); 
pageControl.backgroundColor=[UIColor orangeColor]; 
[self.view addSubview:pageControl]; 
[pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged]; 

/////這裏所說的定時器功能

- (void)loadNextController 
{ 
    pageControl.currentPage = (pageControl.currentPage+1)%self.pageImages.count; 
    ScrollViewPage.contentOffset = CGPointMake(pageControl.currentPage * self.view.bounds.size.width, 0); 
} 

在這裏,我需要顯示留給了uipagecontrol正確的舉措。幫助我..

回答

1

一般而言,您應該始終寫出您當前的結果,期望的結果以及您嘗試的結果。

我只能猜測你面臨的問題是沒有動畫。 滾動視圖有一個方法setContentOffset:animated,您可以使用該方法爲轉換設置動畫。另一種方法是使用UIViewanimateWithDuration並設置方法塊中的偏移量。

嘗試這些...

[ScrollViewPage setContentOffset:CGPointMake(pageControl.currentPage * self.view.bounds.size.width, 0) animated:YES]; 

或者

[UIView animateWithDuration:0.2 animations:^{ 
     ScrollViewPage.contentOffset = CGPointMake(pageControl.currentPage * self.view.bounds.size.width, 0); 
}]; 

編輯任何或全部:循環圖像。

要循環顯示圖像,您當時只需要在滾動視圖上顯示2張圖像。我會用3給你看這個概念,你可以嘗試和它一起玩。

NSArray *allImages; 
NSInteger currentIndex = 0; 
UIScrollView *scrollView; 
UIImageView *imageViews[3]; 

- (void)begin { 

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*3.0, scrollView.frame.size.height); // always have 3 images at the time 
    scrollView.contentOffset = CGPointMake(scrollView.frame.size.width, 0.0); // put to center 

    for(int i=0; i<3; i++) { 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(scrollView.frame.size.width*i, 0.0, scrollView.frame.size.width, scrollView.frame.size.height)]; 
     [scrollView addSubview:imageView]; 
     imageView[i] = imageView; 
    } 
    [self layoutForIndex:currentIndex]; 
} 
- (void)nextImage { 
    currentIndex++; 
    [self layoutForIndex:currentIndex]; 

    scrollView.contentOffset = CGPointZero; 
    [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0.0) animated:YES] 
} 

- (void)layoutForIndex:(NSInteger)index { 
    if(allImages.count > 0) { // we need at least one image to do something 
     for(int i=0; i<3; i++) { 
      // do the circling 
      NSInteger imageIndex = index-1+i; 
      while(imageIndex < 0) imageIndex+=allImages.count; 
      while(imageIndex >= allImages.count) imageIndex-=allImages.count; 

      imageViews[i].image = imageViews[imageIndex]; 
     } 
    } 
} 

但是一般來說,對於你在做什麼,你甚至不需要滾動視圖。接下來,您可以簡單地在當前旁邊添加另一個圖像視圖,然後使用動畫重新定位這兩個圖像視圖。

+0

非常感謝你..爲我工作.. –

+0

一個疑問..顯示[_ImageImages數組]中的所有圖像後,如何移動uipagecontrol中的第一個圖像沒有動畫。這意味着所有圖像都會返回並轉到第一張圖像。我需要它自動來到第一個圖像。幫助我... –

+0

我爲此添加了這個概念。我希望你能理解它。 –