ios - 动画期间 subview 堆叠

标签 ios swift

我是 Swift 新手,并尝试在学习过程中编写一本交互式书籍。我使用 CATransform3DMakeRotation 创建了封面效果。

enter image description here



正如您所看到的,当我堆叠 View 时,问题就出现了。如果我尝试添加后盖,则堆栈顺序会随着动画触发而丢失。

到目前为止我的代码:

let bookcontainerwidth = 400
let bookcontainerheight = 200
let bookcoverwidth = bookcontainerwidth/2
let bookcoverheight = bookcontainerheight

let flipspeed = 2.0

//container
let container = UIView(frame: CGRect(x: 0, y: 0, width: bookcontainerwidth, height: bookcontainerheight))
container.backgroundColor = UIColor.white

//back cover
let backcover = UIButton(frame: CGRect(x: bookcontainerwidth/2, y: 0, width: bookcoverwidth, height: bookcoverheight))
backcover.backgroundColor = UIColor.darkGray
backcover.setTitle("Back", for: UIControlState.normal)

//front cover
let frontcover = UIButton(frame: CGRect(x: bookcontainerwidth/2, y: 0, width: bookcoverwidth, height: bookcoverheight))
frontcover.backgroundColor = UIColor.gray
frontcover.addTarget(receiver, action: #selector(opencover), for: .touchUpInside)
frontcover.setTitle("Front", for: UIControlState.normal)
frontcover.layer.anchorPoint = CGPoint(x: 0.0, y: 0.5);
frontcover.center = CGPoint(x:bookcoverwidth, y: bookcoverheight/2);

container.addSubview(backcover)
container.addSubview(frontcover)


func opencover(){

    UIView.animate(withDuration: flipspeed){ () -> Void in

        //these do not work
        //container.bringSubview(toFront: frontcover)
        //container.sendSubview(toBack: backcover)

        frontcover.layer.transform = CATransform3DMakeRotation(CGFloat(M_PI), 0.0, 1.0, 0.0);
    }

}

接受任何和所有反馈。正如我所说,我是一个菜鸟。例如,我正在使用 UIButton,因为我希望封面可单击。不确定这是对还是错;)

谢谢!

最佳答案

找到了:

frontcover.layer.zPosition = 300.0;

完整说明位于Smooth horizontal flip using CATransform3DMakeRotation

关于ios - 动画期间 subview 堆叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40813784/

相关文章:

ios - 出于安全原因,如何在iOS中从内存中删除字符串?甚至需要它还是iOS自己处理

ios - 在 viewcontroller 之上放置一个 Tabbar Controller

swift - 如何使用swift中的计数数组移动到下一个对象?

xcode - 编译 Swift 源文件

swift - 可以从 Swift 构造函数返回不同的实例吗?

iphone - 尝试在设备上运行测试时出现奇怪的错误

ios - 推送模态视图时,如何在返回父 View 的过程中传递数据?

iOS 获取两个字符串之间的字符串

ios - paymentQueue - 交易失败

swift - 使用 Snapkit 的自定义 View 在使用时不显示