Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift NSValue.valueWithCGRect

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")

}
like image 651
Ross Gibson Avatar asked Jun 10 '14 20:06

Ross Gibson


2 Answers

This will compile:

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

This is "initializer syntax"

like image 198
Jack Avatar answered Sep 27 '22 22:09

Jack


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")
}
like image 41
Ross Gibson Avatar answered Sep 27 '22 22:09

Ross Gibson