我正在使用下面的代码将UIImageViews旋转到不同的角度。
self.imageOne.transform = CGAffineTransformMakeRotation(CGFloat(-rotation))
self.imageTwo.transform = CGAffineTransformMakeRotation(CGFloat(-rotation-0.1))
self.imageThree.transform = CGAffineTransformMakeRotation(CGFloat(-change+0.1))
有没有办法让图像从顶部而不是中间转动?我想避免让精灵参与,因为我从未使用它们,也不知道如何使sprite在单个视图应用程序中工作。谢谢你的时间。
答案 0 :(得分:24)
您要查找的是视图图层的anchorPoint属性。此属性基本上表示在移动视图时使用的句柄。
它默认为视图的中心,这就是为什么你总是看到你的视图从中间旋转。
您可以使用我在SO上找到的这个很棒的方法将您的视图的anchorPoint更改为顶部(使用CGPointMake(0,0.5f))
// Swift version
// Place this before you set the transform property
setAnchorPoint(CGPointMake(0, 0.5), view: imageOne)
setAnchorPoint(CGPointMake(0, 0.5), view: imageTwo)
setAnchorPoint(CGPointMake(0, 0.5), view: imageThree)
func setAnchorPoint(anchorPoint: CGPoint, 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 : CGPoint = 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;
}
这是相同代码的客观c版本
// Obj-c version
// Place this before you set the transform property
[self setAnchorPoint:CGPointMake(0, 0.5f) forView:imageOne];
[self setAnchorPoint:CGPointMake(0, 0.5f) forView:imageTwo];
[self setAnchorPoint:CGPointMake(0, 0.5f) forView:imageThree];
-(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;
}
我建议您在看到anchorPoint属性所代表的内容后,尝试了解此代码的用途。 (该方法由用户:Anand K)
答案 1 :(得分:5)
这是基于mttcrp答案的Swift 3扩展程序。我花了一段时间才发现CGPoint不是视图中的坐标,而是一个比例。例如,如果围绕点CGPoint(x: 0.5, y: 1)
旋转视图,它将围绕视图的底部中心旋转。希望这有帮助!
extension UIView{
func setAnchorPoint(anchorPoint: CGPoint) {
var newPoint = CGPoint(x: self.bounds.size.width * anchorPoint.x, y: self.bounds.size.height * anchorPoint.y)
var oldPoint = CGPoint(x: self.bounds.size.width * self.layer.anchorPoint.x, y: self.bounds.size.height * self.layer.anchorPoint.y)
newPoint = newPoint.applying(self.transform)
oldPoint = oldPoint.applying(self.transform)
var position : CGPoint = self.layer.position
position.x -= oldPoint.x
position.x += newPoint.x;
position.y -= oldPoint.y;
position.y += newPoint.y;
self.layer.position = position;
self.layer.anchorPoint = anchorPoint;
}
}
你会像这样使用它:
let view = UIView() //This could be anything that inherits from UIView
view.setAnchorPoint(anchorPoint: CGPoint(x: 0.5, y: 1))