Implementing zooming in CALayer - calayer

I'm trying to implement zoom on a CALayer in a layered-hosting NSVIew. The NSView with the layer (size of the layer: 1024x768) is inside an NSScrollView. What I did to implement the zoom is the following
- (IBAction)zoomOut:(NSButton *)sender {
CALayer *layer = self.editorView.layer;
CGRect frame = layer.frame;
frame.size.width = frame.size.width / 2;
frame.size.height = frame.size.height / 2;
layer.frame = frame;
}
- (IBAction)zoomIn:(NSButton *)sender {
CALayer *layer = self.editorView.layer;
CGRect frame = layer.frame;
frame.size.width = frame.size.width * 2;
frame.size.height = frame.size.height * 2;
layer.frame = frame;
}
After clicking on the button for zooming in or zooming out in the NSView, the problem is that whenever i scroll the NSView (remember that I put the NSView inside an NSScrollView, and that the NSView is bigger than the NSScrollView), the CALayer resizes automatically to the original size (i.e. 1024x768). I cannot figure out the reason.
Any kind of help would be much appreciated.

+ (CALayer *)layer alias backing store (I call it root layer) resizes with the view, there is no way to prevent this.

Related

Best practice for resize window with animation using ToolBar on Cocoa

I'm wondering what's the best practice to resize a window when Toolbar changes.
I'm trying to get this effect (animated) when a Toolbar selected option changes.
Any ideas?
Thank you for your help! :)
Here's my commonly used method:
After clicking the new toolbar item, firstly get the frame size of the new subview to be added, then change window's frame with animation.
Demo (just change height, but you can add support to change width):
- (void)switchToTabView:(NSView *)settingView withAnimation:(BOOL)animation
{
NSView *windowView = self.window.contentView;
for (NSView *view in windowView.subviews) {
[view removeFromSuperview];
}
CGFloat oldHeight = windowView.frame.size.height;
[windowView addSubview:settingView];
CGFloat newHeight = settingView.frame.size.height;
CGFloat delta = newHeight - oldHeight;
NSPoint origin = settingView.frame.origin;
origin.y -= delta;
[settingView setFrameOrigin:origin];
NSRect frame = self.window.frame;
frame.size.height += delta;
frame.origin.y -= delta;
[self.window setFrame:frame display:YES animate:animation];
}
#GoKu Your answer gave me a hint how to solve it. I just added a new property for my window called "win".
Here's my solution:
- (void)updateView:(int)tag{
[[_ourViewController view] removeFromSuperview];
if (tag==0) {
self.ourViewController = [[UserView alloc] initWithNibName:#"UserView" bundle:nil];
} else if (tag==1){
self.ourViewController = [[ComputerView alloc] initWithNibName:#"ComputerView" bundle:nil];
}
NSView *newView = _ourViewController.view;
NSRect windowRect = _win.frame;
NSRect currentViewRect = newView.frame;
windowRect.origin.y = windowRect.origin.y + (windowRect.size.height - currentViewRect.size.height);
windowRect.size.height = currentViewRect.size.height;
windowRect.size.width = currentViewRect.size.width;
[self.win setContentView:newView];
[self.win setFrame:windowRect display:YES animate:YES];
}
Thanx!

Double tap to Zoom with UIScrollView

I am using Apple's source code. I am sure there is just a snippet that I have messed up. I have a form that I have made programmatically. When I double tap the recognizer fires and goes through the motions but I it won't actually zoom. Any suggestions? Thanks for the help
imageScrollView.delegate=self;
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
imageScrollView=[[UIScrollView alloc]initWithFrame:fullScreenRect];
imageScrollView.contentSize=CGSizeMake(750,1250);
imageScrollView.minimumZoomScale=1.0;
imageScrollView.maximumZoomScale=5.0;
[imageScrollView setZoomScale:imageScrollView.minimumZoomScale];
self.view=imageScrollView;
[imageScrollView release];
Added a bunch of UItextfields/labels to the scrollview.
aircraftType = [[UITextField alloc]initWithFrame:CGRectMake(170, 32, 150, 15)];
aircraftType.font=[UIFont systemFontOfSize:10];
aircraftType.borderStyle = UITextBorderStyleRoundedRect;
aircraftType.placeholder = #"Aicraft Type";
aircraftType.delegate = self;
[self.view addSubview:aircraftType];
#pragma mark UIScrollViewDelegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [imageScrollView viewWithTag:ZOOM_VIEW_TAG];
}
#pragma mark TapDetectingImageViewDelegate methods
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
// double tap zooms in
float newScale = [imageScrollView zoomScale] * ZOOM_STEP;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[imageScrollView zoomToRect:zoomRect animated:YES];
}
#pragma mark Utility methods
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
CGRect zoomRect;
// the zoom rect is in the content view's coordinates.
// At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
// As the zoom scale decreases, so more content is visible, the size of the rect grows.
zoomRect.size.height = [imageScrollView frame].size.height / scale;
zoomRect.size.width = [imageScrollView frame].size.width / scale;
// choose an origin so as to get the right center.
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
return zoomRect;
}

Resize UICollectionView Height

I'm trying to resize a UICollectionView height by setting it to 0 when the view controller is loaded, then increasing its size with an animation when a button is pressed. I've tried a few different things but it doesn't change in size at all. Here's all the different things I've tried to change its height to 0:
CGRect bounds = [self.collectionView bounds];
[self.collectionView setBounds:CGRectMake(bounds.origin.x,
bounds.origin.y,
bounds.size.width,
bounds.size.height - 100)];
....
CGRect frame = [self.collectionView frame];
[self.collectionView setFrame:CGRectMake(frame.origin.x,
frame.origin.y,
frame.size.width,
frame.size.height - 100)];
....
CGRect frame = self.collectionView.frame;
frame.size.height -= 100;
self.collectionView.frame = frame;
....
self.collectionView.clipsToBounds = YES;
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
If you are using Interface Builder for UICollectionView initialisation, switch off "Use Autolayout" from the File Inspector in the xib file where the your CollectionView is created. Then you can change the height with setFrame or setBounds methods.
You can avoid disabling Autolayout by creating an outlet for the height constraint and then adjusting the constraint's constant in code.
Outlet
#IBOutlet weak var collectionViewVerticalConstraint: NSLayoutConstraint!
Adjustment
collectionViewVerticalConstraint.constant = 0

How to calculate the correct size to zoom a WebView?

I have a WebView in my window. When the user clicks on the Zoom button (the green button on the top left of the window), I want to change the size of the window such that the WebView is just large enough to show the web page, as recommended in the Mac HCI guidelines.
Question is: how to I go about calculating the size of the WebView to zoom to?
Thanks.
Oh sorry, then you should reverse it :)
- (IBAction)resizeToView:(id)sender
{
// webview content sub/document-view
NSView *docView =[[[webView mainFrame] frameView] documentView];
NSRect docRect = [docView frame];
// height and width
int height = docRect.size.height;
int width = docRect.size.width;
NSRect frame = [window frame];
int delta = height - frame.size.height;
// y/height
frame.origin.y -= delta;
frame.size.height += delta;
// x/width
frame.size.width = width;
[window setFrame:frame display:YES];
}

how to stop NSScrollView from scrolling on resize?

I'm writing a PDFViewer for a 3rd party PDF library. I have a subclass of NSScrollView and a subclass of NSView. The problem is the NSScrollView will scroll up and down when I resize window.
here is the relevant code:
whenever windows is resized, my subclass of NSScrollView receive this message:
-(void)handleFrameChange:(NSNotification*)notification {
id * docView = [self documentView];
[docView windowResized];
}
in my docView, a subclass of NSView, containing code to draw pdf
- (void)windowResized;
{
NSSize size = [[self superview] bounds].size;
int width = (int) size.width;
int height = (int) size.height;
_pdfView->OnSize(width, height);
double w = _pdfView->GetCanvasWidth();
double h = _pdfView->GetCanvasHeight();
[self setFrameSize:NSMakeSize(w, h)];
//allocate buffer
_buf.resize(width * height * 4, NULL);
[self setNeedsDisplay:YES];
}

Resources