Ofer Magen Ofer Magen - 5 months ago 30
Swift Question

Animation origin iOS

I've been trying to create an animation that increases the height of a view without changing the origin (the top left and right edges stay in the same position)

import UIKit
class SampRect: UIView {
var res : CGRect?
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor(red: 0.91796875, green: 0.91796875, blue: 0.91796875, alpha: 1)
self.layer.cornerRadius = 5

let recognizer = UITapGestureRecognizer(target: self, action:#selector(SampRect.handleTap(_:)))
self.addGestureRecognizer(recognizer)


}

func handleTap(recognizer : UITapGestureRecognizer) {

let increaseSize = CABasicAnimation(keyPath: "bounds")
increaseSize.delegate = self
increaseSize.duration = 0.4
increaseSize.fromValue = NSValue(CGRect: frame)
res = CGRect(x: self.frame.minX, y: self.frame.minY, width: frame.width, height: self.frame.height + 10)
increaseSize.toValue = NSValue(CGRect: res!)

increaseSize.fillMode = kCAFillModeForwards
increaseSize.removedOnCompletion = false

layer.addAnimation(increaseSize, forKey: "bounds")


}

override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
self.frame = res!
}


required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}


but the animation increase the size from the middle (push the top edge up and the bottom edge down)
what am I missing?

Answer

You can Try this.

        let newheight:CGFloat = 100.0
        UIView.animateWithDuration(0.5) {

            self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, newheight)

        }