屏幕底部的动画视图

时间:2020-10-18 12:49:43

标签: swift animation uiview

您好,我很快遇到动画问题。我正在为其中包含一些textviews的视图设置动画,但是当我单击视图或文本字段时,动画将倒退,视图消失。这个你能帮我吗。这是我的代码

//the function that starts the animation

    func Slide()
{
    UIView.animate(withDuration: 1, animations:{
        self.RegisterView.frame = CGRect(x: 0, y:0 + self.ImageView.frame.height, width: self.view.frame.width, height: self.view.frame.height)
        
    })
}

//the button function that starts the animation

@IBAction func LoginButtons(_ sender: UIButton) {
    switch  sender.tag{
    case 0:
        break
    case 1:
        Slide()
        break
    default:
        print("button not pressed")
        break
    }
}

这仍然无法正常工作,我重新制作了代码,现在看起来像这样

override func viewWillLayoutSubviews() {
    
    if let con = self.RegisterView_horizontalPositionAlignment
    {
        con.constant = view.frame.height - ImageView.frame.height - 30
    
    
    }
 
    
}



@IBAction func LoginButtons(_ sender: UIButton) {
    switch  sender.tag{
    case 0:
        break
    case 1:
        Slide()
        break
    default:
        print("button not pressed")
        break
    }
    
    
    
    
    
    
}
func Slide()
{
    if let con = self.RegisterView_horizontalPositionAlignment
    {
        con.constant = 0
        let anim = UIViewPropertyAnimator(duration: 1, curve: .linear){
            
                self.RegisterView.superview!.layoutIfNeeded()
                
        }
         anim.startAnimation()
            
    }

}

当我在视图上完全单击任何内容时,这仍会给我倒带动画吗?

1 个答案:

答案 0 :(得分:0)

很可能您的RegisterView最初是通过自动布局约束定位的。然后,您可以对其 frame 进行动画处理,但是您不能这样做:通过自动布局进行定位和通过frame进行定位是相反。您只能使用其中一个。现在您看到了行动上的冲突。我猜想,一旦您在文本视图中单击,就会启动自动版式并将RegisterView放回约束说明应该的位置。

因此解决方案将是,在动画中不对 frame 进行动画处理,对 constraints 中的更改进行动画处理。