Rotated CALayer and issues with coordinates (should I convert them manually?)
Rotated CALayer and issues with coordinates (should I convert them manually?)
- Subject: Rotated CALayer and issues with coordinates (should I convert them manually?)
- From: Daniele Margutti <email@hidden>
- Date: Sat, 27 Apr 2013 11:48:05 +0200
Hi guys,
I've an NSView hierarchy with:
- an NSWindow with inside an NSView (called BaseContainerView) (yellow color)
- inside BaseContainerView another NSView called HostView (centered) (orange color)
- inside HostView.layer a CALayer called subLayer (red color)
Now, everything works well when I try convert coordinates through the hierarchy.
That's my code:
- (void) mouseDownOnBaseContainer:(NSNotification *) notif {
NSEvent *theEvent = notif.object;
// Location in NSWindow
CGPoint locationInWindow = theEvent.locationInWindow;
// Location is root NSView (baseContainerView)
CGPoint locationInBaseContainer = [baseContainerView convertPoint:locationInWindow fromView:nil];
// Location in sub NSView (hostView)
CGPoint locationInHostView = [baseContainerView convertPoint:locationInBaseContainer toView:hostView];
// Location in sub CALayer (child of hostView's layer, subLayer)
CGPoint locationInSubLayer = [hostView.layer convertPoint:locationInHostView toLayer:subLayer];
NSLog(@"--------------- CLICK ---------------");
NSLog(@"NSWindow: {%0.0f,%0.0f}",locationInWindow.x,locationInWindow.y);
NSLog(@"BaseContainerView: {%0.0f,%0.0f}",locationInBaseContainer.x,locationInBaseContainer.y);
NSLog(@"HostView: {%0.0f,%0.0f}",locationInHostView.x,locationInHostView.y);
NSLog(@"SubLayer: {%0.0f,%0.0f}",locationInSubLayer.x,locationInSubLayer.y);
}
If I try to rotate my hostView.layer (CALayer) using the function below I get wrong coordinates when I try to click at the same (rotated) point (the top,left coordinate of hostView)
- (IBAction)btn_rotateHostView:(id)sender {
isMovingToLandscape = !isMovingToLandscape;
CALayer *hostViewLayer = hostView.layer;
hostViewLayer.anchorPoint = CGPointMake(0.5f, 0.5f); // 0.5,0.5 made the center of the object
hostViewLayer.frame = CGRectMake((NSWidth(baseContainerView.frame) - NSWidth(hostViewLayer.frame)) / 2.0f,
(NSHeight(baseContainerView.frame) - NSHeight(hostViewLayer.frame)) / 2.0f,
hostViewLayer.frame.size.width,
hostViewLayer.frame.size.height);
[CATransaction begin];
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath: @"transform"];
CATransform3D rotateTransform = CATransform3DMakeRotation (DEGREES_TO_RADIANS (-90), 0, 0, 1);
CATransform3D restoreTransform = CATransform3DIdentity;
animation.fromValue = [NSValue valueWithCATransform3D: (isMovingToLandscape ? restoreTransform : rotateTransform)];
animation.toValue = [NSValue valueWithCATransform3D: (isMovingToLandscape ? rotateTransform : restoreTransform)];
animation.duration = 1.25;
animation.cumulative = NO;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[hostViewLayer addAnimation:animation forKey:@"transform"];
[CATransaction commit];
}
This is a visual feeback:
http://i.imgur.com/LYywXXL.png
While here i've uploaded source code
https://dl.dropboxusercontent.com/u/103260/TestinLayer.zip
Should I convert coordinates manually? Or convertPoint: methods take in place rotation transform?
I should expect 4,5 at the same "logical" point (top-left of rotated hostView)
Any idea?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden