Ios 双面视图不工作

Ios 双面视图不工作,ios,uiview,catransform3d,catransformlayer,Ios,Uiview,Catransform3d,Catransformlayer,我有一个子类UIView名为TitleView,这个子类所做的就是覆盖layerClass以返回catTransferMLayer 我的标题视图属性有一些子视图;标题背景视图和标题标签 运行代码时,标题视图的顶层可见(绿色背景),但运行翻转动画时,没有动画。代码只是跳转到结束状态。此外,没有可见的底层(红色背景),只有标题视图的反转版本(转换后的标题标签) 在IBOutlet setter中,我有以下代码: @IBOutlet private weak var titleView: TitleV

我有一个子类
UIView
名为
TitleView
,这个子类所做的就是覆盖
layerClass
以返回
catTransferMLayer

我的
标题视图
属性有一些子视图;
标题背景视图
标题标签

运行代码时,标题视图的顶层可见(绿色背景),但运行翻转动画时,没有动画。代码只是跳转到结束状态。此外,没有可见的底层(红色背景),只有
标题视图的反转版本(转换后的
标题标签)

在IBOutlet setter中,我有以下代码:

@IBOutlet private weak var titleView: TitleView! {
    didSet {
        titleView.backgroundColor = UIColor.clearColor()

        let topLayer = CALayer()
        topLayer.backgroundColor = UIColor.greenColor().CGColor
        topLayer.frame = titleView.bounds
        topLayer.doubleSided = false
        topLayer.zPosition = 3

        titleView.layer.addSublayer(topLayer)

        let bottomLayer = CALayer()
        bottomLayer.backgroundColor = UIColor.redColor().CGColor
        bottomLayer.frame = titleView.bounds
        bottomLayer.doubleSided = false
        bottomLayer.zPosition = 2
        bottomLayer.transform = CATransform3DMakeRotation(CGFloat(M_PI), 1, 0, 0)

        titleView.layer.addSublayer(bottomLayer)
    }
}
标题视图
动画代码:

private func setIsCategoriesShowing(showCategories: Bool, animated: Bool)
{
    let alreadyInFinishState = (isShowingCategories == showCategories) ? true : false

    if alreadyInFinishState
    {
        return
    }

    //  Setup animations

    isAnimatingCategories = true

    headerView.superview?.bringSubviewToFront(headerView)

    titleView.layer.setAnchorPointDynamically(CGPoint(x: 0.5, y: 1)) // Change position when updating anchor point

    //  Animate

    let duration: NSTimeInterval = animated ? 0.8 : 0
    let options: UIViewAnimationOptions = (showCategories == true) ? [.CurveEaseIn] : [.CurveEaseOut]
    let newRotationValue: CGFloat = (showCategories == true) ? -179 : 0
    let damping: CGFloat = (showCategories == true) ? 0.7 : 1
    let initialSpringVelocity: CGFloat = (showCategories == true) ? 0.5 : 1

    UIView.animateWithDuration(duration,
        delay: 0,
        usingSpringWithDamping: damping,
        initialSpringVelocity: initialSpringVelocity,
        options: options,
        animations: { () -> Void in

            var rotationAndPerspectiveTransform = CATransform3DIdentity
            rotationAndPerspectiveTransform.m34 = 1 / -500
            rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, newRotationValue, 1, 0, 0);

            self.titleView.layer.sublayerTransform = rotationAndPerspectiveTransform;
        }) { (success) -> Void in

            if showCategories == false
            {
                self.titleView.layer.sublayerTransform = CATransform3DIdentity
            }

            self.isAnimatingCategories = false
            self.isShowingCategories = showCategories
    }
}
private func setIsCategoriesShowing(showCategories: Bool, animated: Bool)
{
    let alreadyInFinishState = (isShowingCategories == showCategories) ? true : false

    if alreadyInFinishState
    {
        return
    }

    //  Housekeeping

    headerView.superview?.bringSubviewToFront(headerView)

    titleView.layer.setAnchorPointDynamically(CGPoint(x: 0.5, y: 1))

    //  Animate

    isAnimatingCategories = true

    let isOpening = (showCategories == true)

    let duration: NSTimeInterval = animated ? 3 : 0
    let damping: CGFloat = isOpening ? 0.7 : 1
    let initialSpringVelocity: CGFloat = isOpening ? 0.5 : 1
    let options: UIViewAnimationOptions = isOpening ? [.CurveEaseIn] : [.CurveEaseOut]

    let newRotationValue: CGFloat = isOpening ? -179 : 0

    var rotationAndPerspectiveTransform = CATransform3DIdentity
    rotationAndPerspectiveTransform.m34 = 1 / -500
    rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, newRotationValue, 1, 0, 0);

    UIView.animateWithDuration(duration,
        delay: 0,
        usingSpringWithDamping: damping,
        initialSpringVelocity: initialSpringVelocity,
        options: options,
        animations: {

            self.titleView.layer.transform = rotationAndPerspectiveTransform;
        }) { (success) -> Void in

            if !isOpening
            {
                self.titleView.layer.transform = CATransform3DIdentity
            }

            self.isAnimatingCategories = !success
            self.isShowingCategories = showCategories
    }
}

好的,经过一系列的尝试和错误,我已经设法(似乎)解决了我的问题。请随便看看

以下是工作代码:

@IBOutlet private weak var titleView: TitleView! {
    didSet {
        let bottomLayer = CALayer()
        bottomLayer.backgroundColor = UIColor.redColor().CGColor
        bottomLayer.frame = titleView.bounds
        bottomLayer.doubleSided = false
        bottomLayer.zPosition = 2
        bottomLayer.transform = CATransform3DMakeRotation(CGFloat(M_PI), 0, 1, 0) // Reverse bottom layer, so when flipped it's visible.

        titleView.layer.addSublayer(bottomLayer)

        //  All subviews are one-sided

        for subview in titleView.subviews
        {
            subview.layer.doubleSided = false
        }
    }
}

@IBOutlet private weak var titleViewBackgroundView: UIView!
动画代码:

private func setIsCategoriesShowing(showCategories: Bool, animated: Bool)
{
    let alreadyInFinishState = (isShowingCategories == showCategories) ? true : false

    if alreadyInFinishState
    {
        return
    }

    //  Setup animations

    isAnimatingCategories = true

    headerView.superview?.bringSubviewToFront(headerView)

    titleView.layer.setAnchorPointDynamically(CGPoint(x: 0.5, y: 1)) // Change position when updating anchor point

    //  Animate

    let duration: NSTimeInterval = animated ? 0.8 : 0
    let options: UIViewAnimationOptions = (showCategories == true) ? [.CurveEaseIn] : [.CurveEaseOut]
    let newRotationValue: CGFloat = (showCategories == true) ? -179 : 0
    let damping: CGFloat = (showCategories == true) ? 0.7 : 1
    let initialSpringVelocity: CGFloat = (showCategories == true) ? 0.5 : 1

    UIView.animateWithDuration(duration,
        delay: 0,
        usingSpringWithDamping: damping,
        initialSpringVelocity: initialSpringVelocity,
        options: options,
        animations: { () -> Void in

            var rotationAndPerspectiveTransform = CATransform3DIdentity
            rotationAndPerspectiveTransform.m34 = 1 / -500
            rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, newRotationValue, 1, 0, 0);

            self.titleView.layer.sublayerTransform = rotationAndPerspectiveTransform;
        }) { (success) -> Void in

            if showCategories == false
            {
                self.titleView.layer.sublayerTransform = CATransform3DIdentity
            }

            self.isAnimatingCategories = false
            self.isShowingCategories = showCategories
    }
}
private func setIsCategoriesShowing(showCategories: Bool, animated: Bool)
{
    let alreadyInFinishState = (isShowingCategories == showCategories) ? true : false

    if alreadyInFinishState
    {
        return
    }

    //  Housekeeping

    headerView.superview?.bringSubviewToFront(headerView)

    titleView.layer.setAnchorPointDynamically(CGPoint(x: 0.5, y: 1))

    //  Animate

    isAnimatingCategories = true

    let isOpening = (showCategories == true)

    let duration: NSTimeInterval = animated ? 3 : 0
    let damping: CGFloat = isOpening ? 0.7 : 1
    let initialSpringVelocity: CGFloat = isOpening ? 0.5 : 1
    let options: UIViewAnimationOptions = isOpening ? [.CurveEaseIn] : [.CurveEaseOut]

    let newRotationValue: CGFloat = isOpening ? -179 : 0

    var rotationAndPerspectiveTransform = CATransform3DIdentity
    rotationAndPerspectiveTransform.m34 = 1 / -500
    rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, newRotationValue, 1, 0, 0);

    UIView.animateWithDuration(duration,
        delay: 0,
        usingSpringWithDamping: damping,
        initialSpringVelocity: initialSpringVelocity,
        options: options,
        animations: {

            self.titleView.layer.transform = rotationAndPerspectiveTransform;
        }) { (success) -> Void in

            if !isOpening
            {
                self.titleView.layer.transform = CATransform3DIdentity
            }

            self.isAnimatingCategories = !success
            self.isShowingCategories = showCategories
    }
}

好的,经过一系列的尝试和错误,我已经设法(似乎)解决了我的问题。请随便看看

以下是工作代码:

@IBOutlet private weak var titleView: TitleView! {
    didSet {
        let bottomLayer = CALayer()
        bottomLayer.backgroundColor = UIColor.redColor().CGColor
        bottomLayer.frame = titleView.bounds
        bottomLayer.doubleSided = false
        bottomLayer.zPosition = 2
        bottomLayer.transform = CATransform3DMakeRotation(CGFloat(M_PI), 0, 1, 0) // Reverse bottom layer, so when flipped it's visible.

        titleView.layer.addSublayer(bottomLayer)

        //  All subviews are one-sided

        for subview in titleView.subviews
        {
            subview.layer.doubleSided = false
        }
    }
}

@IBOutlet private weak var titleViewBackgroundView: UIView!
动画代码:

private func setIsCategoriesShowing(showCategories: Bool, animated: Bool)
{
    let alreadyInFinishState = (isShowingCategories == showCategories) ? true : false

    if alreadyInFinishState
    {
        return
    }

    //  Setup animations

    isAnimatingCategories = true

    headerView.superview?.bringSubviewToFront(headerView)

    titleView.layer.setAnchorPointDynamically(CGPoint(x: 0.5, y: 1)) // Change position when updating anchor point

    //  Animate

    let duration: NSTimeInterval = animated ? 0.8 : 0
    let options: UIViewAnimationOptions = (showCategories == true) ? [.CurveEaseIn] : [.CurveEaseOut]
    let newRotationValue: CGFloat = (showCategories == true) ? -179 : 0
    let damping: CGFloat = (showCategories == true) ? 0.7 : 1
    let initialSpringVelocity: CGFloat = (showCategories == true) ? 0.5 : 1

    UIView.animateWithDuration(duration,
        delay: 0,
        usingSpringWithDamping: damping,
        initialSpringVelocity: initialSpringVelocity,
        options: options,
        animations: { () -> Void in

            var rotationAndPerspectiveTransform = CATransform3DIdentity
            rotationAndPerspectiveTransform.m34 = 1 / -500
            rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, newRotationValue, 1, 0, 0);

            self.titleView.layer.sublayerTransform = rotationAndPerspectiveTransform;
        }) { (success) -> Void in

            if showCategories == false
            {
                self.titleView.layer.sublayerTransform = CATransform3DIdentity
            }

            self.isAnimatingCategories = false
            self.isShowingCategories = showCategories
    }
}
private func setIsCategoriesShowing(showCategories: Bool, animated: Bool)
{
    let alreadyInFinishState = (isShowingCategories == showCategories) ? true : false

    if alreadyInFinishState
    {
        return
    }

    //  Housekeeping

    headerView.superview?.bringSubviewToFront(headerView)

    titleView.layer.setAnchorPointDynamically(CGPoint(x: 0.5, y: 1))

    //  Animate

    isAnimatingCategories = true

    let isOpening = (showCategories == true)

    let duration: NSTimeInterval = animated ? 3 : 0
    let damping: CGFloat = isOpening ? 0.7 : 1
    let initialSpringVelocity: CGFloat = isOpening ? 0.5 : 1
    let options: UIViewAnimationOptions = isOpening ? [.CurveEaseIn] : [.CurveEaseOut]

    let newRotationValue: CGFloat = isOpening ? -179 : 0

    var rotationAndPerspectiveTransform = CATransform3DIdentity
    rotationAndPerspectiveTransform.m34 = 1 / -500
    rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, newRotationValue, 1, 0, 0);

    UIView.animateWithDuration(duration,
        delay: 0,
        usingSpringWithDamping: damping,
        initialSpringVelocity: initialSpringVelocity,
        options: options,
        animations: {

            self.titleView.layer.transform = rotationAndPerspectiveTransform;
        }) { (success) -> Void in

            if !isOpening
            {
                self.titleView.layer.transform = CATransform3DIdentity
            }

            self.isAnimatingCategories = !success
            self.isShowingCategories = showCategories
    }
}

更新:将标题EWBACKGROUNDVIEW
的背景色设置为清晰的颜色将显示红色的底层。但是仍然没有动画发生。此外,反转版本在不应该更新时显示反转的
标题标签
:将
标题背景视图
的背景颜色设置为清晰的颜色显示红色的底层。但是仍然没有动画发生。而且,反转版本显示了一个反转的
标题标签,而它不应该显示