【发布时间】:2016-12-12 16:15:36
【问题描述】:
我创建了一个CAShapeLayer 并将其添加为子图层。
然后我覆盖了:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
它被成功调用,但是在我使用CABasicAnimation移动这一层之后。
它不再是可触摸的了。
奇怪的是,动画结束后CAShapeLayer原来空的地方是可以触摸的。
视图的结构如下:
我将 UIView 子类化并添加到它的层,我们将其命名为视图 X。
然后我将视图 X 添加到另一个视图中。
然后我使用 CABasicAnimation 为视图 X 设置动画
代码:
class AIFloatingMenuItem: UIView
{
var image = UIImage()
var color = UIColor.brown
var location:CGPoint?
var fontName:String?
var text:String?
let rectShape = CAShapeLayer()
convenience init(title:String, img:UIImage, bgColor:UIColor)
{
self.init(frame: CGRect(x: 0, y: 0, width: ITEM_SIZE, height: ITEM_SIZE))
self.image = img
self.color = bgColor
self.drawCircle()
backgroundColor = UIColor.red
}
override init(frame: CGRect)
{
super.init(frame: frame)
}
//MARK: - Drawing -
func drawCircle()
{
rectShape.anchorPoint = CGPoint(x:0.5 , y:0.5)
rectShape.fillColor = color.cgColor
let path = UIBezierPath(arcCenter: CGPoint(x: ITEM_SIZE/2, y: ITEM_SIZE/2), radius: ITEM_SIZE/2, startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
rectShape.path = path.cgPath
layer.addSublayer(rectShape)
}
}
class AIFloatingMenu: UIView, CAAnimationDelegate
{
var item:AIFloatingMenuItem?
override func awakeFromNib()
{
super.awakeFromNib()
animateItem1()
}
func animateItem1()
{
item = AIFloatingMenuItem(title: "title", img: UIImage(named : "icon-plus")!, bgColor: .green)
let itemsInitialPostition = CGPoint(x: 250, y: 300)
item?.layer.position = itemsInitialPostition
self.addSubview(item!)
drawAnimateY(end: CGPoint(x: item!.layer.position.x, y: 300), isEnd: false, item: item!, delay: 0.5)
}
func drawAnimateY(end:CGPoint, isEnd:Bool, item:AIFloatingMenuItem, delay:Double)
{
let pathAnimation = CASpringAnimation(keyPath: "position")
pathAnimation.damping = 13
CATransaction.begin()
CATransaction.setCompletionBlock
{
}
pathAnimation.delegate = self
pathAnimation.duration = 1.5
pathAnimation.fromValue = item.layer.position
pathAnimation.toValue = end
pathAnimation.fillMode = kCAFillModeForwards
pathAnimation.isRemovedOnCompletion = false
pathAnimation.beginTime = CACurrentMediaTime() + delay
item.layer.add(pathAnimation, forKey: nil)
CATransaction.commit()
}
}
我在情节提要中添加一个视图并将其类设置为 AIFloatingMenu。
【问题讨论】:
-
动画层的当前状态(例如位置,大小)不是由层反映,而是由其表示层反映。当您将
isRemovedOnCompletion设置为false时,这也会在动画结束后应用。为避免这种情况,请在开始动画之前将层的位置设置为其结束位置,并将isRemovedOnCompletion留给true。请参阅stackoverflow.com/questions/8259613/… 以获取命中测试动画层。 -
感谢您的解释,我通过在动画末尾设置新位置克服了这个问题。
标签: ios cashapelayer cabasicanimation