Ken*_*ker 127 iphone cocoa cocoa-touch uiview
我想改变anchorPoint
,但保持视图在同一个地方.我试过NSLog
-ing分词self.layer.position
和self.center
他们都保持不变,无论更改anchorPoint的.然而我的观点却在变化
关于如何做到这一点的任何提示?
self.layer.anchorPoint = CGPointMake(0.5, 0.5);
NSLog(@"center point: %f %f", self.layer.position.x, self.layer.position.y);
self.layer.anchorPoint = CGPointMake(1, 1);
NSLog(@"center point: %f %f", self.layer.position.x, self.layer.position.y);
Run Code Online (Sandbox Code Playgroud)
输出是:
2009-12-27 20:43:24.161 Type[11289:207] center point: 272.500000 242.500000
2009-12-27 20:43:24.162 Type[11289:207] center point: 272.500000 242.500000
Run Code Online (Sandbox Code Playgroud)
小智 203
我有同样的问题.Brad Larson的解决方案即使在旋转视图时也能很好地工作.这是他的解决方案翻译成代码.
-(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view
{
CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x,
view.bounds.size.height * anchorPoint.y);
CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x,
view.bounds.size.height * view.layer.anchorPoint.y);
newPoint = CGPointApplyAffineTransform(newPoint, view.transform);
oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);
CGPoint position = view.layer.position;
position.x -= oldPoint.x;
position.x += newPoint.x;
position.y -= oldPoint.y;
position.y += newPoint.y;
view.layer.position = position;
view.layer.anchorPoint = anchorPoint;
}
Run Code Online (Sandbox Code Playgroud)
而快速的等价物:
func setAnchorPoint(anchorPoint: CGPoint, forView view: UIView) {
var newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y)
var oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y)
newPoint = CGPointApplyAffineTransform(newPoint, view.transform)
oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform)
var position = view.layer.position
position.x -= oldPoint.x
position.x += newPoint.x
position.y -= oldPoint.y
position.y += newPoint.y
view.layer.position = position
view.layer.anchorPoint = anchorPoint
}
Run Code Online (Sandbox Code Playgroud)
SWIFT 4.x
func setAnchorPoint(anchorPoint: CGPoint, forView view: UIView) {
var newPoint = CGPoint(x: view.bounds.size.width * anchorPoint.x,
y: view.bounds.size.height * anchorPoint.y)
var oldPoint = CGPoint(x: view.bounds.size.width * view.layer.anchorPoint.x,
y: view.bounds.size.height * view.layer.anchorPoint.y)
newPoint = newPoint.applying(view.transform)
oldPoint = oldPoint.applying(view.transform)
var position = view.layer.position
position.x -= oldPoint.x
position.x += newPoint.x
position.y -= oldPoint.y
position.y += newPoint.y
view.layer.position = position
view.layer.anchorPoint = anchorPoint
}
Run Code Online (Sandbox Code Playgroud)
Bra*_*son 137
Core Animation Programming Guide 的Layer Geometry and Transforms部分解释了CALayer的position和anchorPoint属性之间的关系.基本上,层的位置是根据图层的anchorPoint的位置指定的.默认情况下,图层的anchorPoint为(0.5,0.5),位于图层的中心.设置图层的位置时,您将在其超层的坐标系中设置图层中心的位置.
因为位置是相对于图层的anchorPoint,所以在保持相同位置的同时更改该anchorPoint会移动图层.为了防止这种移动,您需要调整图层的位置以考虑新的anchorPoint.我这样做的一种方法是抓取图层的边界,将边界的宽度和高度乘以旧的和新的anchorPoint的标准化值,取两个anchorPoints的差值,并将该差值应用于图层的位置.
您甚至可以通过使用CGPointApplyAffineTransform()
UIView的CGAffineTransform以这种方式考虑轮换.
Ken*_*ker 44
解决这个问题的关键是使用frame属性,这是奇怪的唯一改变.
斯威夫特2
let oldFrame = self.frame
self.layer.anchorPoint = CGPointMake(1, 1)
self.frame = oldFrame
Run Code Online (Sandbox Code Playgroud)
斯威夫特3
let oldFrame = self.frame
self.layer.anchorPoint = CGPoint(x: 1, y: 1)
self.frame = oldFrame
Run Code Online (Sandbox Code Playgroud)
然后我做我的调整大小,它从anchorPoint扩展.然后我必须恢复旧的anchorPoint;
斯威夫特2
let oldFrame = self.frame
self.layer.anchorPoint = CGPointMake(0.5,0.5)
self.frame = oldFrame
Run Code Online (Sandbox Code Playgroud)
斯威夫特3
let oldFrame = self.frame
self.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.frame = oldFrame
Run Code Online (Sandbox Code Playgroud)
编辑:如果视图被旋转,这会剥落,因为如果已应用CGAffineTransform,则帧属性未定义.
2cu*_*ech 27
当我开始将它与我对UIView中的frame.origin的理解进行比较时,对我来说理解position
并且anchorPoint
是最简单的.带有frame.origin =(20,30)的UIView意味着UIView距左侧20点,距其父视图顶部30点.这个距离是从UIView的哪个点算出的?它是从UIView的左上角计算出来的.
在层中anchorPoint
标记计算该距离的点(以标准化形式,即0到1),例如,layer.position =(20,30)表示该层anchorPoint
距离其父层的左侧20个点和顶部30个点.默认情况下,图层anchorPoint为(0.5,0.5),因此距离计算点位于图层的中心.下图将有助于澄清我的观点:
anchorPoint
也恰好是在将变换应用于图层时将发生旋转的点.
Fri*_*ice 16
有这么简单的解决方案.这是基于Kenny的回答.但是,不是应用旧框架,而是使用它的原点和新框架来计算过渡,然后将该过渡应用到中心.它也适用于旋转视图!这是代码,比其他解决方案简单得多:
func setAnchorPoint(anchorPoint: CGPoint, view: UIView) {
let oldOrigin = view.frame.origin
view.layer.anchorPoint = anchorPoint
let newOrigin = view.frame.origin
let translation = CGPoint(x: newOrigin.x - oldOrigin.x, y: newOrigin.y - oldOrigin.y)
view.center = CGPoint(x: view.center.x - translation.x, y: view.center.y - translation.y)
}
Run Code Online (Sandbox Code Playgroud)
和Swift版本:
func setAnchorPoint(anchorPoint: CGPoint, view: UIView) {
let oldOrigin = view.frame.origin
view.layer.anchorPoint = anchorPoint
let newOrigin = view.frame.origin
let translation = CGPoint(x: newOrigin.x - oldOrigin.x, y: newOrigin.y - oldOrigin.y)
view.center = CGPoint(x: view.center.x - translation.x, y: view.center.y - translation.y)
}
Run Code Online (Sandbox Code Playgroud)
小智 13
对于那些需要它的人来说,这是Magnus在Swift中的解决方案:
func setAnchorPoint(anchorPoint: CGPoint, view: UIView) {
var newPoint: CGPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y)
var oldPoint: CGPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y)
newPoint = CGPointApplyAffineTransform(newPoint, view.transform)
oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform)
var position: CGPoint = view.layer.position
position.x -= oldPoint.x
position.x += newPoint.x
position.y -= oldPoint.y
position.y += newPoint.y
view.setTranslatesAutoresizingMaskIntoConstraints(true) // Added to deal with auto layout constraints
view.layer.anchorPoint = anchorPoint
view.layer.position = position
}
Run Code Online (Sandbox Code Playgroud)
这是针对OS X上的NSView调整的user945711的答案.除了NSView没有.center
属性之外,NSView的框架不会改变(可能是因为NSView默认情况下不带CALayer)但是当更改anchorPoint时CALayer框架原点会发生变化.
func setAnchorPoint(anchorPoint: NSPoint, view: NSView) {
guard let layer = view.layer else { return }
let oldOrigin = layer.frame.origin
layer.anchorPoint = anchorPoint
let newOrigin = layer.frame.origin
let transition = NSMakePoint(newOrigin.x - oldOrigin.x, newOrigin.y - oldOrigin.y)
layer.frame.origin = NSMakePoint(layer.frame.origin.x - transition.x, layer.frame.origin.y - transition.y)
}
Run Code Online (Sandbox Code Playgroud)