I am new to Swift and trying to build an interactive book as I learn. I have created the cover effect using CATransform3DMakeRotation.
As you can see, the issue comes when I stack the views. If I try to add a back cover the stack order gets lost as the animation triggers.
My code so far:
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);
}
}
Open to any and all feedback. As I said I am a noob. for example, I am using UIButton as I'd like the covers to be clickable. Not sure of that is right or not ;)
Thank you!
Found it:
frontcover.layer.zPosition = 300.0;
Full explanation at Smooth horizontal flip using CATransform3DMakeRotation
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.