IOS / Objective-C:在滑动

时间:2017-07-21 15:19:36

标签: ios objective-c animation

我想模仿向左滑动以显示在照片应用中看到的下一张照片,以及其他地方,以便新照片使用动画从右向左滑动。

但是,在我的情况下,视图有多张照片。它也有一个标题。

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

当新的更新视图从右侧移入时,是否有办法显示旧视图向左移动。

以下内容将视图移动到左侧,但将屏幕显示为黑色,因为没有任何内容放在其中。

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

3 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:2)

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

  • UIPageViewController(参见DuncanC的回答)。

  • UICollectionView设置为true的
  • pagingEnabled。使用全屏大小的单元格,UICollectionViewFlowLayout scrollDirection设置为水平。

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

答案 2 :(得分: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;
        }

    }