11

I'm trying to animate the bounds of a UIButton using Facebook pop POPSpringAnimation, but I can't find what I should use in swift in replace of NSValue.valueWithCGRect

This is what I'm trying to do:

@IBAction func buttonTapped(sender : UIButton) {

    var springAnimation = POPSpringAnimation()
    springAnimation.property = POPAnimatableProperty.propertyWithName(kPOPLayerBounds) as POPAnimatableProperty
    springAnimation.springBounciness = 12.0
    springAnimation.springSpeed = 10.0

    springAnimation.toValue = NSValue.valueWithCGRect(newBounds)

    shutterButton.pop_addAnimation(springAnimation, forKey: "size")

}
Cezar
  • 55,636
  • 19
  • 86
  • 87
Ross Gibson
  • 980
  • 1
  • 8
  • 14

3 Answers3

20

This will compile:

let rect = CGRect(x: 0,y: 0,width: 1,height: 1)
let val = NSValue(CGRect: rect)

This is "initializer syntax"

Jack
  • 16,677
  • 8
  • 47
  • 51
  • Where is this documented? I don't get results for this with autocomplete either – rounak Jun 11 '14 at 04:37
  • 1
    @rounak: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html#//apple_ref/doc/uid/TP40014216-CH4-XID_27 – newacct Jun 12 '14 at 22:08
  • Yeah, I know about the conversion of method names, but I assumed this would be documented in the Swift NSValue header or docs. – rounak Jun 14 '14 at 15:17
  • 2
    @rounak it's hidden in the Swift UIGeometry header, you can get to it by option-clicking the `CGRect` portion of the call in Swift. – Alex Pretzlav Aug 20 '14 at 18:30
1

Here's my solution:

func scaleView(view: UIView, magnitude: CGFloat) {
    var springAnimation = POPSpringAnimation()
    springAnimation.property = POPAnimatableProperty.propertyWithName(kPOPViewScaleXY) as POPAnimatableProperty
    springAnimation.springBounciness = 12.0
    springAnimation.springSpeed = 10.0

    let newRect = CGRect(x: (view.bounds.origin.x + (view.bounds.size.width / 2.0)) - ((view.bounds.size.width / 2.0) * magnitude), y: (view.bounds.origin.y + (view.bounds.size.height / 2.0)) - ((view.bounds.size.height / 2.0) * magnitude), width: view.bounds.size.width * magnitude, height: view.bounds.size.height * magnitude)

    springAnimation.fromValue = NSValue(CGRect: view.bounds)
    springAnimation.toValue = NSValue(CGRect: newRect)
    view.pop_addAnimation(springAnimation, forKey: "size")
}
Ross Gibson
  • 980
  • 1
  • 8
  • 14
0

In Swift:

    let hover = CABasicAnimation(keyPath: "position")
    hover.additive = true
    hover.fromValue = NSValue(CGPoint: CGPointZero)
    hover.toValue = NSValue(CGPoint: CGPointMake(0.0, -10.0))
    hover.autoreverses = true
    hover.duration = 0.2
    hover.repeatCount = Float(Int.max)
    button.layer.addAnimation(hover, forKey: "myHoverAnimation")
royherma
  • 4,095
  • 1
  • 31
  • 42