2016-12-15 106 views
1

大家好,我正面臨一個奇怪的問題。我想增加一個視圖的寬度,點擊一個動畫效果很好的按鈕,在我的情況下。我使用的增加寬度的代碼如下─用動畫降低視圖的寬度

@IBAction func increaseWidth(_ sender: AnyObject) { 

     UIView.animate(withDuration: 1.0, delay: 0, options: 
     [.allowUserInteraction], animations: { 

      print("Animation function animateStuff() started!") 

      let frmPlay : CGRect = self.nameLbl.frame 
      let originXbutton = frmPlay.origin.x 
      let originYbutton = frmPlay.origin.y 
      let originWidthbutton = frmPlay.size.width 
      let originHeightbutton = frmPlay.size.height 

      self.nameLbl.frame = CGRect(origin: CGPoint(x: originXbutton,y :originYbutton), size: CGSize(width: originWidthbutton+100, height: originHeightbutton)) 


     }, completion: { finished in 

    }) 
} 

但是這是我不減少與animation.It寬度只是減少其使用降低了width.The代碼使用的代碼寬度低於 -

@IBAction func decreaseWidth(_ sender: AnyObject) { 


    UIView.animate(withDuration: 1.0, delay: 0, options: 
     [.allowUserInteraction], animations: { 

      print("Animation function animateStuff() started!") 

      let frmPlay : CGRect = self.nameLbl.frame 
      let originXbutton = frmPlay.origin.x 
      let originYbutton = frmPlay.origin.y 
      let originWidthbutton = frmPlay.size.width 
      let originHeightbutton = frmPlay.size.height 

      // self.nameLbl.frame = frmPlay 
      self.nameLbl.frame = CGRect(origin: CGPoint(x: originXbutton,y :originYbutton), size: CGSize(width: originWidthbutton-100, height: originHeightbutton)) 



     }, completion: { finished in 

    }) 

} 

請幫助我哪裏出錯了。

+0

檢查我update.let我知道你在想什麼.... – Joe

+0

有趣的問題,似乎是的UILabel,你不能做調整動畫特殊元素它(http://stackoverflow.com/a/22224630/3869284)。嘗試將其放入UIView並在該視圖上設置動畫。 – Ryan

+0

@Jeo - 更新的代碼也與前一個代碼具有相同的響應。 ): –

回答

3

您可以簡單地使用CALayer來實現此動畫。請嘗試以下方法。

func scaleX() { 

    let animation = CABasicAnimation(keyPath: "transform.scale.x") 
    animation.fromValue = NSValue(cgSize: CGSize(width: 1, height: 1)) 
    animation.toValue = NSValue(cgSize: CGSize(width: 1.2, height: 1)) 
    animation.duration = 1.0 
    self.imageView.layer.add(animation, forKey: "Image-scale") 
    imageView.transform = CGAffineTransform(scaleX: 1.2, y: 1.0) 

} 

func reset() { 

    UIView.animate(withDuration: 1) { 
      self.imageView.transform = CGAffineTransform.identity 
    } 

} 

注:我使用imageView測試animation.You可以指定你的view進行動畫。

更新時間:

,我建議您可以通過更改下面的方法它的框架內容size.Try使用基本的UIView動畫。

override func viewDidAppear(_ animated: Bool) { 
    //To restrict animation repeats. 
    resetButton.isEnabled = false 
    resetButton.showsTouchWhenHighlighted = true 
    scaleButton.showsTouchWhenHighlighted = true 
} 


func scaleX() { 

    resetButton.isEnabled = true 
    scaleButton.isEnabled = false 
    UIView.animate(withDuration: 1) { 
     self.imageView.frame = CGRect(x: self.imageView.frame.origin.x, y: self.imageView.frame.origin.y, width: self.imageView.frame.width + 100, height: self.imageView.frame.height) 
    } 
} 

func reset() { 

    resetButton.isEnabled = false 
    scaleButton.isEnabled = true 
    UIView.animate(withDuration: 1) {  
    self.imageView.frame = CGRect(x: self.imageView.frame.origin.x, y: self.imageView.frame.origin.y, width: self.imageView.frame.width - 100, height: self.imageView.frame.height)  
} 

} 

輸出:

enter image description here

+0

感謝您的迴應,但我希望增加視圖的寬度而不改變視圖的x座標,並且在不改變x座標的情況下減小視圖的寬度。請讓我知道我們如何通過上述方法實現此目的。 –