2016-05-25 2 views
2

Immediateにいくつかのボタンをアニメーション化してビュー全体を無作為に動かすことはできますが、ボタン 'target'は動きません。ボタンをクリックするには、画面の反対側であっても、作成された元の位置をクリックする必要があります。swift(iOS)でボタンをアニメーション化するときに、ボタンターゲット/ジェスチャー認識子を移動する方法は?

ボタンのターゲットをボタンで移動する方法の簡単な手順はありますか?

func circlePath(view: UIButton, pathDuration: Double){ 
    //create an animation to follow a circular path 
    let pathAnimation: CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "position") 
    //interpolate the movement to be more smooth 
    pathAnimation.calculationMode = kCAAnimationPaced 
    pathAnimation.repeatCount = .infinity 
    //no ease in/out to have the same speed along the path 
    pathAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) 
    //the paths circular animation is proportional its size 
    pathAnimation.duration = pathDuration 
    //The circle to follow will be inside the circleContainer frame. 
    //it should be a frame around the center of your view to animate. 
    //do not make it to large, a width/height of 3-4 will be enough. 
    let curvedPath: CGMutablePathRef = CGPathCreateMutable() 
    let circleContainer: CGRect = CGRectInset(view.frame, 23, 23) 
    CGPathAddEllipseInRect(curvedPath, nil, circleContainer) 
    //add the path to the animation 
    pathAnimation.path = curvedPath 
    //add animation to the view's layer 
    view.layer.addAnimation(pathAnimation, forKey: "myCircleAnimation") 
} 
+0

使用しているアニメーションコードを共有できますか? – Wez

+1

ボタンターゲットは移動する別個のものではありません。あなたはボタンをアニメーションの位置に適切に配置してはいけません。 – Wolverine

+0

私は、ボタンがどのように移動するのかという質問にコードを追加しました。 – user3411006

答えて

1

「ターゲット」という用語は、タップする領域を意味するために使用しないでください。 「ターゲット」はボタンに関して特定の意味を持ちますが、それはそうではありません。

「ヒット矩形」としましょう。

コアアニメーションは、実際にはオブジェクトを画面全体にアニメーション表示しません。オブジェクトの外観を与える「プレゼンテーションレイヤー」を作成しますが、オブジェクト自体はアニメートされません。

このように、アニメーションの「飛行中」の間にタップに応答するボタンを持つことはできません。

代わりに、スーパービュー(または、CAAnimationを実行している場合は、アニメーション化しているレイヤーを含むレイヤー)にタップジェスチャーレコグナイザーを配置する必要があります。次に、hitTestメソッドを使用できます。アニメーションレイヤーのプレゼンテーションレイヤーで、タップがアニメーション化されているレイヤーの境界内にあるかどうかをテストします。

私はこの手法を示すgithubに関するプロジェクトを持っています。 iOS-CAAnimation-group-demo(link。)Objective-Cで書かれていますが、テクニックは言語にかかわらず同じですので、どのように進めるかのアイデアを与える必要があります。

関連する問題