如何让UIButton摇动?

时间:2017-10-04 03:12:04

标签: ios swift animation uibutton shake

我正在使用关键帧和弹簧动画学习UIView动画,并且我在点击它之后尝试按下按钮。问题是我从库中拖放按钮并将后缘固定在它上面的UILabel而没有别的。在各种示例中,我看到了标题约束,但我的按钮没有标题。这是我到目前为止的代码

@IBAction func noButtonPressed(_ sender: UIButton) {
    UIView.animate(withDuration: 1, delay: 1, usingSpringWithDamping: 0.5, initialSpringVelocity: 15, options: [], animations: {
        self.noButtonTrailing.constant = 16
        self.view.layoutIfNeeded()
    })
}

我想在某处制作标题约束吗?感谢

3 个答案:

答案 0 :(得分:12)

这是用于线性运动的简单媒体定时动画& UIView阻尼动画。

enter image description here

注意:Swift 4

extension UIView {


    // Using CAMediaTimingFunction
    func shake(duration: TimeInterval = 0.5, values: [CGFloat]) {
        let animation = CAKeyframeAnimation(keyPath: "transform.translation.x")

        // Swift 4.2 and above
        animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)

        // Swift 4.1 and below
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)


        animation.duration = duration // You can set fix duration
        animation.values = values  // You can set fix values here also
        self.layer.add(animation, forKey: "shake")
    }


    // Using SpringWithDamping
    func shake(duration: TimeInterval = 0.5, xValue: CGFloat = 12, yValue: CGFloat = 0) {
        self.transform = CGAffineTransform(translationX: xValue, y: yValue)
        UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
            self.transform = CGAffineTransform.identity
        }, completion: nil)

    }


    // Using CABasicAnimation
    func shake(duration: TimeInterval = 0.05, shakeCount: Float = 6, xValue: CGFloat = 12, yValue: CGFloat = 0){
        let animation = CABasicAnimation(keyPath: "position")
        animation.duration = duration
        animation.repeatCount = shakeCount
        animation.autoreverses = true
        animation.fromValue = NSValue(cgPoint: CGPoint(x: self.center.x - xValue, y: self.center.y - yValue))
        animation.toValue = NSValue(cgPoint: CGPoint(x: self.center.x + xValue, y: self.center.y - yValue))
        self.layer.add(animation, forKey: "shake")
    }

}

按钮操作

@IBAction func noButtonPressed(button: UIButton) {

  // for spring damping animation
  //button.shake()  


  // for CAMediaTimingFunction
  button.shake(duration: 0.5, values: [-12.0, 12.0, -12.0, 12.0, -6.0, 6.0, -3.0, 3.0, 0.0])

 // for CABasicAnimation
 //button.shake(shakeCount: 10)  

}

答案 1 :(得分:0)

我认为你正在寻找这个:

let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.07
animation.repeatCount = 4
animation.autoreverses = true
animation.fromValue = NSValue(cgPoint: CGPoint(x: self.BUTTON.center.x - 10, y: BUTTON.center.y))
animation.toValue = NSValue(cgPoint: CGPoint(x: BUTTON.center.x + 10, y: BUTTON.center.y))
BUTTON.layer.add(animation, forKey: "position")

答案 2 :(得分:0)

这是@Krunal代码的输出。我使用了3种不同的情况,输出非常好:)

Tests

Sub Graph()
'
' Graph Macro
'
' Keyboard Shortcut: Ctrl+e
'
'Select values in a column from specified workbook and sheet
Dim LR As Long, cell As Range, rng As Range

With Workbooks("Area3-LG").Sheets("Graph data")
    LR = .Range("B" & .Rows.Count).End(xlUp).Row
    For Each cell In .Range("B4:B" & LR)
        If cell.Value <> "" Then
            If rng Is Nothing Then
                Set rng = cell
            Else
                Set rng = Union(rng, cell)
            End If
        End If
    Next cell
End With
rng.Copy ' copy the union range (no need to select it first)

' paste without all the selecting
With Windows("InstData_TEMS_Existing").Sheets("L")
    ' Paste (without select) un the next empty cell fromn column AA
    .Range("AA" & .Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
                            SkipBlanks:=False, Transpose:=False
End With

Application.CutCopyMode = False

'Go back to previous workbook & delete column
Workbooks("Area3-LG").Sheets("Graph data").Columns("B:B").Delete Shift:=xlToLeft

End Sub