如何使用Swift将自定义视图右至左右左右移动

问题描述:

我设计了一个视图。通常当我点击一个按钮时,这个视图将显示和隐藏。现在我正在尝试使用左到右和从右到左来动画显示该视图。以下是我的代码,但它不起作用。请检查一下。如何使用Swift将自定义视图右至左右左右移动

if(self.openedView) { 
     self.openedView = false; 
     UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseIn, animations: {() -> Void in 



     }, completion: {(_ finished: Bool) -> Void in 
     }) 

     shareMainView.isHidden = true; 
    } else { 
     self.openedView = true; 
     UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseOut, animations: {() -> Void in 
     }, completion: {(_ finished: Bool) -> Void in 
      /*done*/ 
     }) 

     shareMainView.isHidden = false; 
    } 

shareMainView.isHidden = false;这是我的看法,这将被打开,隐藏的动画。

+0

您希望您的视图从左向右扩展并从右向左折叠? –

+0

是的。第一次点击将显示从右到左的视图,然后第二次点击将隐藏并显示从左到右的动画。 –

+0

兄弟根据你发布的代码,你既没有改变你的'shareMainView'的框架,也没有任何约束。因此,尝试使用开放状态的约束框设置一些宽度(如150),并将动画块中的关闭设置宽度设置为0.0。 –

兄弟根据您发布的代码,您既没有改变您的shareMainView的框架,也没有任何约束。因此,尝试使用开放状态的约束框设置一些宽度(如150),并将动画块中的关闭设置宽度设置为0.0。

不要忘记在动画代码的最后使用shareMainView.layoutIfNeeded(),在完成块中使用shareMainView.isHidden = false。这将有助于正确显示动画。

鉴于没有负载:

if(self.openedView) { 
     self.openedView = false; 
     UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseIn, animations: {() -> Void in 
     shareMainView.layoutIfNeeded() 
     shareMainView.frame = CGRectMake(_aview.frame.origin.x, _aview.frame.origin.y, 200, _aview.frame.size.height); 


     }, completion: {(_ finished: Bool) -> Void in 
     }) 

     shareMainView.isHidden = true; 
    } else { 
     self.openedView = true; 
     UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseOut, animations: {() -> Void in 
     shareMainView.layoutIfNeeded() 
     shareMainView.frame = CGRectMake(_aview.frame.origin.x, _aview.frame.origin.y, 0, _aview.frame.size.height); 

     }, completion: {(_ finished: Bool) -> Void in 
      /*done*/ 
     }) 

     shareMainView.isHidden = false; 
    } 

宽度可以尽可能要expand.For比如我有用户200.In视图并加载它:按钮点击

shareMainView.frame = CGRectMake(_aview.frame.origin.x, _aview.frame.origin.y, 0, _aview.frame.size.height); 

是0,然后是200,同时展开,然后再折叠为0。 希望它会有所帮助。