2017-07-21 58 views
1

我想模擬左側的滑動以顯示在照片應用中看到的下一張照片以及其他位置,以便新照片從右向左滑動使用動畫。IOS/Objective-C:從右側滑動刷新更新視圖

但是,就我而言,該視圖有多張照片。它也有一個標題。

我可以在沒有動畫的情況下通過刷卡刷新照片和文字來完成此操作。這只是改變了屏幕上的照片和標題,但是,沒有左手動畫技術的權利。

有沒有辦法顯示舊的視圖移動到左側,而新的更新視圖從右側移入。

以下代碼將視圖移動到左側,但由於沒有放置任何位置,因此會將屏幕呈現爲黑色。

//In handleSwipe called by gesture recognizer 

    CGRect topFrame = self.view.frame; 
     topFrame.origin.x = -320; 
     [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ 
self.view.frame = topFrame; _myImage=newImage; 
_mycaption=newcaption;} completion:^(BOOL finished){ }]; 

感謝您的任何建議。

編輯: 有問題的視圖是一個細節視圖,你可以從tableview通過segue得到。它目前已經使用滾動視圖進行垂直滾動,因爲內容可以超過垂直維度中的單個屏幕。

我已經找到了一種方法來獲取舊的照片和標題左移和新的來自右側使用完成塊,但是,它不是令人滿意的,因爲有兩個行動之間的差距時屏幕是黑色的。這似乎是特有的完成方法,因爲新圖像在老圖像移動完成之前不會開始移動。

- (IBAction)swipedLeft:(id)sender 
    { 

    CGRect initialViewFrame = self.view.frame; 
    CGRect movedToLeftViewFrame = CGRectMake(initialViewFrame.origin.x - 375, initialViewFrame.origin.y, initialViewFrame.size.width, initialViewFrame.size.height); 
    CGRect movedToRightViewFrame = CGRectMake(initialViewFrame.origin.x + 375, initialViewFrame.origin.y, initialViewFrame.size.width, initialViewFrame.size.height); 

    [UIView animateWithDuration:0.1 
    animations:^{self.view.frame = movedToLeftViewFrame; 
//above moves old image out of view. 
} 
         completion:^(BOOL finished) 
     { 
      self.view.frame = movedToRightViewFrame; 
      [self loadNextItem];//changes pic and text. 
      [UIView animateWithDuration:0.1 
          animations:^{self.view.frame = initialViewFrame; 
//moves new one in 
} 
          completion:nil]; 
     }]; 
    } 

回答

2

三種方式來建立這個使用現有的組件:

  • UIPageViewController(見DuncanC的答案)。

  • UICollectionViewpagingEnabled設置爲true。使用全屏大小的單元格和UICollectionViewFlowLayout並將scrollDirection設置爲水平。

  • UICollectionView如上所述,但不是將pagingEnabled設置爲true,而是在您的委託中實施scrollViewWillEndDragging:withVelocity:targetContentOffset:。這使您可以在每個單元格之間都有一個裝訂線,同時仍然讓每個單元格填滿屏幕。 See this answer

+0

很好的答案。 (投票) –

+0

問題的視圖控制器是一個主表的詳細視圖,是一個UIViewController繼承自我創建的通用UIViewController,其中包含所有UIViewControllers使用的一些通用方法。另外,VC還有一些我需要的方法。你會建議我將它的形式更改爲UICollectionView或UIPageView。或者我應該延續到一個新的UICollectionView或UIPageView,然後加載顯示圖像和標題的UIVC? – user6631314

+0

'UICollectionView'是一個視圖,而不是視圖控制器。您可以將視圖控制器設置爲其數據源和委託。 –

4

我建議使用一個UIPageViewController,和管理每個圖像/組圖像作爲一個單獨的視圖控制器。

頁面視圖控制器支持頁面捲曲樣式轉換或幻燈片轉換。你會想要幻燈片轉換。

+0

從數據庫(核心數據)中可以得到數百個圖像和標題。我可以只有一個視圖控制器遍歷它們嗎? – user6631314

+0

事實上,您可以擁有數百個圖像是頁面視圖控制器的參數。它以與表格視圖管理單元格相同的方式管理視圖控制器。它只根據需要創建子視圖控制器。 –

+0

有一個與Xcode捆綁在一起的示例應用程序「PhotoScroller」,演示瞭如何使用頁面視圖控制器滾動高清晰度圖像。不幸的是,它很古老,用Objective-C編寫,所以它可能沒有那麼有用。 –

0

這是ScrollView + PageControl 我希望它能幫助你。

- (void)viewDidLoad { 
        [super viewDidLoad]; 
        self.scrollView = [[UIScrollView alloc] init]; 
        self.scrollView.delegate =self; 
        self.scrollView.translatesAutoresizingMaskIntoConstraints =NO; 
        self.scrollView.pagingEnabled =YES; 
      self.scrollView.contentSize =CGSizeMake(ViewWidth*VIewCount, ViewHeight); 
        [self.view addSubview:self.scrollView]; 


      self.pageControl = [[UIPageControl alloc] init]; 
       self. pageControl.translatesAutoresizingMaskIntoConstraints =NO; 
        [self.view addSubview:self.pageControl]; 
        self. pageControl.numberOfPages = VIewCount; 
        self.pageControl.currentPage = 0; 

        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView 
                                                               attribute:NSLayoutAttributeCenterX 
                                                               relatedBy:NSLayoutRelationEqual 
                                                                  toItem:self.view 
                                                               attribute:NSLayoutAttributeCenterX 
                                                              multiplier:1.0f 
                                                                constant:0.0f]]; 
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[self.scrollView(width)]" 
                                                                         options:0 
                                                                         metrics:@{@"width":@(ViewWidth)} 
                                                                           views:NSDictionaryOfVariableBindings(self.scrollView)]]; 
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[self.scrollView(height)]" 
                                                                          options:0 
                                                                          metrics:@{@"height":@(HEIGHT_VIEW)} 
                                                                            views:NSDictionaryOfVariableBindings(self.scrollView)]]; 
          
        
       
      
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_scrollView]-5-[pageControl(15)]" 
                                                                          options:NSLayoutFormatAlignAllCenterX 
                                                                          metrics:nil 
                                                                            views:NSDictionaryOfVariableBindings(self.scrollView, self.pageControl)]]; 
//[imgArr addObject:[UIImage imageNamed:...]]; 
        for (NSInteger index = 0; index<ViewCount; index++) { 
            UIImageView *addSubview = [[UIImageView alloc] initWithFrame:CGRectMake(index*ViewWidth, 0, ViewWidth, ViewHeight)]; 
    // addSubView.image = [imgArr objectAtIndex:index]; 
            [self.scrollView addSubview:addSubview]; 
        } 
    } 
      
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ 
        NSInteger currentOffset = scrollView.contentOffset.x; 
        NSInteger index = currentOffset/ViewWidth; 
          
        if (currentOffset % ViewWidth == 0) { 
            pageControl.currentPage = index; 
        } 

    }