0

我有一个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];

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

我怎样才能做到这一点?

米海

4

5 回答 5

2

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

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

无论如何,当你深入测试它时,这一切都比看起来更棘手。我建议放弃卷轴上的动画!

于 2013-03-14T15:30:48.717 回答
1

Make a timer that will periodically call this table view method

[tableView scrollRowToVisible:nextRow++];

nextRow can be a property NSInteger or a global/static variable.

于 2013-03-14T15:19:59.817 回答
1
- (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];
    }
}
于 2013-03-14T15:20:26.390 回答
0

我还发现这段代码可以解决问题:

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

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

它是逐页的,但它最终适合我的需求。

于 2013-03-15T10:55:03.307 回答
0

这个改编的代码成功了(每张图片都是 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;
}
}
于 2013-03-15T11:18:54.787 回答