动画起源 iOS
Posted
技术标签:
【中文标题】动画起源 iOS【英文标题】:Animation origin iOS 【发布时间】:2016-07-08 14:45:48 【问题描述】:我一直在尝试创建一个在不改变原点的情况下增加视图高度的动画(左上边缘和右上边缘保持在同一位置)
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")
但动画会从中间增大尺寸(将顶边向上推,底边向下推) 我错过了什么?
【问题讨论】:
要这样做,您需要为视图设置一个锚点。您是否尝试过在animateWithDuration
中执行此操作?然后,您可以将框架显式设置为您想要的任何内容,它会准确地保持这一点(无需为您估算内容)。
【参考方案1】:
你可以试试这个。
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)
【讨论】:
以上是关于动画起源 iOS的主要内容,如果未能解决你的问题,请参考以下文章