动画起源iOS

时间:2016-07-08 14:45:49

标签: ios swift animation

我一直在尝试创建一个动画来增加视图的高度而不改变原点(左上角和右上角保持在同一位置)

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

但是动画从中间增加了尺寸(向上推上边缘,向下推下边缘) 我错过了什么?

1 个答案:

答案 0 :(得分:2)

你可以试试这个。

/targets