Car*_*one 8 animation ios swift
我使用下面的代码将UIImageViews旋转到不同的角度.
self.imageOne.transform = CGAffineTransformMakeRotation(CGFloat(-rotation))
self.imageTwo.transform = CGAffineTransformMakeRotation(CGFloat(-rotation-0.1))
self.imageThree.transform = CGAffineTransformMakeRotation(CGFloat(-change+0.1))
Run Code Online (Sandbox Code Playgroud)
是否有任何方法可以使图像从顶部而不是中间转动?我想避免让精灵参与,因为我从未使用它们,也不知道如何在单个视图应用程序中使精灵工作.谢谢你的时间.
mtt*_*rsp 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;
}
Run Code Online (Sandbox Code Playgroud)
这是相同代码的客观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;
}
Run Code Online (Sandbox Code Playgroud)
一旦你看到了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;
}
}
Run Code Online (Sandbox Code Playgroud)
你会像这样使用它:
let view = UIView() //This could be anything that inherits from UIView
view.setAnchorPoint(anchorPoint: CGPoint(x: 0.5, y: 1))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10007 次 |
| 最近记录: |