Oomph MacMapKit Drawing Wrong - cocoa

I've integrated Oomph MacMapKit in one of my projects, I did all the steps. But there is a problem.
I'm using a NSToolbar and switching between the views. whenever I'm in my map page if I switch into another page and then switch back to the map page, the map is drew under the MKMapView and MKMapView is white but I can navigate in map by dragging mouse in the white area.
I've tried it in another project as well, And it acts just like this again.
Normal Look
After switching back
Does anyone know how can I fix this?
This code is running in my window controller delegate for switching pages
- (NSView*)viewForTag:(int)tag
{
switch (tag)
{
case 0:
return [firstViewController view];
break;
case 1:
return [secondViewController view];
break;
default:
return [firstViewController view];
break;
}
}
- (IBAction)switchViews:(id)sender
{
NSView* currentView = [self viewForTag:[sender tag]];
NSView* previousView = [self viewForTag:currentViewTag];
currentViewTag = [sender tag];
[[[[self window] contentView] animator] replaceSubview:previousView with:currentView];
}
There is no special code in maps page.
Thanks in advance

I cannot fault your code. I presume you stepped through your code and checked that all views are properly added and removed. Also, you might want to check what frame is set for your map view after the switch. If the NSRect for the frame is where your white space is, somehow MKMapView is not keen on being added to and removed from the view stack.
It's a long shot, but you could try one of the following:
Use setHidden: to temporarily make your map view invisible, in stead of removing and adding it. Not sure, but you might gain a bit of performance from this as well as hiding and showing is probably less expensive than adding and removing...
Use addSubView: and removeFromSuperview to switch your views. This might need some code to provide for setting the framing of the views right or have a "default" frame handy that you can use when you add a view.
Bottom line MKMapView is wrapper around WebView, which sometimes seems to behave a bit odd.

This Map Kit is a bit buggy sometimes, changing my way of presenting views solved the problem.

Related

layerUsesCoreImageFilters causes glitch on first use

When I uses Views that are layer backend and have layerUsesCoreImageFilters=YES the first time I use a filter the view 'reinitializes'. Its pretty ugly.
I use a blur filter on a subview, but it causes the complete view to go blank and the reappear.
I am currently on Mavericks (10.9.4)
Anyway is there a standard method to force the initialization at app startup ?
I was able to work around this by setting layerUsesCoreImageFilters on some of my top level views before they were every displayed in awakeFromNib. This caused subsequent calls to setLayerUsesCoreImageFilters on other views to work properly without any drawing issues or flashing views.
Something like this in awakeFromNib:
NSArray *subviews = self.mainView.subviews;
for (NSView *subview in subviews)
{
if ([subview respondsToSelector:#selector(setLayerUsesCoreImageFilters:)])
{
if (!subview.layerUsesCoreImageFilters)
{
[subview setLayerUsesCoreImageFilters:YES];
}
}
}

Xcode changing constraints dependant on screen orientation

I have designed an app using auto layout and all works perfectly, issue is, when I switch to landscape I want the location of one button to move to the other side of the screen.
Im assuming the best way of doing this is by changing the constraints when I rotate the screen, is this possible in the code, or even the best way of doing it? Obviously if the app starts up in landscape I obviously want this button to be in the landscape mode also.
thanks
Absolutely. Remove and add constraints as desired in your view controller's updateViewConstraints implementation.
You will find it easiest to prepare both sets of alternate constraints beforehand, maintaining them in instance variables. Then you can just swap them in and out. Here's a sketch of what the code could look like:
-(void)updateViewConstraints {
[self.view removeConstraints:self.oneSideConstraints];
[self.view removeConstraints:self.otherSideConstraints];
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
[self.view addConstraints:self.oneSideConstraints];
else
[self.view addConstraints:self.otherSideConstraints];
[super updateViewConstraints];
}

Are layer-hosting NSViews allowed to have subviews?

Layer-hosting NSViews (so NSViews that you supply a CALayer instance for and set it with setLayer:) can obviously contain subviews. Why obviously? Because in Apple's own Cocoa Slides sample code project, you can check a checkbox that switches the AssetCollectionView from being layer-backed to being layer-hosting:
- (void)setUsesQuartzCompositionBackground:(BOOL)flag {
if (usesQuartzCompositionBackground != flag) {
usesQuartzCompositionBackground = flag;
/* We can display a Quartz Composition in a layer-backed view tree by
substituting our own QCCompositionLayer in place of the default automanaged
layer that AppKit would otherwise create for the view. Eventually, hosting of
QCViews in a layer-backed view subtree may be made more automatic, rendering
this unnecessary. To minimize visual glitches during the transition,
temporarily suspend window updates during the switch, and toggle layer-backed
view rendering temporarily off and back on again while we prepare and set the
layer.
*/
[[self window] disableScreenUpdatesUntilFlush];
[self setWantsLayer:NO];
if (usesQuartzCompositionBackground) {
QCCompositionLayer *qcLayer = [QCCompositionLayer compositionLayerWithFile:[[NSBundle mainBundle] pathForResource:#"Cells" ofType:#"qtz"]];
[self setLayer:qcLayer];
} else {
[self setLayer:nil]; // Discard the QCCompositionLayer we were using, and let AppKit automatically create self's backing layer instead.
}
[self setWantsLayer:YES];
}
}
In the same AssetCollectionView class, subviews are added for each image that should be displayed:
- (AssetCollectionViewNode *)insertNodeForAssetAtIndex:(NSUInteger)index {
Asset *asset = [[[self assetCollection] assets] objectAtIndex:index];
AssetCollectionViewNode *node = [[AssetCollectionViewNode alloc] init];
[node setAsset:asset];
[[self animator] addSubview:[node rootView]];
[nodes addObject:node];
return [node autorelease];
}
When I build and run the app and play around with it, everything seems to be fine.
However, in Apple's NSView Class Reference for the setWantsLayer: method it reads:
When using a layer-hosting view you should not rely on the view for
drawing, nor should you add subviews to the layer-hosting view.
What is true? Is the sample code incorrect and it's just a coincidence that it works? Or is the documentation false (which I doubt)? Or is it OK because the subviews are added through the animator proxy?
When AppKit is "layer hosting" we assume you may (or may not) have a whole subtree of layers that AppKit doesn't know about.
If you add a subview to the layer hosted view, then it might not come out in the right sibling order that you want. Plus, we sometimes add and remove them, so it might change depending on when you call setLayer:, setWantsLayer: or when the view is added or removed from the superview. On Lion (and before) we remove the layers that we "own" (ie: layer backed) when the view is removed from the window (or superview).
It is okay to add subviews...their children-sibling-order in the sublayers array just might not be deterministic if you have sibling-layers that aren't NSViews.
I don't know what's the "right" answer to this. But I do think that the CocoaSlides example works within the boundaries of what the docs say you "shouldn't" do. In the example, look at where the insertNodeForAssetAtIndex: method is called, and you'll see that it only happens when the view is being populated, before it ever is assigned a layer or has setWantsLayer: called on it.
The docs don't say that a layer-hosted view can't contain any subviews, they just say that you can't add and subviews to one. At the point in time when those subviews are added, the main view hasn't yet become a layer-hosting view. After it has been turned into a layer-hosting view by having a manually created layer assigned to it, no more subviews are added.
So there's really no contradiction between the docs and this particular example. That being said, it could be interesting to explore this further, maybe by switching on the QC background layer right from the start, e.g. by sticking a [self setUsesQuartzCompositionBackground:YES]; right inside initWithFrame:.
SPOLIER ALERT:
It seems to work just fine. The creation of the display is a bit slower (not surprising with all that QC animation going on), but apart from that it's smooth sailing.
One comment about this code from Apple: it's busted.
When you first start the app up, note the nice gradient background. Turn QC on, then off.
Poof, no more gradient background.

CALayer Live Resize Poor Performance

I have a UI where the content of an NSCollectionViewItem's View is drawn programmatically through CALayers. I am using a CAConstraintLayoutManager to keep the layot of the sublayers consistent when resizing, but I am getting very poor performance when doing so. It seems that resizing the window, which causes the resize of two CATextLayers so that they fit the root layer's width, and the repositioning of one CATextLayer so that it stays right-aligned, is causing the application to spend most of its time executing the CGSScanConvolveAndIntegrateRGB function (I have used the Time Profiler instrument).
The most "expensive" layer (the one that causes the most stuttering even if it's the only one displayed) is a wrapped multiline CATextLayer. I have absolutely no idea how to get better performance (I have tried not using a CAConstraintLayoutManager and going with layer alignments but I'm getting the same thing). Has anyone had this problem? Is there a way around it?
PS: I have subclassed the layout manager and disabled all the animations during the execution of - (void)layoutSublayersOfLayer:(CALayer *)layer by setting YES to kCATransactionDisableActions in the CATransaction but it doesn't seems to help.
Edit: I have disabled Font Smoothing for the Text Layers and performance has increased a little bit (very little), but it spends an awful amount of time in _ZL9view_drawP7_CAViewdPK11CVTimeStampb (which is something that gets called by a thread of the ATI Radeon driver, I suppose).
I solved it. Kind of. It still seems like a dirty hack to me, but I couldn't find out how to make setNeedsDisplayInRect work so I ended up doing it like this:
In the NSWindow delegate:
-(void)windowWillStartLiveResize:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"beginResize" object:nil];
}
-(void)windowDidEndLiveResize:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"endResize" object:nil];
}
In my Custom View those two notifications call, respectively, the -(void)beginResize and -(void)endResize selectors. The first one sets a BOOL inLiveResize variable to YES, while the second one sets it to NO and calls setFrameSize again with the new frame size.
I overrode (overridden? Not native english speaker, sorry) the -(void)setFrameSize:(NSSize)newSize method like this:
-(void)setFrameSize:(NSSize)newSize
{
if (inLiveResize) {
NSRect scrollFrame = [[[self superview] enclosingScrollView] documentVisibleRect];
BOOL condition1 = (self.frame.origin.y > (scrollFrame.origin.y - self.frame.size.height));
BOOL condition2 = (self.frame.origin.y < (scrollFrame.origin.y + scrollFrame.size.height + self.frame.size.height));
if (condition1 && condition2)
[super setFrameSize:newSize];
}
else {
[super setFrameSize:newSize]; }}
That's it. This way, only the visible views resize live with the window, while the others get redrawn at the end of the operation. It works, but I don't like how 'dirty' it is, I'm sure there is a more elegant, built-in(ish) way to do this by using the setNeedsDisplayInRect method. I will research more.

scrolling NSTextView leaves scrollbar in wrong position

I have an NSTextView inside an NSScrollView. I put text in the NSTextView and scroll it to the bottom programatically, which works OK, but the scrollbar stays at the top. Using the mouse to position the scrollbar causes it to jump to the bottom, where it belongs, and from that point it operates OK.
My code:
textView.string = s;
[textView scrollToEndOfDocument:self];
Don't get hung up on the scrollToEndOfDocument method--I also tried:
[textView scrollRangeToVisible:NSMakeRange(s.length, 0)];
and:
[[scrollViewText contentView] scrollToPoint:NSMakePoint(0, textView.frame.size.height)];
[scrollViewText reflectScrolledClipView:[scrollViewText contentView]];
with exactly the same problem, shown here:
I fixed the problem by adding one line:
textView.string = s;
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]];
[textView scrollToEndOfDocument:self];
That runUntilDate call shouldn't be necessary. My theory is that it gives the NSScrollView a chance to catch up and synchronize itself, somehow.
This is all on Lion. I tried it with the System Preference set to both the Lion "backwards" scrolling and the the traditional pre-Lion scrolling, with identical results.
Any ideas about:
Why that call I added helped, and
How to make it work without that call?
Try NSScrollView's reflectScrolledClipView:
Edit: How about replacing
[[scrollViewText contentView] scrollToPoint:NSMakePoint(0, textView.frame.size.height)];
with
[scrollView.contentView scrollToPoint:NSMakePoint(0, scrollView.documentView.frame.size.height-scrollView.contentSize.height)];
Because as it stands I believe you're scrolling past the document view's frame.

Resources