NSScrollView scroll position - cocoa

I currently have a NSView in a NSScrollView.
The NSView is large and I need to scroll to manage the object inside.
I want to draw a static rectangle in the center of the NSView without scrolling.
I want to get the scrolling point (the NSClipPoint?) in the drawRect method of the NSView so that I can draw the rectangle at the last step of drawRect to make it on top.
I have looked through the doc and could only find methods to set the scroll point, not getting it.
How can I get that point?

The answer is [[myScrollView contentView] documentVisibleRect]

I've never tried this, so I don't know if it will actually work or not (and I'm about 8 hours away from my Mac, so I can't give it a shot). But can you subclass the NSClipView that your NSScrollView is using and draw the rectangle in NSClipView's drawRect:? If that doesn't work, what about trying the same thing with the NSScrollView directly?
And if that doesn't work, er...report back and I'll remove this answer so as not to mislead anyone coming from Google. :)

Related

Swiping UIImageView ofscreen

I have a UIImageView that needs to be able to be panned all over the view, pinched and rotated, but the imageView needs to change picture on a swipe movement as well. Any idea on how to accomplish this? I have added a UISwipeGestureRecognizer but that will change the picture on any swipe movement. Ideal would be for me to make the picture animate away and the new one on to the screen once the user drags it of the screen or when it's not visible on the screen for more than 50%. Thanks in advance.
This is exactly what a UIScrollView was made for. Simply add all your UIImageView's to the UIScrollView, position them in the right order, and set .paging = YES. Make sure you also set the correct contentSize, or the UIScrollView won't scroll at all!
Also, check out the delegate methods for how to configure the UIScrollView for zooming. You may need to put the UIImageView's in their own UIScrollView which will enable zooming for that specific UIImageView, but this depends on your needs.
Hope that Helps!

NSButtons leave artefacts when used as subviews of a custom toolbar view

I'm placing a few buttons in a simple rectangular NSview which acts as a custom toolbar. On first render the buttons/views come out as expected, but every time a button is pressed (and sometimes with no mouse interaction at all) artefacts start appearing.
Before
After
I can eliminate the artefacts by calling a [self.toolbarView setNeedsDisplay:YES] in all the action and focus methods but this seems like a hack, is there any clean way to deal with this?
It was a beginner's problem. In the drawRect method
- (void)drawRect:(NSRect)dirtyRect
I was using the param dirtyRect for drawing an outline of my view, assuming it was the view's bounds, where in fact it was only the area around the buttons that became dirty when they were pressed. The 'artefacts' were actually my outline being drawn in the wrong place.
By correctly using the bounds of the view
NSRect drawingRect = [self bounds];
the 'artefacts' no longer appeared.
You just try to set focus ring for a buttons to 'none' in IB.

NSScrollView and ScrollToPoint on an NSImage

I have a NSView as the documentView for a NSScrollView. I also have a NSImageView as a subview of the NSView. The image dynamically changes size so the scroll bars become active/inactive at various times. Once the image has changed, I'd like to scroll to a certain point on the image. From within the NSView's drawRect: method, I call
[[myScrollView contentView] scrollToPoint: myPoint];
The scroll bars update and the image appears as I'd like, but as soon as the image is scrolled, a double image appears or parts of the image get cut off. Any help would be appreciated. Thank you.
Sounds like you might want to turn off the "Copy On Scroll" behavior option of the NSScrollView either in Interface Builder or programmatically.
From Scroll View Programming Guide for Mac OS X: How Scrolling Works:
The NSClipView class provides low-level scrolling support through the
scrollToPoint: method. This method translates the origin of the
content view’s bounds rectangle and optimizes redisplay by copying as
much of the rendered document view as remains visible, only asking the
document view to draw newly exposed regions. This usually improves
scrolling performance but may not always be appropriate. You can turn
this behavior off using the NSClipView method setCopiesOnScroll:
passing NO as the parameter. If you do leave copy-on-scroll active, be
sure to scroll the document view programmatically using the NSView
method scrollPoint: method rather than translateOriginToPoint:.

How to tell what's causing drawRect to be called?

I've got my custom NSView with a bunch of custom buttons in it, the buttons are added as a subView in the NSView's drawRect method.
Now I'm finding that after pressing a button the drawRect of the parent view is repeatedly called. Sometimes it only stops when I quit the app - I know this from a simple log statement in drawRect.
Now I know there are probably bigger architectural issues in my app causing this, where do I go to begin tracking down what's causing this view to be repeatedly redrawn?
Thanks!
First of all you shouldn't be adding subviews in drawRect:.
Are you doing any custom drawing or are you just adding subviews? If you're not doing any drawing, you should not implement drawRect:.
You want to add the subviews in initWithFrame: and then you want to set the frames of the subviews in layoutSubviews based on self.bounds.
If you have any questions, feel free to ask.
EDIT: Just realized that you were asking about NSView and not UIView. I've never used NSView, but perhaps they work similarly.
EDIT 2: I read a bit about NSView and it doesn't seem to have layoutSubviews. Perhaps you should set the frames in drawRect:? I'm still pretty sure you don't want to add subviews in drawRect:.

NSScrollView - pattern background - how to start from top-left corner?

To fill NSScrollview with pattern image I use - (void)setBackgroundColor:(NSColor *)aColor method where aColor is created with + (NSColor *)colorWithPatternImage:(NSImage *)image method. Despite of what isFlipped returns for NSScrollView and its content view, pattern start repeat from bottom-left corner while I want it to start from top-left corner. How can I achieve that?
I believe the key here may be that -setBackgroundColor: of the NSScrollView:
Sets the color of the content view’s
background to aColor.
The content view of a NSScrollView is a separate view inside of the NSScrollView.
So, what you will probably need to do is be able to change the isFlipped of the content view of the NSScrollView.
I haven't played around with this yet, so I don't know what problems you may run into.
EDIT: However, it appears your answer may be found here:
CocoaDev: NSScrollView
Look near the bottom of the article.
You need to use the method [NSGraphicsContext setPatternPhase:]. This thread provides the details. It worked for me.

Resources