try this code, The animation is not perfect, but you will be able to convert an image to a line this way. I have to say that Rob's solution above is far more superior and if I were give a choice, i would do it his way.
The below code is very self-explanatory, but if in case of any doubt, feel free to ask.
To make the animation happen, tap the yellow button.
class ViewController: UIViewController{
var imageView : UIImageView = {
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
image.layer.cornerRadius = 120
image.backgroundColor = UIColor.clearColor()
image.layer.borderWidth = 3.0
image.layer.borderColor = UIColor.blackColor().CGColor
return image
}()
var animatorButton : UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.tintColor = UIColor.blueColor()
button.layer.borderWidth = 3.0
button.layer.borderColor = UIColor.blackColor().CGColor
button.backgroundColor = UIColor.yellowColor()
return button
}()
var constraintHeight = [NSLayoutConstraint]()
override func viewDidLoad() {
view.addSubview(imageView)
view.addSubview(animatorButton)
view.addConstraint(view.centerXAnchor.constraintEqualToAnchor(imageView.centerXAnchor))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[imageView(240)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["imageView": imageView]))
self.constraintHeight = NSLayoutConstraint.constraintsWithVisualFormat("V:|-100-[imageView(240)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["imageView": imageView])
view.addConstraints(constraintHeight)
view.addConstraint(animatorButton.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-450-[Button(40)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["Button": animatorButton]))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[Button]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["Button": animatorButton]))
animatorButton.addTarget(self, action: #selector(ViewController.animateImageToLine), forControlEvents: .TouchUpInside)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func animateImageToLine() {
view.removeConstraints(constraintHeight)
constraintHeight = NSLayoutConstraint.constraintsWithVisualFormat("V:|-100-[imageView(3)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["imageView": imageView])
view.addConstraints(constraintHeight)
UIView.animateWithDuration(0.3, animations: {
self.imageView.layer.cornerRadius = 0
self.view.layoutIfNeeded()
}, completion: { success in
self.imageView.backgroundColor = UIColor.blackColor()
self.imageView.layer.borderWidth = 3.0
})
}
}