gpt4 book ai didi

ios - 如何围绕左上角的点旋转图像?

转载 作者:行者123 更新时间:2023-11-28 09:24:32 27 4
gpt4 key购买 nike

我有一个音乐播放器,我希望播放器播放时针头图像在磁盘上。在播放器未播放时将图像围绕左上角点旋转 45°。needleImgView 的约束是 centerX to superView, top to superview = 40, size = (97,153)

我用谷歌搜索并找到了以下代码:

func setAnchorPoint(_ anchorPoint:CGPoint, for view:UIView){

let oldOrigin = view.frame.origin
view.layer.anchorPoint = anchorPoint
let newOrigin = view.frame.origin

var transition = CGPoint()
transition.x = newOrigin.x - oldOrigin.x
transition.y = newOrigin.y - oldOrigin.y
view.center = CGPoint(x: view.center.x - transition.x, y: view.center.y - transition.y)

}

然后用下面的方法旋转图片:

if !player.isPlaying {
setAnchorPoint(CGPoint(x:40 / 97.0,y:40 / 153.0), for: needleImgView) // I'm NOT SURE the 40 is right.
//needleImgView.layer.anchorPoint = CGPoint(x:0,y:0)
UIView.animate(withDuration: 0.5, animations: {
self.needleImgView.transform = CGAffineTransform(rotationAngle: CGFloat(-Double.pi / 4))
}, completion: nil)

}else{
setAnchorPoint(CGPoint(x:40 / 97.0,y:40 / 153.0), for: needleImgView)
//needleImgView.layer.anchorPoint = CGPoint(x:0,y:0)
UIView.animate(withDuration: 0.5, animations: {
self.needleImgView.transform = .identity
}, completion: nil)

}

我设置了 setAnchorPoint(CGPoint(x:0/97.0,y:0/153.0), for: needleImgView)needleImgView.layer.anchorPoint = CGPoint(x:0,y :0),结果是左上角的点也移开了

Screen Shot For The Left-top Point Moving Away

如何解决?谢谢。

最佳答案

你可以尝试CABasicAnimation

anchor x的0.5表示在宽度的中间,y的0表示高度的顶部。

    imageView.layer.anchorPoint = CGPoint.init(x: 0.5, y: 0)

let basic = CABasicAnimation.init(keyPath: "transform.rotation")
basic.fromValue = NSNumber.init(value: 0)
basic.toValue = NSNumber.init(value: -(Double.pi / 4))
basic.fillMode = kCAFillModeForwards
basic.isRemovedOnCompletion = false
imageView.layer.add(basic, forKey: nil)

关于ios - 如何围绕左上角的点旋转图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46681419/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com