标签:
当我们需要做旋转动画时,默认情况都是以视图的中点作为轴进行旋转的,但有时我们需要改变旋转的轴,这个时候需要通过改变视图layer的anchorPoint属性来满足需求。
anchorPoint是一个CGPoint类型,取值范围为(0, 0)~(1, 1),默认值为(0.5, 0.5)。但直接改变anchorPoint会意外的发现视图的位置改变了。看下面这个例子:
self.aView = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 120)) self.aView.backgroundColor = UIColor.blackColor() self.view.addSubview(aView) println("view‘s bounds: \(self.aView.bounds)") println("view‘s frame: \(self.aView.frame)") println("view‘s center: \(self.aView.center)") println("layer‘s bounds: \(self.aView.layer.bounds)") println("layer‘s frame: \(self.aView.layer.frame)") println("layer‘s position: \(self.aView.layer.position)") println("layer‘s anchorPoint: \(self.aView.layer.anchorPoint)") self.aView.layer.anchorPoint = CGPoint(x: 0, y: 0) println() println("view‘s bounds: \(self.aView.bounds)") println("view‘s frame: \(self.aView.frame)") println("view‘s center: \(self.aView.center)") println("layer‘s bounds: \(self.aView.layer.bounds)") println("layer‘s frame: \(self.aView.layer.frame)") println("layer‘s position: \(self.aView.layer.position)") println("layer‘s anchorPoint: \(self.aView.layer.anchorPoint)")这段代码很容易理解,创建了一个UIView,是一个边长为120的正方形,起点为屏幕的原点。当改变其anchorPoint时发现它在屏幕中的位置改变了。通过打印view和layer位置相关的属性,发现view的center与layer的position、anchorPoint是关联在一起的,在屏幕中指同一个点。改变anchorPoint时,这三个值还是指向同一个点,但layer的frame的origin将会改变。
为了解决这个问题有两种方法:
1. 定义一个私有方法传入改变后的anchorPoint,和需要改变anchorPoint的view
private func setAnchorPoint(anchoPoint: CGPoint, forView view: UIView) { var newPoint: CGPoint = CGPoint(x: view.bounds.width * anchoPoint.x, y: view.bounds.height * anchoPoint.y) var oldPoint: CGPoint = CGPoint(x: view.bounds.width * view.layer.anchorPoint.y, y: view.bounds.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 = anchoPoint }2. 直接修改该view的frame
var originRect = self.aView.frame self.aView.layer.anchorPoint = CGPoint(x: 0, y: 0) self.aView.frame = originRect
经过测试,两种方式的结果是相同的。
标签:
原文地址:http://my.oschina.net/u/1032381/blog/483901