gpt4 book ai didi

ios - UIView Swift 倾斜动画

转载 作者:搜寻专家 更新时间:2023-11-01 05:52:00 26 4
gpt4 key购买 nike

我有一个 UIImageView,我希望它在我调用一个函数时,图像从平坦(正常状态)到倾斜到右旋转(比如可能旋转 20 度),然后回到它是平坦(正常)状态。

enter image description here

这就是我目前拥有的,但我无法获得想要的结果。

extension UIView {

func rotate(duration: CFTimeInterval = 2) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(20)
rotateAnimation.isRemovedOnCompletion = true
rotateAnimation.duration = duration
rotateAnimation.repeatCount=Float.infinity
self.layer.add(rotateAnimation, forKey: nil)
}

}

UIImageView.rotate()

最佳答案

我尝试创建上面的动画,我尝试过的代码在这里

要求的声明

///View Outlet - Image or UIView
@IBOutlet weak var baseView: UIImageView!

/// Timer - To make Animation in repititive State
var newNAimationTimer : Timer?

/// Plus Degree
let degrees : CGFloat = 20.0
/// Minus Degree
let minusDegree : CGFloat = -20.0

定时器功能

//MARK: Start Timer
func startTimer()
{
/// Start timer if Timer is Not Initialised Before
if newNAimationTimer == nil {
/// Assign Timer
newNAimationTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(animationVC.animateView), userInfo: nil, repeats: true)
}
}

//MARK: Stop Timer
func stopTimer()
{
///Check Do timer is Initialised
if newNAimationTimer != nil {
/// Yes Stop
newNAimationTimer?.invalidate()
}
}

定时器处理程序

/// Animate View With Animations
@objc func animateView()
{
let plusRadian : CGFloat = degrees * CGFloat((Double.pi/180))
let minusRadian : CGFloat = minusDegree * CGFloat((Double.pi/180))

/// First Animation To make View Goes clockwise
UIView.animate(withDuration: 0.5, animations: {
self.baseView.transform = CGAffineTransform(rotationAngle: plusRadian)
}) { (success) in
/// Second Animation to make view go antiClockWise
UIView.animate(withDuration: 0.5, animations: {
self.baseView.transform = CGAffineTransform(rotationAngle: minusRadian)
})
}
}

按钮操作

@IBAction func animationButton(_ sender: Any) {
/// Start the Animation
startTimer()
}

屏幕截图

首先: enter image description here

第二个: enter image description here

运行输出enter image description here

关于ios - UIView Swift 倾斜动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49933629/

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