3

我正在尝试根据 UIScrollView 的滚动使轮子平滑旋转。当它几乎停止滚动滚动视图时,我希望它能够缓解。

我尝试了很多但到目前为止失败了,我当前的代码是这样的:

#pragma mark - Scrollview delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    NSLog(@"Offset: %@ Page: %i", NSStringFromCGPoint(scrollView.contentOffset), page);

    UIImageView* leftWheelImageView = (UIImageView*)[self.carImageView viewWithTag:10];

    // Calculate speed of scrolling and turn the wheels to it
    CGPoint currentOffset = scrollView.contentOffset;
    NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];

    if(lastTime - currentTime > 0.1) {
        CGFloat distance = currentOffset.x - lastOffset.x;
        CGFloat scrollSpeedNotAbs = (distance * 10) / 1000; //in pixels per millisecond        
        CGFloat scrollSpeed = fabsf(scrollSpeedNotAbs);

        leftWheelImageView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(2*3.14*99));
        NSLog(@"Rotate wheel");
    }

    lastOffset = currentOffset;
    lastTime = currentTime;
}

谁能把我推向正确的方向:-)?

4

2 回答 2

3

您设置的偏移量和时间不正确;照原样,并且lastOffset每次收到 didScroll 消息时都会lastTime更改为。因此,只有在每条消息之间的间隔超过 0.1 秒时才会触发您的 if 语句。既然您说您希望滚动流畅,那么这似乎不太可能是您想要的。也许将您的作业移到您的 if 语句中?我看看能不能找到另一种方法供你使用。currentOffsetcurrentTime

于 2012-07-03T12:25:47.007 回答
2

Why don't you use the library for doing this ? I have done this using carousel library . Please refer following link https://github.com/nicklockwood/iCarousel this is easy and better to implement.

于 2013-01-14T11:13:13.017 回答