UIView 在用户交互后消失
Posted
技术标签:
【中文标题】UIView 在用户交互后消失【英文标题】:UIView disappears after user interaction 【发布时间】:2016-11-12 17:24:53 【问题描述】:每当我单击视图内的文本字段,然后单击另一个文本字段时,视图就会消失。奇怪...有人可以帮忙吗?
我使用 facebook pop 为视图设置动画。这是我的动画引擎代码: 导入 UIKit 进口流行音乐
class AnimationEngine
class var offScreenRightPosition: CGPoint
return CGPoint(x: UIScreen.main.bounds.width + 250,y: UIScreen.main.bounds.midY - 75)
class var offScreenLeftPosition: CGPoint
return CGPoint(x: -UIScreen.main.bounds.width,y: UIScreen.main.bounds.midY - 75)
class var offScreenTopPosition: CGPoint
return CGPoint(x: UIScreen.main.bounds.midX,y: -UIScreen.main.bounds.midY)
class var screenCenterPosition: CGPoint
return CGPoint(x: UIScreen.main.bounds.midX, y: UIScreen.main.bounds.midY - 75)
let ANIM_DELAY : Int = 1
var originalConstants = [CGFloat]()
var constraints: [NSLayoutConstraint]!
init(constraints: [NSLayoutConstraint])
for con in constraints
originalConstants.append(con.constant)
con.constant = AnimationEngine.offScreenRightPosition.x
self.constraints = constraints
func animateOnScreen(_ delay: Int)
let time = DispatchTime.now() + Double(Int64(Double(delay) * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: time)
var index = 0
repeat
let moveAnim = POPSpringAnimation(propertyNamed: kPOPLayoutConstraintConstant)
moveAnim?.toValue = self.originalConstants[index]
moveAnim?.springBounciness = 8
moveAnim?.springSpeed = 8
if (index < 0)
moveAnim?.dynamicsFriction += 10 + CGFloat(index)
let con = self.constraints[index]
con.pop_add(moveAnim, forKey: "moveOnScreen")
index += 1
while (index < self.constraints.count)
class func animateToPosisition(_ view: UIView, position: CGPoint, completion: ((POPAnimation?, Bool) -> Void)!)
let moveAnim = POPSpringAnimation(propertyNamed: kPOPLayerPosition)
moveAnim?.toValue = NSValue(cgPoint: position)
moveAnim?.springBounciness = 8
moveAnim?.springSpeed = 8
moveAnim?.completionBlock = completion
view.pop_add(moveAnim, forKey: "moveToPosition")
然后这是我的视图控制器代码,其中视图位于:
import UIKit
import pop
class LoginVC: UIViewController, UITextFieldDelegate
override var prefersStatusBarHidden: Bool
return true
@IBOutlet weak var emailLoginVCViewConstraint: NSLayoutConstraint!
@IBOutlet weak var emailLoginVCView: MaterialView!
@IBOutlet weak var emailAddressTextField: TextFieldExtension!
@IBOutlet weak var passwordTextField: TextFieldExtension!
var animEngine : AnimationEngine!
override func viewDidAppear(_ animated: Bool)
self.emailLoginVCView.isUserInteractionEnabled = true
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.bringSubview(toFront: emailAddressTextField)
self.animEngine = AnimationEngine(constraints: [emailLoginVCViewConstraint])
self.emailAddressTextField.delegate = self
self.passwordTextField.delegate = self
emailAddressTextField.allowsEditingTextAttributes = false
func textFieldShouldReturn(_ textField: UITextField) -> Bool
if (textField === emailAddressTextField)
passwordTextField.becomeFirstResponder()
else if (textField === passwordTextField)
passwordTextField.resignFirstResponder()
else
// etc
return true
@IBAction func emailTapped(_ sender: AnyObject)
AnimationEngine.animateToPosisition(emailLoginVCView, position: AnimationEngine.screenCenterPosition, completion: (POPAnimation, Bool)
in
)
@IBAction func exitTapped(_ sender: AnyObject)
AnimationEngine.animateToPosisition(emailLoginVCView, position: AnimationEngine.offScreenRightPosition, completion: (POPAnimation, Bool)
in
)
最后是我的层次结构和选项:(我的视图名称是 emailLoginVCView)。此外,当我单击另一个文本字段进行调试时,我设置了一个断点,因此我得到了以下信息:enter image description here
【问题讨论】:
有导航栏 我猜exitTapped
被某种方式调用了。闯入它以(取消)确认。
没有导航栏,还有@shallowThought 什么意思?
exitTapped
将视图移出屏幕。我假设在您实际上不想调用它的情况下无意中调用它。在exitTapped
中设置断点,以便在调用时仔细检查。
它没有改变任何东西,谢谢
【参考方案1】:
我有一个约束,将登录视图的中心与主屏幕的中心绑定
当我创建 AnimationEngine 时,我将那个约束传递给它,并将其 constant
设置为 offScreenRightPosition.x
当我打开电子邮件登录表时,我并没有更改约束的常量;我只是在改变视图的位置
这意味着自动布局认为它应该仍然在屏幕外
当第二个文本字段变为活动状态时,这会以某种方式触发自动布局以重新评估约束,并且它会看到登录视图的位置与约束所说的不匹配......
自动布局将其移出屏幕
所以如果我在emailTapped(_:)
中添加这个,问题就会消失 :)
@IBAction func emailTapped(_ sender: AnyObject)
AnimationEngine.animateToPosisition(emailLoginVCView, position: AnimationEngine.screenCenterPosition, completion: (POPAnimation, Bool)
in
self.emailLoginVCViewConstraint.constant = 0
)
【讨论】:
以上是关于UIView 在用户交互后消失的主要内容,如果未能解决你的问题,请参考以下文章