Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8601003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:44:41+00:00 2026-06-12T01:44:41+00:00

I am trying to create a custom view, which is similar to NSScrollView, but

  • 0

I am trying to create a custom view, which is similar to NSScrollView, but is based on CoreAnimation CAScrollLayer/CATiledLayer. Basically, my app requires a lot of near realtime CGPath drawing, and animates these paths using shape layers (it’s similar to the way GarageBand animates while recording). The first prototype I created used NSScrollView, but I could not get more than 20 frames per second with it (The reason was that NSRulerView updates by drawing every time the scrollEvent happens, and the entire call flow from -[NSClipView scrollToPoint] to -[NSScrollView reflectScrolledClipView:] is extremely expensive and inefficient).

I’ve created a custom view that uses CAScrollLayer as scrolling mechanism and CATiledLayer as the traditional documentView (for infinite scrolling option), and now I can get close to 60fps. However, I’m having hard time implementing scrollWheel elastic scrolling, and I’m not sure how to do it. Here’s the code I have so far, and would really appreciate if someone can tell me how to implement elasticScrolling.

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:44:42+00:00Added an answer on June 12, 2026 at 1:44 am

    Take a look at TUIScrollView at https://github.com/twitter/twui.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a custom view, which has a rectangle, and some text
I am trying to create a custom view for a sharepoint list, similar to
I've been trying to create a custom view which has blurred shapes under text.
I'm trying to create a custom view component which looks like this: a component
I'm trying to create programatically a custom view controller with subviews which center on
I am trying to create a custom popup view that can be called from
I'm trying to create a CGRect inside of a custom view (rectView) that will
I'm trying to create a custom usercontrol that acts like a button, but i
I am trying to create a list view which as TextView that can display
I'm trying to use hook_menu to create a link to a view which takes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.