CAShapeLayer不可触摸?

时间:2016-12-12 16:15:36

标签: ios cashapelayer cabasicanimation

我创建了一个CAShapeLayer并将其添加为subLayer。 然后我覆盖了:

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。

1 个答案:

答案 0 :(得分:0)

我已经能够通过在项目完成动画后将动画项目的位置更改为目标位置来解决我的问题:

func drawAnimateY(end:CGPoint, isEnd:Bool, item:AIFloatingMenuItem, delay:Double)
{
let pathAnimation = CASpringAnimation(keyPath: "position")
pathAnimation.damping = 13
CATransaction.begin()
CATransaction.setCompletionBlock
{
    item.layer.position = end// changing the position of item to the destination location
}
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()
}
相关问题