【问题标题】:IOS/Objective-C: Slide updated view in from right on swipeIOS/Objective-C:在滑动时从右侧滑入更新的视图
【发布时间】:2017-07-21 15:19:36
【问题描述】:

我想模拟向左滑动以显示在照片应用程序中看到的下一张图片,以便新图片使用动画从右向左滑入。

但是,就我而言,该视图包含不止一张照片。它还有一个标题。

只需在滑动时更新照片和文字,我就可以在没有动画的情况下做到这一点。这只是改变了屏幕上的照片和标题,但是没有从右到左的动画技术。

有没有办法显示旧视图向左移动,而新的更新视图从右侧移动。

以下内容将视图向左移动,但由于没有放置任何内容,因此将屏幕呈现为黑色。

//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){ }];

感谢您的任何建议。

编辑: 有问题的视图是您通过 segue 从 tableview 获得的详细视图。它目前已经使用滚动视图进行垂直滚动,因为内容可以在垂直维度上超过单个屏幕。

我找到了一种方法让旧照片和标题向左移动,而新照片和标题使用完成块从右边进入,但是,它不太令人满意,因为当两个动作之间存在间隙时屏幕是黑色的。这似乎是完成方法特有的,因为新图像在旧图像完成移动之前不会开始移动。

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

【问题讨论】:

    标签: ios objective-c animation


    【解决方案1】:

    我建议使用UIPageViewController,并将每个图像/图像集作为单独的视图控制器进行管理。

    页面视图控制器支持页面卷曲样式转换或幻灯片转换。你会想要幻灯片过渡。

    【讨论】:

    • 从数据库(核心数据)中提取的图像和标题可能有数百个。我可以只有一个视图控制器来循环浏览它们吗?
    • 您可以拥有数百张图像这一事实是页面视图控制器的一个论据。它管理视图控制器的方式与表格视图管理单元格的方式相同。它只根据需要创建子视图控制器。
    • 有一个与 Xcode 捆绑在一起的示例应用程序“PhotoScroller”,它演示了使用页面视图控制器滚动高分辨率图像。不幸的是,它很旧并且是用 Objective-C 编写的,所以它可能没有那么有用。
    • 数百张图片意味着您绝对应该使用按需加载内容的东西:UIPageViewControllerUICollectionView
    • @robmayoff,你说得比我好。
    【解决方案2】:

    使用现有组件构建它的三种方法:

    • UIPageViewController(见 DuncanC 的回答)。

    • UICollectionView pagingEnabled 设置为真。使用全屏大小的单元格和 UICollectionViewFlowLayout 并将 scrollDirection 设置为水平。

    • UICollectionView 如上所述,但不是将 pagingEnabled 设置为 true,而是在您的委托中实现 scrollViewWillEndDragging:withVelocity:targetContentOffset:。这允许您在每个单元格之间有一个装订线,同时仍然让每个单元格填满屏幕。 See this answer.

    【讨论】:

    • 好答案。 (已投票)
    • 有问题的视图控制器是来自主表的详细视图,并且是从我创建的通用 UIViewController 继承的 UIViewController,其中包含所有 UIViewController 使用的一些通用方法。此外,VC 有一些我需要的自己的方法。您是否建议我将其形式更改为 UICollectionView 或 UIPageView。或者我应该选择一个新的 UICollectionView 或 UIPageView,然后加载 UIVC 显示图像和标题?
    • UICollectionView 是一个视图,而不是视图控制器。您可以将视图控制器设置为其数据源和委托。
    • 太棒了。我想我应该知道这一点。谢谢!
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多