2017-07-25 50 views
0

在我的應用程序中,我添加了一個UIButton,而不是讓UIButton的標準觸摸事件變暗,然後在觸摸事件結束時變成標準顏色,我想添加一個動畫,當你點擊UIButton按鈕變小,然後當觸摸事件結束時,UIButton在觸摸事件結束時變回正常大小。到目前爲止,我已經輸入了這段代碼,但它似乎不起作用。請讓我知道我該如何完成這個動畫。如何禁用標準的UIButton觸摸事件

// whatsTheGame is the UIButton I have 

@IBAction func whatsTheGame(_ sender: UIButton) { 

    logo.isHighlighted = false 

    UIView.animate(withDuration: 0.3) { 

     if let centerLogo = self.logo { 

      centerLogo.transform = CGAffineTransform(scaleX: 0.7, y: 0.7) 
     } 
    } 
} 

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { 
    super.touchesCancelled(touches, with: event) 

    UIView.animate(withDuration: 0.3) { 

     if let centerLogo = self.logo { 

      centerLogo.transform = CGAffineTransform(scaleX: 1, y: 1) 
     } 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    super.touchesEnded(touches, with: event) 

    UIView.animate(withDuration: 0.3) { 

     if let centerLogo = self.logo { 

      centerLogo.transform = CGAffineTransform(scaleX: 1, y: 1) 
     } 
    } 
} 

回答

1

添加觸摸的內心和Touch Down事件

@IBAction func onTouchUpInside(_ sender: Any) { 

    let button = sender as! UIButton 

    UIView.animate(withDuration: 0.3) { 

     button.transform = CGAffineTransform(scaleX: 1, y: 1) 
    } 
} 

@IBAction func onTouchDown(_ sender: Any) { 

    let button = sender as! UIButton 

    UIView.animate(withDuration: 0.3) { 

     button.transform = CGAffineTransform(scaleX: 0.7, y: 0.7) 
    } 
} 
+0

你可能要考慮還加入了'touchDragOut(_ :)','touchDragIn(_ :)'一個更標準的行爲: - ) –