2

我是 IOS Dev 的初学者。询问一种UIScrollView通过拖动使图像滚动的方法UISlider。这是我的情况:

所需案例:

拖动滑块时,图像会滚动UIScrollView。滚动图像时,滑块也会相应地更改其值。

实际案例:

滚动图像时,滑块会相应地更改其值,拖动滑块图像时不要滚动。

这是我的代码,我希望有人告诉我如何在滑块拖动时滚动。

滑块IBAction

- (IBAction)sliding:(UISlider *)sender{
    int slider_value = (int)slider.value;
    NSString *current_page = [[NSString alloc] initWithFormat:@"%i",slider_value];
    current_page_lbl.text = current_page ;
    [self loadPage:slider_value];
    [self loadVisiblePages];
}

UIScrollview 的方法:

- (void)loadPage:(NSInteger)page {
        if (page < 0 || page >= self.pageImages.count) {
            return;
        }

        UIView *pageView = [self.pageViews objectAtIndex:page];
        if ((NSNull*)pageView == [NSNull null]) {

            CGRect frame = self.scrollView.bounds;
            frame.origin.x = frame.size.width * page;
            frame.origin.y = 0.0f;

            UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
            newPageView.contentMode = UIViewContentModeScaleAspectFit;
            newPageView.frame = frame;
            [self.scrollView addSubview:newPageView];

            [self.pageViews replaceObjectAtIndex:page withObject:newPageView];
        }
    }

- (void)purgePage:(NSInteger)page {
    if (page < 0 || page >= self.pageImages.count) {
        // If it's outside the range of what you have to display, then do nothing
        return;
    }


    UIView *pageView = [self.pageViews objectAtIndex:page];
    if ((NSNull*)pageView != [NSNull null]) {
        [pageView removeFromSuperview];
        [self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
    }
}

加载VisiblePage

- (void)loadVisiblePages {

    CGFloat pageWidth = self.scrollView.frame.size.width;
    NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
    NSLog(@"page loaded is %d",page);

    self.pageControl.currentPage = page; 

    NSInteger firstPage = page - 1;
    NSInteger lastPage = page + 1;

    for (NSInteger i=0; i<firstPage; i++) {
         [self purgePage:i];
    }

    for (NSInteger i=firstPage; i<=lastPage; i++) {
         [self loadPage:i];
         slider.value = 21-i ;
         int slider_value = (int)slider.value;
         NSString *current_page = [[NSString alloc] initWithFormat:@"%i",21-slider_value];
         current_page_lbl.text = current_page ;
    }

    for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
         [self purgePage:i];
    }
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self loadVisiblePages];
}
4

1 回答 1

2

uislider 不适合与 uiscrollview 一起使用....为此,有一个可用的控件名称 UIPagecontrol.. 以下链接是一个很好的示例,其中包含指导您如何将其与 UIScollview 一起使用的代码

UIPagecontroll 和 UIscollview 和代码点击这里

于 2012-09-24T07:52:10.793 回答