1

我正在尝试创建一个自定义视图,它类似于 NSScrollView,但基于 CoreAnimation CAScrollLayer/CATiledLayer。基本上,我的应用程序需要大量近乎实时的 CGPath 绘图,并使用形状图层为这些路径设置动画(类似于 GarageBand 在录制时设置动画的方式)。我创建的第一个原型使用 NSScrollView,但我无法获得超过 20 帧/秒(原因是 NSRulerView 每次滚动事件发生时都会通过绘制更新,整个调用流程从 -[NSClipView scrollToPoint] 到 -[ NSScrollView reflectScrolledClipView:] 非常昂贵且效率低下)。

我创建了一个自定义视图,它使用 CAScrollLayer 作为滚动机制,使用 CATiledLayer 作为传统的 documentView(用于无限滚动选项),现在我可以接近 60fps。但是,我很难实现 scrollWheel 弹性滚动,而且我不知道该怎么做。这是我到目前为止的代码,如果有人能告诉我如何实现弹性滚动,我将不胜感激。

-(void) scrollWheel:(NSEvent *)theEvent{
    NSCAssert(mDocumentScrollLayer, @"The Scroll Layer Cannot be nil");
    NSCAssert(mDocumentLayer, @"The tiled layer cannot be nil");
    NSCAssert(self.layer, @"The base layer of view cannot be nil");
    NSCAssert(mRulerLayer, @"The ScrollLayer for ruler cannot be nil");

    NSPoint locationInWindow = [theEvent locationInWindow];
    NSPoint locationInBaseLayer = [self convertPoint:locationInWindow
                                        fromView:nil];


    NSPoint locationInRuler = [mRulerLayer convertPoint:locationInBaseLayer
                                          fromLayer:self.layer];
    if ([mRulerLayer containsPoint:locationInRuler]) {
       return;
    }
    CGRect docRect = [mDocumentScrollLayer convertRect:[mDocumentLayer bounds]
                                             fromLayer:mDocumentLayer];

    CGRect scrollRect = [mDocumentScrollLayer visibleRect];
    CGPoint newOrigin = scrollRect.origin;

    CGFloat deltaX = [theEvent scrollingDeltaX];
    CGFloat deltaY = [theEvent scrollingDeltaY];

    if ([self isFlipped]) {
     deltaY *= -1;
    }

    scrollRect.origin.x -= deltaX;
    scrollRect.origin.y += deltaY;

    if ((NSMinX(scrollRect) < NSMinX(docRect)) ||
        (NSMaxX(scrollRect) > NSMaxX(docRect)) ||
        (NSMinY(scrollRect) < NSMinY(docRect)) ||
        (NSMaxY(scrollRect) > NSMaxX(docRect))) {
        mIsScrollingPastEdge = YES;
        CGFloat heightPhase = 0.0;
        CGFloat widthPhase  = 0.0;
        CGSize size = [self frame].size;

    if (NSMinX(scrollRect) < NSMinX(docRect)) {
        widthPhase = ABS(NSMinX(scrollRect) - NSMinX(docRect));
    }

    if (NSMaxX(scrollRect) > NSMaxX(docRect)) {
        widthPhase = ABS(NSMaxX(scrollRect) - NSMaxX(docRect));
    }

    if (NSMinY(scrollRect) < NSMinY(docRect)) {
        heightPhase = ABS(NSMinY(scrollRect) - NSMinY(docRect));
    }

    if (NSMaxY(scrollRect) > NSMaxY(docRect)) {
        heightPhase = ABS(NSMaxY(scrollRect) - NSMaxY(docRect));
    }

    if (widthPhase > size.width/2.0) {
        widthPhase = size.width/2.0;
    }

    if (heightPhase > size.width/2.0) {
        heightPhase = size.width/2.0;
    }

    deltaX = deltaX*(1-(2*widthPhase/size.width));
    deltaY = deltaY*(1-(2*heightPhase/size.height));
}

newOrigin.x -= deltaX;
newOrigin.y += deltaY;

if ( mIsScrollingPastEdge &&
    (([theEvent phase] == NSEventPhaseEnded) ||
     ([theEvent momentumPhase] == NSEventPhaseEnded)
     )
    ){
    CGPoint confinedScrollPoint = [mDocumentScrollLayer bounds].origin;
    mIsScrollingPastEdge = NO;
    CGRect visibleRect = [mDocumentScrollLayer visibleRect];

    if (NSMinX(scrollRect) < NSMinX(docRect)){
        confinedScrollPoint.x = docRect.origin.x;
    }

    if(NSMinY(scrollRect) < NSMinY(docRect)) {
        confinedScrollPoint.y = docRect.origin.y;
    }

    if (NSMaxX(scrollRect) > NSMaxX(docRect)) {
        confinedScrollPoint.x = NSMaxX(docRect) - visibleRect.size.width;
    }

    if (NSMaxY(scrollRect) > NSMaxY(docRect)){
        confinedScrollPoint.y = NSMaxY(docRect) - visibleRect.size.height;
    }

    [mDocumentScrollLayer scrollToPoint:confinedScrollPoint];
    CGPoint rulerPoint = [mRulerLayer bounds].origin;
    rulerPoint.x = [mDocumentLayer bounds].origin.x;
    [mRulerLayer scrollToPoint:rulerPoint];
    return;
}

CGPoint rulerPoint = [mDocumentScrollLayer convertPoint:newOrigin
                                                toLayer:mRulerLayer];
rulerPoint.y = [mRulerLayer bounds].origin.y;

if (!mIsScrollingPastEdge) {
    [CATransaction setDisableActions:YES];
    [mDocumentScrollLayer scrollToPoint:newOrigin];
    [CATransaction commit];
}else{
    [mDocumentScrollLayer scrollToPoint:newOrigin];
    [mRulerLayer scrollToPoint:rulerPoint];
}

}
4

2 回答 2

1

在https://github.com/twitter/twui查看 TUIScrollView 。

于 2012-09-28T17:56:58.073 回答
0

添加 ElasticScrolling 的核心概念是使用 SpringSolver。

这有一个可以重复使用的 ElasticScrollView:
https ://github.com/eonist/Element

于 2017-05-07T20:15:31.333 回答