macos – 旋转NSImageView的CALayer 90度

macos – 旋转NSImageView的CALayer 90度,第1张

概述我正在尝试旋转NS ImageView的CALayer.我的问题是anchorPoint默认显示为(0.0,0.0),但我希望它是图像的中心(Apple的文档表明默认值应该是中心(0.5,0.5),但它不在我的系统 *** 作系统上X 10.7),当我修改锚点时,图像中心的原点移动到左下角. 这是我用来旋转图层的代码: CALayer *myLayer = [imageView layer];CGFlo 我正在尝试旋转NS ImageVIEw的CALayer.我的问题是anchorPoint默认显示为(0.0,0.0),但我希望它是图像的中心(Apple的文档表明默认值应该是中心(0.5,0.5),但它不在我的系统 *** 作系统上X 10.7),当我修改锚点时,图像中心的原点移动到左下角.

这是我用来旋转图层的代码:

CALayer *myLayer = [imageVIEw layer];CGfloat myRotationAngle = -M_PI_2;NSNumber *rotationAtStart = [myLayer valueForKeyPath:@"transform.rotation"];CAtransform3D myRotationtransform = CAtransform3DRotate(myLayer.transform,myRotationAngle,0.0,1.0);myLayer.transform = myRotationtransform;        CABasicAnimation *myAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];myAnimation.duration = 1.0;myAnimation.fromValue = rotationAtStart;myAnimation.tovalue = [NSNumber numberWithfloat:([rotationAtStart floatValue] + myRotationAngle)];[myLayer addAnimation:myAnimation forKey:@"transform.rotation"];myLayer.anchorPoint = CGPointMake(0.5,0.5);[myLayer addAnimation:myAnimation forKey:@"transform.rotation"];
解决方法 似乎改变某些东西的锚点也会改变它的位置.以下代码段修复了此问题:

CGPoint anchorPoint = CGPointMake(0.5,0.5);  CGPoint newPoint = CGPointMake(disclosureTriangle.bounds.size.wIDth * anchorPoint.x,disclosureTriangle.bounds.size.height * anchorPoint.y);  CGPoint oldPoint = CGPointMake(disclosureTriangle.bounds.size.wIDth * disclosureTriangle.layer.anchorPoint.x,disclosureTriangle.bounds.size.height * disclosureTriangle.layer.anchorPoint.y);  newPoint = CGPointApplyAffinetransform(newPoint,[disclosureTriangle.layer affinetransform]);  oldPoint = CGPointApplyAffinetransform(oldPoint,[disclosureTriangle.layer affinetransform]);  CGPoint position = disclosureTriangle.layer.position;  position.x -= oldPoint.x;  position.x += newPoint.x;  position.y -= oldPoint.y;  position.y += newPoint.y;  disclosureTriangle.layer.position = position;  disclosureTriangle.layer.anchorPoint = anchorPoint;
总结

以上是内存溢出为你收集整理的macos – 旋转NSImageView的CALayer 90度全部内容,希望文章能够帮你解决macos – 旋转NSImageView的CALayer 90度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1001190.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存