我正在尝试显示一张卡片并轻轻翻转该卡片。
为此,我有一个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冲突,这就是我想避免这种情况的原因。我很感激有关如何解决这个问题的提示。并且有人可以解释为什么这首先发生了这种情况吗?
非常感谢!
答案 0 :(得分: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
}
答案 1 :(得分: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
}
}
}
如果translatesAutoresizingMaskIntoConstraints = true
意味着
系统创建一组约束,复制视图的自动调整掩码指定的行为。当它为false时,意味着使用纯约束(NSLayoutConstraint
)。
此外,请注意:
默认情况下,对于您的任何视图,该属性都设置为true 以编程方式创建。如果在Interface Builder中添加视图,则 系统自动将此属性设置为false。