我是iOS Swift开发的新手,我尝试在一个动画中结合三个参数,但我没有成功。
我认为解决方案就在这里 - Apple Dev Core Animation Programming Guide通过对动画进行分组,但作为初学者,经过大量的互联网研究,我无法找到我想要的东西。
您如何看待我的代码以及为您提供的最佳解决方案,将性能与稳定性结合起来。
我想指出这个动画的目的是创建一个动画的Splashscreen。还有其他元素(UIImage)将用于动画。
这是我的代码:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
logoImg.alpha = 0
logoImg.transform = CGAffineTransform(translationX: 0, y: -200)
logoImg.transform = CGAffineTransform(scaleX: 0, y: 0)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 1.0, delay: 1.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [.curveEaseOut], animations: {
self.logoImg.transform = CGAffineTransform(translationX: 0, y: 0)
self.logoImg.transform = CGAffineTransform(scaleX: 1, y: 1)
self.logoImg.alpha = 1
}, completion: nil)
}
答案 0 :(得分:11)
根据我所看到的,您想要预设动画并将其翻译回来。在那种情况下,我会这样做。
self.logoImg.transform = CGAffineTransform(translationX: 0, y: -200).concatenating(CGAffineTransform(scaleX: 0, y: 0))
self.logoImg.alpha = 0
UIView.animate(withDuration: 1.0, delay: 1.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [.curveEaseOut], animations: {
self.logoImg.transform = .identity
self.logoImg.alpha = 1
}, completion: nil)
我想你可能没有看到所有动画,所以尝试以0.5
开始比例self.logoImg.transform = CGAffineTransform(translationX: 0, y: -200).concatenating(CGAffineTransform(scaleX: 0.5, y: 0.5))
self.logoImg.alpha = 0
UIView.animate(withDuration: 1.0, delay: 1.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 1, options: [.curveEaseOut], animations: {
self.logoImg.transform = .identity
self.logoImg.alpha = 1
}, completion: nil)
这里的关键是动画动画回原始身份。希望这有帮助
答案 1 :(得分:2)
您可以使用concatenating
方法组合两个现有的仿射变换。
UIView.animate(withDuration: 1.0, delay: 1.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [.curveEaseOut], animations: {
let translation = CGAffineTransform(translationX: 0, y: 0)
let scale = CGAffineTransform(scaleX: 1, y: 1)
self.logoImg.transform = translation.concatenating(scale)
self.logoImg.alpha = 1
}, completion: nil)
查看Apple Document了解更多信息。希望它有所帮助。 :)
答案 2 :(得分:0)
你也可以在swift 3.0.1中使用它:
UIView.transition(with: self.imageView,
duration:0.5,
options: .transitionCrossDissolve,
animations: { self.imageView.image = newImage },
completion: nil)
答案 3 :(得分:0)
连接变换尚未工作。这工作
let outCenterX = outFrame.origin.x + outFrame.size.width/2
let inCenterX = inFrame.origin.x + inFrame.size.width/2
let transX = inCenterX - outCenterX // translate X
let transY = outFrame.size.height/2 // translate Y
let scale = CGFloat(0)
self.transform = CGAffineTransform(
a: scale, b: 0.0,
c: 0.0, d: scale,
tx: transX, ty: transY)
alpha = 0
UIView.animate(withDuration: 0.25, animations: {
self.alpha = 1.0
self.transform = .identity
})
答案 4 :(得分:0)
旋转和翻译并使其像tableview标题中的视差效果一样:
func setupTableHeaderView() {
self.customHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 200))
self.customHeaderView?.backgroundColor = .white
self.customImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 200))
self.customImageView?.image = ImageNamed(name: "camera")
self.customImageView?.contentMode = .center
self.customHeaderView?.addSubview(self.customImageView!)
self.tableHeaderView = self.customHeaderView
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let yPos = scrollView.contentOffset.y
if yPos < 0 {
let scaleX = ((yPos * -1) / 200) + 1
let translateY = yPos / 2
var commonTransform = CGAffineTransform.identity
commonTransform = commonTransform.translatedBy(x: 0, y: translateY)
commonTransform = commonTransform.scaledBy(x: scaleX, y: scaleX)
self.customImageView?.transform = commonTransform
}else{
self.customImageView?.transform = CGAffineTransform.identity
}
}
注意:确保您应用转换视图是标题视图的SUBVIEW,而不是标题视图本身。在上面的示例中,customImageView是主标题视图的子视图。