這是一篇關(guān)于實現(xiàn)一種注冊登錄頁轉(zhuǎn)場動效的文章,基于Swift編程語言和iOS開發(fā)平臺。首先上效果圖
動效.gif
下面就來分析一下這個效果的實現(xiàn)。
轉(zhuǎn)場動畫是一個3D旋轉(zhuǎn),由于3D旋轉(zhuǎn)特性會導(dǎo)致view上的內(nèi)容同樣做出翻轉(zhuǎn)效果,所以要是讓整個view轉(zhuǎn)180度的話,字就會變成反的,因此這里我實際做了讓整個view轉(zhuǎn)了90度,再讓它轉(zhuǎn)回原始位置。在轉(zhuǎn)到90度的時候,我們是無法看到view上的內(nèi)容的,所以在這個時候進行內(nèi)容的更改,等它再轉(zhuǎn)回來的時候就會看到內(nèi)容的變換了。這里貼出核心代碼,你只需要把closure閉包中的代碼替換掉即可測試效果了。
@IBAction func goRegister(_ sender: UIButton) {
self.viewTransform(closure: {
self.welcomeLabel.isHidden = true
self.titleImgView.image = UIImage.init(named: "pic_loginhome_signin")
self.initialView.isHidden = true
self.registerView.isHidden = false
self.loginView.isHidden = true
self.customNaviBar.rightBtnText = "登錄"
self.customNaviBar.rightBtn.removeTarget(self, action: #selector(self.goRegister(_:)), for: .touchUpInside)
self.customNaviBar.rightBtn.addTarget(self, action: #selector(self.goLogin(_:)), for: .touchUpInside)
})
}
@IBAction func goLogin(_ sender: UIButton) {
self.viewTransform(closure: {
self.welcomeLabel.isHidden = true
self.titleImgView.image = UIImage.init(named: "pic_loginhome_login")
self.initialView.isHidden = true
self.loginView.isHidden = false
self.registerView.isHidden = true
self.customNaviBar.rightBtnText = "注冊"
self.customNaviBar.rightBtn.removeTarget(self, action: #selector(self.goLogin(_:)), for: .touchUpInside)
self.customNaviBar.rightBtn.addTarget(self, action: #selector(self.goRegister(_:)), for: .touchUpInside)
})
}
func viewTransform(closure: @escaping () -> Void) {
var transform = CATransform3DIdentity
let angle = CGFloat(-Double.pi/2)
transform.m34 = -1 / 500
UIView.animate(withDuration: 1, animations: {() -> Void in
self.view.layer.transform = CATransform3DRotate(transform, angle, 0, 1, 0)
}, completion: {(_ finished: Bool) -> Void in
closure()
UIView.animate(withDuration: 1, animations: {() -> Void in
self.view.layer.transform = CATransform3DRotate(transform, 0
, 0, 1, 0)
})
})
}