Ios 翻转视图,如UIView.transition和核心动画
我想在两个视图之间创建一个转换,如Ios 翻转视图,如UIView.transition和核心动画,ios,swift,animation,Ios,Swift,Animation,我想在两个视图之间创建一个转换,如UIView.transition和.transitionFlipFromLeft。例如: 导入UIKit 类ViewController:UIViewController{ 静态let frame=CGRect(x:0,y:0,宽度:300,高度:200) 让viewContainer=UIView(帧:帧) 让view1=UIView(帧:帧) 让view2=UIView(帧:帧) var currentView:UIView! 重写func viewDid
UIView.transition
和.transitionFlipFromLeft
。例如:
导入UIKit
类ViewController:UIViewController{
静态let frame=CGRect(x:0,y:0,宽度:300,高度:200)
让viewContainer=UIView(帧:帧)
让view1=UIView(帧:帧)
让view2=UIView(帧:帧)
var currentView:UIView!
重写func viewDidLoad(){
super.viewDidLoad()
self.view1.backgroundColor=.blue
self.view2.backgroundColor=.red
self.currentView=self.view1
self.view.addSubview(self.viewContainer)
self.viewContainer.addSubview(self.currentView)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
如果currentView==view1{
翻转(到:视图2)
}否则{
翻转(到:视图1)
}
}
func翻转(到toView:UIView){
UIView.transition(从:currentView,
致:toView,
持续时间:1,
选项:。transitionFlipFromLeft,
完成:{in self.currentView=toView})
}
}
使用核心动画应该可以达到类似的效果。我替换了UIView.transition
中的func-flip(到toView:UIView)
:
func翻转(到toView:UIView){
var transform=CATTransferM3DidEntity
transform.m34=-1/400
toView.layer.transform=转换
self.currentView.layer.transform=转换
let UnsearByRotating=CABasicAnimation(关键路径:“transform.rotation.y”)
消失旋转。持续时间=0.5
消失ByRotating.fromValue=0
消失byrotating.toValue=CGFloat.pi/2
让appearbyroting=cabasicaniation(关键路径:“transform.rotation.y”)
出现发热。持续时间=0.5
appearByRotating.fromValue=CGFloat.pi/2
appearByRotating.toValue=CGFloat.pi
CATransaction.begin()
CATransaction.setCompletionBlock{
self.currentView.removeFromSuperview()
self.viewContainer.addSubview(toView)//一个选项:
- 将90度旋转应用于
toView.layer
,然后再执行其他操作
toView
现在将“不可见”,因此将其添加为子视图
- 在原始完成块中嵌入新的
CATTransaction
和新的完成块
,以“删除”从到View.layer的转换
试试这个:
func flip(to toView: UIView) {
var transform = CATransform3DIdentity
transform.m34 = -1 / 400
// start toView rotated 90 degrees
transform = CATransform3DRotate(transform, CGFloat.pi / 2, 0, 1, 0)
toView.layer.transform = transform
// because toView's layer is rotated 90 degrees, we can
// add the subview here and it won't be visible
// really no difference between .addSubview and .insertSubview for our purposes
// but we'll insert it under the current view anyway
self.viewContainer.insertSubview(toView, belowSubview: self.currentView)
transform = CATransform3DIdentity
transform.m34 = -1 / 400
self.currentView.layer.transform = transform
let disappearByRotating = CABasicAnimation(keyPath: "transform.rotation.y")
disappearByRotating.duration = 0.5
disappearByRotating.fromValue = 0
disappearByRotating.toValue = CGFloat.pi / 2
disappearByRotating.fillMode = .forwards
disappearByRotating.isRemovedOnCompletion = false
let appearByRotating = CABasicAnimation(keyPath: "transform.rotation.y")
appearByRotating.duration = 0.5
appearByRotating.fromValue = -CGFloat.pi / 2
appearByRotating.toValue = 0
appearByRotating.fillMode = .forwards
appearByRotating.isRemovedOnCompletion = false
CATransaction.begin()
CATransaction.setCompletionBlock {
self.currentView.removeFromSuperview()
// start a new CATransaction with new Completion Block
CATransaction.begin()
CATransaction.setCompletionBlock {
self.currentView.layer.removeAnimation(forKey: "disappear")
toView.layer.removeAnimation(forKey: "appear")
// remove 90-degree-rotation starting point from toView
toView.layer.transform = CATransform3DIdentity
self.currentView = toView
}
toView.layer.add(appearByRotating, forKey: "appear")
CATransaction.commit()
}
self.currentView.layer.add(disappearByRotating, forKey: "disappear")
CATransaction.commit()
}
在模拟器上,它看起来很完美。不幸的是,在设备上,它仍然闪烁。在第一个动画结束时,它的视图在第二个动画开始之前恢复到初始大小。第二个动画开始时很好,但最后一帧被删除。哎哟……没有在设备上检查它。添加了几行代码来修复剩余的问题。@andreas1724-我已经看过了.isRemovedOnCompletion
,但没有得到太多…可能应该坚持下去…看起来像。removeAnimation()
也是需要的。很高兴你让它工作起来了。