CGPointApplyAffineTransform 中锚点的位置不正确 (iOS/Swift)
Incorrect position of anchor point in CGPointApplyAffineTransform (iOS/Swift)
看来我的buttonView
的anchor point
是根据view
的位置变化的。我希望 buttonView
收缩到自身的中间而不是 view
的中间。我想知道我做错了什么?或者如果我完全误解了锚点?我试着阅读了一些文章,但它们似乎暗示一个对象的锚点是指它自己的坐标而不是它的父视图的坐标。
import UIKit
class ViewController: UIViewController {
var buttonView:UIView!
var toggleButton:UIButton!
var isVisible = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let buttonTitles = ["1", "2", "3"]
let size:CGFloat = 100.0
let viewSize = CGFloat(buttonTitles.count) * size
buttonView = UIView(frame: CGRectMake(40, 40, viewSize, viewSize))
setAnchorPoint(CGPointMake(0.5, 0), forView: buttonView)
buttonView.layer.anchorPoint = CGPointMake(0.5, 0.0)
view.addSubview(buttonView)
for (index, title) in buttonTitles.enumerate() {
let button = UIButton(type: .Custom) as UIButton
button.frame = CGRectMake(0, size * CGFloat(index), size, size)
button.setTitle(title, forState: .Normal)
button.backgroundColor = UIColor.redColor()
buttonView.addSubview(button)
}
toggleButton = UIButton(type: .Custom) as UIButton
toggleButton.frame = CGRectMake(200, 100, 80, 50)
toggleButton.setTitle("Toggle", forState: .Normal)
toggleButton.backgroundColor = UIColor.purpleColor()
toggleButton.addTarget(self, action: #selector(ViewController.animate), forControlEvents: .TouchUpInside)
view.addSubview(toggleButton)
}
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
}
func animate() {
if (isVisible) { // collapse it
isVisible = false
UIView.animateWithDuration(0.5, animations: {
self.buttonView.transform = CGAffineTransformMakeScale(0.2, 0.2)
})
}
else { // expand it
isVisible = true
UIView.animateWithDuration(0.5, animations: {
self.buttonView.transform = CGAffineTransformMakeScale(1.0, 1.0)
})
}
}
}
点击切换前:
点击切换后:
没关系,我只是把 buttonView's
宽度设置得太大了。
看来我的buttonView
的anchor point
是根据view
的位置变化的。我希望 buttonView
收缩到自身的中间而不是 view
的中间。我想知道我做错了什么?或者如果我完全误解了锚点?我试着阅读了一些文章,但它们似乎暗示一个对象的锚点是指它自己的坐标而不是它的父视图的坐标。
import UIKit
class ViewController: UIViewController {
var buttonView:UIView!
var toggleButton:UIButton!
var isVisible = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let buttonTitles = ["1", "2", "3"]
let size:CGFloat = 100.0
let viewSize = CGFloat(buttonTitles.count) * size
buttonView = UIView(frame: CGRectMake(40, 40, viewSize, viewSize))
setAnchorPoint(CGPointMake(0.5, 0), forView: buttonView)
buttonView.layer.anchorPoint = CGPointMake(0.5, 0.0)
view.addSubview(buttonView)
for (index, title) in buttonTitles.enumerate() {
let button = UIButton(type: .Custom) as UIButton
button.frame = CGRectMake(0, size * CGFloat(index), size, size)
button.setTitle(title, forState: .Normal)
button.backgroundColor = UIColor.redColor()
buttonView.addSubview(button)
}
toggleButton = UIButton(type: .Custom) as UIButton
toggleButton.frame = CGRectMake(200, 100, 80, 50)
toggleButton.setTitle("Toggle", forState: .Normal)
toggleButton.backgroundColor = UIColor.purpleColor()
toggleButton.addTarget(self, action: #selector(ViewController.animate), forControlEvents: .TouchUpInside)
view.addSubview(toggleButton)
}
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
}
func animate() {
if (isVisible) { // collapse it
isVisible = false
UIView.animateWithDuration(0.5, animations: {
self.buttonView.transform = CGAffineTransformMakeScale(0.2, 0.2)
})
}
else { // expand it
isVisible = true
UIView.animateWithDuration(0.5, animations: {
self.buttonView.transform = CGAffineTransformMakeScale(1.0, 1.0)
})
}
}
}
点击切换前:
点击切换后:
没关系,我只是把 buttonView's
宽度设置得太大了。