2016-12-12 5 views
0

CAShapeLayerを作成し、それをサブレイヤとして追加しました。CAShapeLayerはタッチできません。

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) 

そして、それが正常に呼び出されますが、私はCABasicAnimationを使用して、この層を移動した後: は、それから私はオーバーライドされます。

もうタッチできません。

CAShapeLayerの元の空いている場所は、アニメーションが終了した後で触れることができます。次のように

ビューの構造は次のとおりです。

私はUIViewのをサブクラス化し、その後、私は、ビューXを取り、葯のビューに追加するのは、ビューX.

という名前を付けてみましょう、それにレイヤーを追加します。私はストーリーボードにビューを追加し、それがAIFloatingMenuにクラスの設定

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

それから私はCABasicAnimation

コードを使用してビューのXをアニメーション化。

+1

アニメーションの現在の状態(例えば位置、大きさ)層はその層によって反射されず、その提示層によって反射される。 'isRemovedOnCompletion'を' false'に設定すると、これはアニメーションの終了後にも適用されます。これを避けるには、アニメーションを開始する前にレイヤの位置を終了位置に設定し、 'isRemovedOnCompletion'を' true'のままにしておきます。アニメーションレイヤのヒットテストについては、http://stackoverflow.com/questions/8259613/hittest-on-calayer-how-do-you-find-which-actual-layer-was-hitを参照してください。 – clemens

+1

説明をいただきありがとうございます。アニメーションの最後に新しい位置を設定することで、この問題を解決しました。 –

答えて

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() 
} 
関連する問題