简体   繁体   English

用户交互后UIView消失

[英]UIView disappears after user interaction

whenever I click a textfield inside the view, then click the other text field, the view disappears. 每当我单击视图内的文本字段,然后单击另一个文本字段时,视图就会消失。 Strange... Can anyone help? 奇怪...有人可以帮忙吗?

这是中间的视图。

I animate the view using facebook pop. 我使用facebook pop为视图设置动画。 Here is my animation engine code: import UIKit import pop 这是我的动画引擎代码:import UIKit import pop

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")
    }
}

Then here is my viewcontroller code where the view is inside in: 然后这是我的viewcontroller代码,其中的视图位于其中:

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
        })
    }

}

Last here is my hierchy and options: (my view's name is emailLoginVCView). 最后是我的选项和选项:(我的视图的名称为emailLoginVCView)。 Also when I was debugging when I clicked another textfield I set a breakpoint so I got this info: enter image description here 另外,当我在调试时,当我单击另一个文本字段时,我设置了一个断点,所以我得到了此信息: 在此处输入图像描述

在此处输入图片说明 在此处输入图片说明

  1. I have a constraint that binds the center of the login view with the center of the main screen 我有一个约束将登录视图的中心与主屏幕的中心绑定在一起

  2. when I create the AnimationEngine,I pass it that constraint, and it sets its constant to be the offScreenRightPosition.x 当我创建AnimationEngine时,将其传递给它,并将其constant设置为offScreenRightPosition.x

  3. when I bring up the email login sheet, I'm not changing the constant of the constraint; 调出电子邮件登录表时,我没有更改约束的常量; I'm just changing the position of the view 我只是在改变视图的位置

  4. which means that autolayout thinks it's supposed to still be offscreen 这意味着自动版式认为它应该仍在屏幕外

  5. when the second textfield becomes active, that's somehow triggering auto-layout to re-evaluate the constraints, and it sees that the login view's position doesn't match what the constraint says it should be so.... 当第二个文本字段变为活动状态时,将以某种方式触发自动布局以重新评估约束,并且它会看到登录视图的位置与约束所指示的位置不匹配。

  6. Autolayout moves it offscreen 自动版式将其移出屏幕

So if I add this in emailTapped(_:) , the problem goes away :) 因此,如果我在emailTapped(_:)添加它,问题就消失了:)

@IBAction func emailTapped(_ sender: AnyObject) { AnimationEngine.animateToPosisition(emailLoginVCView, position: AnimationEngine.screenCenterPosition, completion: { (POPAnimation, Bool) in self.emailLoginVCViewConstraint.constant = 0 }) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM