动画期间的子视图堆叠

时间:2016-11-26 00:22:52

标签: ios swift

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

enter image description here



如您所见,当我堆叠视图时会出现问题。如果我尝试添加后盖,则在动画触发时堆栈顺序会丢失。

到目前为止我的代码:

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,因为我希望封面可以点击。不确定这是对的;)

谢谢!

1 个答案:

答案 0 :(得分:0)

找到它:

frontcover.layer.zPosition = 300.0;

Smooth horizontal flip using CATransform3DMakeRotation

的完整说明