使用UIView.transition(from,to)和autoLayout约束

问题描述 投票:3回答:2

我正试图显示一张卡片并轻轻翻转该卡片。

为此我有一个UIView,它充当两个独立的UIViews(frontView,backView)的容器视图。我已将tapGestureRecognizer附加到容器视图,并调用下面的函数。

@objc func flipCard() {
    print("Flipping card")

    if showingBack == true {
        UIView.transition(from: backView, to: frontView, duration: 0.3, options: .transitionFlipFromLeft, completion: nil)
        showingBack = false
    } else {
        UIView.transition(from: frontView, to: backView, duration: 0.3, options: .transitionFlipFromRight, completion: nil)
        showingBack = true
    }
}

第一次翻转工作,翻转不起作用 - 卡片只是消失了。我能够弄清楚,只要我将这两个子视图的AutoresizingMaskIntoConstraints设置为true,翻转就可以正常工作,如下所示:

@objc func flipCard() {
    print("Flipping card")
    frontView.translatesAutoresizingMaskIntoConstraints = true
    backView.translatesAutoresizingMaskIntoConstraints = true

    if showingBack == true {
        UIView.transition(from: backView, to: frontView, duration: 0.3, options: .transitionFlipFromLeft, completion: nil)
        showingBack = false
    } else {
        UIView.transition(from: frontView, to: backView, duration: 0.3, options: .transitionFlipFromRight, completion: nil)
        showingBack = true
    }
}

但是,将AutoresizingMaskIntoConstraints设置为true会在我的视图中导致一些UI冲突,这就是为什么我要避免这种情况。我很感激有关如何解决这个问题的提示。并且有人可以解释为什么这首先发生了这种情况吗?

非常感激!

swift autolayout
2个回答
1
投票

如果translatesAutoresizingMaskIntoConstraints给你带来问题,试试这个:

@objc func flipCard() {
    print("Flipping card")
    frontView.translatesAutoresizingMaskIntoConstraints = true
    backView.translatesAutoresizingMaskIntoConstraints = true

    if showingBack == true {
        UIView.transition(from: backView, to: frontView, duration: 0.3, options: .transitionFlipFromLeft) { (success) in
            frontView.translatesAutoresizingMaskIntoConstraints = false
            backView.translatesAutoresizingMaskIntoConstraints = false
            showingBack = false
        }
    } else {
        UIView.transition(from: frontView, to: backView, duration: 0.3, options: .transitionFlipFromRight) { (success) in
            frontView.translatesAutoresizingMaskIntoConstraints = false
            backView.translatesAutoresizingMaskIntoConstraints = false
            showingBack = true
        }
    }
}

if translatesAutoresizingMaskIntoConstraints = true表示系统创建一组约束,复制视图的自动调整掩码指定的行为。当它为假时,意味着正在使用纯约束(NSLayoutConstraint)。

此外,请注意:

默认情况下,对于以编程方式创建的任何视图,该属性都设置为true。如果在Interface Builder中添加视图,系统会自动将此属性设置为false。


3
投票

我有一个不断翻转的UIView容器,使用false。我所做的是以不同方式处理options参数,将.showHideTransitionViews添加到翻转方向。没有这个,我只是翻了一下然后......没事。

这有点奇怪,因为你不能“只是”添加它(至少在Swift 3 iOS 10天内),但这是你可能尝试的:

let flipFromLeftOptions: UIViewAnimationOptions = [.transitionFlipFromLeft, .showHideTransitionViews]
let flipFromRightOptions: UIViewAnimationOptions = [.transitionFlipFromRight, .showHideTransitionViews]

并在转换代码中:

if showingBack == true {
    UIView.transition(from: backView, to: frontView, duration: 0.3, options: flipFromLeftOptions, completion: nil)
    showingBack = false
} else {
    UIView.transition(from: frontView, to: backView, duration: 0.3, options: flipFromRightOptions, completion: nil)
    showingBack = true
}
© www.soinside.com 2019 - 2024. All rights reserved.