为CGAffineTransformMakeRotation Swift设置旋转点
我正在使用下面的代码来旋转UIImageViews到不同的angular度。
self.imageOne.transform = CGAffineTransformMakeRotation(CGFloat(-rotation)) self.imageTwo.transform = CGAffineTransformMakeRotation(CGFloat(-rotation-0.1)) self.imageThree.transform = CGAffineTransformMakeRotation(CGFloat(-change+0.1))
有什么办法可以使图像从顶部而不是从中间旋转? 我想避免让精灵参与,因为我从来没有使用过它们,也不知道如何使精灵在单一视图应用程序中工作。 谢谢你的时间。
你正在寻找的是你的视图层的anchorPoint属性。 这个属性基本上代表了视图被移动时使用的句柄 。
它默认为视图的中心,所以你总是看到你的视图从中间旋转。
您可以使用我在这里find的这个伟大的方法来将您的视图的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 )
这是一个基于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))
- 为什么当我为我的CABasicAnimation设置一个较低的持续时间值时,它会跳转吗?
- iOS 10 UILabels – > 1 UIView – >通过animation循环
- 如何animationUITableView标题视图
- 如何在Scroll上设置UICollectionViewCell的大小,使其中间最大?
- 隐式animation究竟在iOS中发生?
- 如何通过重复和自动反向来减慢animation
- animationUICollectionViewCell折叠
- 它是如何animationUINavigationBar的条形颜色的变化,而不是一个UITabBar?
- 认定animation类似于在iOS 7中打开应用程序:viewcontroller框架不能resize