目标C. UIScrollView。如果速度超过1,如何滚动到一页

时间:2016-06-16 16:15:54

标签: ios objective-c uiscrollview paging uipageviewcontroller

在这个时候我写了pod,自定义pageViewController。 在这项工作中我有一个问题。

我有scrollView。在这个scrollView上我有3 \ 5 \ 7 ViewControllers。 它们很小,在Scroll视图中我们可以看到第一个VC,第二个VC和第三个VC的一半。

ScrollView上的

pagingEnabled为On。如果我们滚动VC移动和scrollView更改聚焦区域。

如果我们快速滚动或触摸一个屏幕尺寸并移动到其他尺寸,那么我们将从第一个屏幕移动到第三个VC。

http://prntscr.com/bhfqfn

http://prntscr.com/bhfqr8

http://prntscr.com/bhfrdq

pragma mark - UIScrollViewDelegate

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
if (0 > velocity.x) {
    if ([self.dataSource respondsToSelector:@selector(pageViewController:viewControllerBeforeViewController:)]) {
        if (self.scrollView.contentOffset.x <= self.internalViewControllers.firstObject.view.frame.size.width + self.view.frame.size.width) {
            UIViewController *newViewController = [self.dataSource pageViewController:self viewControllerBeforeViewController:self.internalViewControllers.firstObject];
            if (newViewController) {
                [self popRightViewCintroller];
                [self pushLeftViewController:newViewController];
                [self moveViewControllers];
                targetContentOffset->x = scrollView.contentOffset.x;
                [scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + self.scrollView.frame.size.width, 0) animated:NO];
            }
        }
    }
}
if (0 < velocity.x) {
    if ([self.dataSource respondsToSelector:@selector(pageViewController:viewControllerAfterViewController:)]) {
        if (self.scrollView.contentOffset.x >= self.internalViewControllers.lastObject.view.frame.origin.x - self.view.frame.size.width * 2) {
            UIViewController *newViewController = [self.dataSource pageViewController:self viewControllerAfterViewController:self.internalViewControllers.lastObject];
            if (newViewController) {
                [self popLeftViewCintroller];
                [self pushRightViewController:newViewController];
                [self moveViewControllers];
                targetContentOffset->x = scrollView.contentOffset.x;
                [scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x - self.scrollView.frame.size.width, 0) animated:NO];
            }
        }
    }
} }

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

pragma mark - Helpers

- (void)moveViewControllers {
int numberOfFocusetView = floor(self.scrollView.contentOffset.x / self.scrollView.frame.size.width);
CGFloat contentOffsetView;
if (numberOfFocusetView > 0) {
    contentOffsetView = self.scrollView.contentOffset.x - (numberOfFocusetView * self.scrollView.frame.size.width);
} if (numberOfFocusetView == 0) {
    contentOffsetView = self.scrollView.contentOffset.x;
}
if (numberOfFocusetView >= 0) {
    UIViewController* centralVC = self.internalViewControllers[numberOfFocusetView];
    contentOffsetView = contentOffsetView / (self.scrollView.frame.size.width / (self.scrollView.frame.size.width - self.childVCSize.width - self.sizeBetweenVC));
    CGFloat form = 0;
    if (self.animationSpeed == 0) {
        self.animationSpeed = 1;
    }
    switch (self.animation) {
        case defaultAnimation:
            form = 0;
            break;
        case easeIn:
            form = sinf(contentOffsetView / (self.scrollView.frame.size.width - self.childVCSize.width - self.sizeBetweenVC) * M_PI) * self.animationSpeed;
            break;
        case easeOut:
            form = -sinf(contentOffsetView / (self.scrollView.frame.size.width - self.childVCSize.width - self.sizeBetweenVC) * M_PI) * self.animationSpeed;
            break;
        case easeBorder:
            form = sinf(contentOffsetView / (self.scrollView.frame.size.width - self.childVCSize.width - self.sizeBetweenVC) * M_PI*2) * self.animationSpeed;
            break;
        case easeCenter:
            form = -sinf(contentOffsetView / (self.scrollView.frame.size.width - self.childVCSize.width - self.sizeBetweenVC) * M_PI*2) * self.animationSpeed;
            break;
        default:
            break;
    }
    CGFloat originX = (self.spaceHorizontal + (self.scrollView.frame.size.width * numberOfFocusetView) + contentOffsetView);
    centralVC.view.frame = CGRectMake(originX + form, self.spaceVertical, self.childVCSize.width, self.childVCSize.height);
    [self changeViewControllersOriginXBy:centralVC withNumber:numberOfFocusetView];
} }

- (void) changeViewControllersOriginXBy:(__kindof UIViewController *) centralVC withNumber:(NSInteger) numberOfFocusetView {
for (int i = 0; i < self.internalViewControllers.count; i++) {
    if (i != numberOfFocusetView) {
        CGFloat originXForView = centralVC.view.frame.origin.x + (self.childVCSize.width + self.sizeBetweenVC) * (i - numberOfFocusetView);
        self.internalViewControllers[i].view.frame = CGRectMake(originXForView, self.spaceVertical, self.childVCSize.width, self.childVCSize.height);
    }
} }

有人可以帮助我吗?

0 个答案:

没有答案