【问题标题】:xcode how can i make a scrollView scroll very slowly to the end without user interaction?xcode如何在没有用户交互的情况下使scrollView滚动到最后?
【发布时间】:2013-03-14 15:15:39
【问题描述】:

我有一个 scrollView,我需要它自己非常缓慢地向下滚动到最后,当用户触摸视图时,它应该响应手势(向上向下)。除此之外,我需要放置一个停止/播放自动滚动的按钮。

代码:

self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.scrollView.contentSize = CGSizeMake(320,4750);
_scrollView.frame = CGRectMake(0, 44, 320, 420);

UIButton *camilaButton = [UIButton buttonWithType:UIButtonTypeCustom];
[camilaButton setImage:[UIImage imageNamed:@"camila.jpg"] forState:UIControlStateNormal];
[camilaButton setImage:[UIImage imageNamed:@"camila.jpg"] forState:UIControlStateHighlighted];
camilaButton.frame = CGRectMake(20, 10, 280, 200);

[self.scrollView addSubview:camilaButton];
[camilaButton addTarget:self action:@selector(onButtonPressCamila) forControlEvents:UIControlEventTouchUpInside];

UILabel *camilaLabel = [[UILabel alloc] initWithFrame:CGRectMake(40,220,100,30)];
camilaLabel.text = @"Camila";
[self.scrollView addSubview:camilaLabel];

...然后在滚动视图的末尾有类似的带有图像的按钮。

我该怎么做?

米海

【问题讨论】:

    标签: xcode button scroll playback autoscroll


    【解决方案1】:

    可能最简单的方法是从一个点滚动到另一个点没有动画:

    CGPoint theScrollPoint;
    theScrollPoint.x = 0;
    theScrollPoint.y = (nRegion * self.tableView.rowHeight) +
    dblPctIntoRegion * (self.tableView.rowHeight);
    [self.tableView setContentOffset: theScrollPoint animated:FALSE];
    

    如果您可以将其近似为点对点(并且您确实说得很慢),它将足够流畅,并且避免在动画过程中处理用户手动滚动的麻烦。

    如果您确实决定为您的程序化滚动设置动画,请注意,当您尝试以编程方式滚动等时,用户轻弹滚动条等方式可能会导致您重新进入 scrollViewDidEndScrollingAnimation。我不得不求助于黑客:

    - (void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
    {
      if(!m_bNoReentrantscroll)
      {
        m_bNoReentrantscroll = TRUE;
      }
      m_bNoReentrantscroll = FALSE;
    }
    

    无论如何,这一切都比您深入测试时看起来要复杂得多。我建议放弃卷轴上的动画!

    【讨论】:

      【解决方案2】:

      制作一个定时调用这个表格视图方法

      [tableView scrollRowToVisible:nextRow++];
      

      nextRow 可以是属性 NSInteger 或全局/静态变量。

      【讨论】:

      • 哪个答案对您有用?请接受,以便其他人知道如何解决此问题。
      • 可能你所有的答案都是好的;我是个菜鸟,但无法全部测试。我找到了另一个更容易实现的代码。
      • 如果你用另一个不在这里的代码解决了,你应该把它粘贴在这里,并接受它:)
      • 我已经发布了,明天我会接受,因为系统还不允许我。
      • 这段代码在使用标签栏控制器的视图控制器中,当我在视图控制器之间切换时它不会停止(我还在每张幻灯片上实现了声音)。您知道更改视图控制器后如何停止它吗?
      【解决方案3】:
      - (void) animateScroll:(NSTimer *)timerParam
      {
          const NSTimeInterval duration = 10.2;
      
          NSTimeInterval timeRunning = -[startTime timeIntervalSinceNow];
      
          if (timeRunning >= duration)
          {
              [scrollView setContentOffset:destinationOffset animated:YES];
              [timer invalidate];
              timer = nil;
              return;
          }
          CGPoint offset = [scrollView contentOffset];
          offset.y = startOffset.y + (destinationOffset.y - startOffset.y) * timeRunning / duration;
          [scrollView setContentOffset:offset animated:YES];
      }
      
      - (void) doAnimatedScrollTo:(CGPoint)offset
      {
          self.startTime = [NSDate date];
          startOffset = scrollView.contentOffset;
          destinationOffset = offset;
      
          if (!timer)
          {
              self.timer =
              [NSTimer scheduledTimerWithTimeInterval:0.01
                      target:self
                      selector:@selector(animateScroll:)
                      userInfo:nil
                      repeats:YES];
          }
      }
      

      【讨论】:

        【解决方案4】:

        我还发现了这个代码:

        http://sugartin.info/2012/01/21/image-sliding-page-by-page-uiscrollview-auto-scrolling-like-image-slider/

        并将其调整为垂直滑动而不是水平滑动。

        它是一页一页的,但它最终满足了我的需求。

        【讨论】:

          【解决方案5】:

          这个改编的代码成功了(每张图片是 280 x 200):

              - (void)viewDidLoad
           {
          [super viewDidLoad];
          
          UIScrollView *scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
          scr.tag = 1;
          scr.autoresizingMask=UIViewAutoresizingNone;
          [self.view addSubview:scr];
          [self setupScrollView:scr];
          UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 264, 480, 36)];
          [pgCtr setTag:12];
          pgCtr.numberOfPages=10;
          pgCtr.autoresizingMask=UIViewAutoresizingNone;
          [self.view addSubview:pgCtr];
           }
          
            - (void)setupScrollView:(UIScrollView*)scrMain {
          // we have 10 images here.
          // we will add all images into a scrollView & set the appropriate size.
          
          for (int i=1; i<=10; i++) {
              // create image
              UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"sti%02i.jpg",i]];
              // create imageView
              UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(20, ((i-1)*scrMain.frame.size.height+100), 280, 200)];
              // set scale to fill
              imgV.contentMode=UIViewContentModeScaleToFill;
              // set image
              [imgV setImage:image];
              // apply tag to access in future
              imgV.tag=i+1;
              // add to scrollView
              [scrMain addSubview:imgV];
          }
          // set the content size to 10 image width
          [scrMain setContentSize:CGSizeMake(scrMain.frame.size.width, scrMain.frame.size.height*10)];
          // enable timer after each 2 seconds for scrolling.
          [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scrollingTimer) userInfo:nil repeats:YES];
          
          
          }
          
           - (void)scrollingTimer {
          // access the scroll view with the tag
          UIScrollView *scrMain = (UIScrollView*) [self.view viewWithTag:1];
          // same way, access pagecontroll access
          UIPageControl *pgCtr = (UIPageControl*) [self.view viewWithTag:12];
          // get the current offset ( which page is being displayed )
          CGFloat contentOffset = scrMain.contentOffset.y;
          // calculate next page to display
          int nextPage = (int)(contentOffset/scrMain.frame.size.height) + 1 ;
          // if page is not 10, display it
          if( nextPage!=10 )  {
          
          
              [scrMain scrollRectToVisible:CGRectMake(0, nextPage*scrMain.frame.size.height, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
              pgCtr.currentPage=nextPage;
              // else start sliding form 1 :)
          
          
          } else {
          
              [scrMain scrollRectToVisible:CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
              pgCtr.currentPage=0;
          }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-11-28
            • 1970-01-01
            • 2010-11-21
            • 1970-01-01
            • 2016-03-08
            • 2012-11-06
            • 2014-11-17
            相关资源
            最近更新 更多