2011-10-26 3 views
0

UITableViewCellをアニメーション化して、左から右に数回バウンスする場合、どうすればいいですか?私はそれを試みている:バウンスタイプのアニメーション。同じプロパティに適用するとき

var bounds = activeCell.Bounds; 
var originalLocation = bounds.Location; 
var loc = originalLocation; 

UIView.Animate(0.2,()=>{ 
    loc.X = originalLocation.X + 20; 
    activeCell.Bounds = new RectangleF (loc, bounds.Size); 
    loc.X = originalLocation.X - 20; 
    activeCell.Bounds = new RectangleF (loc, bounds.Size); 
}); 

これは、最後の状態(つまり、要素を左に移動)のみをアニメーション化します。私はそれらを分離したAnimateブロックに入れようとしました - それは助けになりませんでした。別のを試してUIAnimationOptions - 同じです。

+0

問題は、最初の変更を上書きすることです(最初のloc.XとactiveCell.Boundsは何もしません)。使用する場所を指定するには、何らかのタイマーやフラグなどが必要です。 – NickLH

+0

どういう意味ですか?コードスニペットで私を見せてもらえますか? – Agzam

答えて

0

ドキュメントが不足していると、実際には単純な作業であっても面倒なことがあります。

はここ

確かにコードがエレガントではない解決策であるが、それは動作します。それはいつか誰かを助けることを願っていますので、彼または彼女はあなたのアプローチの問題はUIView.Animateは、その変更を記録することになる

var activeCell = ((Element)sender).GetActiveCell(); 

var animation = 
(CAKeyFrameAnimation)CAKeyFrameAnimation.FromKeyPath ("transform.translation.x"); 
animation.Duration = 0.3; 

animation.TimingFunction = // small details matter :) 
      CAMediaTimingFunction.FromName(CAMediaTimingFunction.EaseOut.ToString()); 

animation.Values = new NSObject[]{ 
            NSObject.FromObject (20), 
            NSObject.FromObject (-20), 
            NSObject.FromObject (10), 
            NSObject.FromObject (-10), 
            NSObject.FromObject (15), 
            NSObject.FromObject (-15), 
           }; 

activeCell.Layer.AddAnimation (animation,"bounce"); 
0

私の頭の上から、最も簡単な方法は、アニメーションコードをメソッドに入れ、必要に応じて再帰的に呼び出すことです。コードはテストされていませんが、うまくいくはずです。

// Repeat 10 times, move 20 right and the left and right etc. 
FancyAnim(activeCell, activeCell.Bounds.Location, 10, 20); 

private void FancyAnim(UITableViewCell activeCell, PointF originalLocation, int repeat, float offset) 
{ 
var bounds = activeCell.Bounds; 
var loc = originalLocation; 

UIView.Animate(0.2, 
delegate 
{ 
    // Called when animation starts. 
    loc.X = originalLocation.X + offset; 
    activeCell.Bounds = new RectangleF (loc, bounds.Size); 
}, 
delegate 
{ 
    // Called when animation ends. 
repeat--; 
// Call the animation method again but invert the movement. 
// If you don't do this too often, you should not run out of memory because of a stack overflow. 
if(repeat >= 0) 
{ 
    FancyAnim(activeCell, originalLocation, repeat, -offset); 
} 
}); 

ただし、パスアニメーションを使用することもできます。パスを「20単位右、中央に戻し、20単位を左に戻し、中央に戻す」と定義し、好きなだけアニメーションを繰り返します。 これには、CAKeyFrameAnimationを処理する必要がありますが、少しだけコードが追加されます。 このサイトでジャンプを開始することができます:http://www.bdunagan.com/2009/04/26/core-animation-on-the-iphone/

+0

ありがとうございますが、しばらくしてからCAKeyFrameAnimationが動作するはずです。ちょうどこの状況のた​​めにそれを適用するC#の例を見つけることができません – Agzam

2

のような愚かなシンプルなものに半日費やす必要はありませんあなたはあなたの視点に立つが、彼らの最終的な状態だけを作る。

アニメーションブロックでBoundsプロパティを100回変更すると、最後のものだけがアニメーションフレームワークの観点から重要なものになります。

CoreAnimationには、WWDC 2010およびWWDC2011のビデオで説明されているいくつかの特徴があります。彼らは素晴らしい素材を持っており、それほど明白ではないトリックのいくつかを説明します。

UITableView内でセルをアニメーション化することは、実際にはUITableViewの内部を突き止めているため、複雑な問題です。そのため、さまざまな奇妙な副作用が予想されます。あなたは、そのアニメーションを行い、様々なコーナーケースを処理するTweetStationからコードを持ち上げることができます。しかし、TweetStationとiOS用Twitterアプリケーションでも、完璧に管理することはできません。アニメーション化しているプロパティと同じプロパティを常に更新して変更しているUIViewの背後にあるものをアニメーション化しているからです。

+0

それは私がCAKeyFrameAnimationを使用しなければならない理由です。ありがとう、ミゲル。 btw、私はちょうど私の最初のiPhoneアプリを始めたモノタッチはとても素晴らしい、とても素晴らしいです。私もMonotouch.Dialogを使用しています。私はたくさんの質問がありますが、私の人生はずっと楽になります。私はあなたにどれくらい義務づけられているかは言えません。そして、あなたがここで質問に答えるのはとても涼しいです。それは本当に感動的です。私の誠実な敬意。 – Agzam

+1

UITableViewでの突き刺しに関するMiguelの懸念事項に関して:UITableViewCellの_content_のアニメーションを最適化するWWDC2011セッションのビデオがあります。しかし、epsisodeを覚えていない。たぶんそれは一見価値があるかもしれません。 – Krumelur

12

ここでは、それをバウンスさせる方法を説明する素敵な記事です。

http://khanlou.com/2012/01/cakeyframeanimation-make-it-bounce/

また、説明バウンス経路を計算するために使用される式があります。 私の個人的な使用のために、私は地面でのリバウンドをシミュレートするために計算の絶対値をとってきました。

- (void) displayNoCommentWithAnimation{ 
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.y"]; 
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
animation.duration = 2; 

int steps = 120; 
NSMutableArray *values = [NSMutableArray arrayWithCapacity:steps]; 
double value = 0; 
float e = 2.71; 
for (int t = 0; t < steps; t++) { 
    value = 210 - abs(105 * pow(e, -0.025*t) * cos(0.12*t)); 
    [values addObject:[NSNumber numberWithFloat:value]]; 
} 
animation.values = values; 

animation.removedOnCompletion = NO; 
animation.fillMode = kCAFillModeForwards; 
animation.delegate = self; 
[viewThatNeedToBounce.layer addAnimation:animation forKey:nil]; 
} 


- (void) animationDidStop:(CAAnimation *)animation finished:(BOOL)flag { 
CAKeyframeAnimation *keyframeAnimation = (CAKeyframeAnimation*)animation; 
[viewThatNeedToBounce.layer setValue:[NSNumber numberWithInt:210] forKeyPath:keyframeAnimation.keyPath]; 
[viewThatNeedToBounce.layer removeAllAnimations]; 
} 
+0

完璧、ありがとうございます。あなたが下から上へそれをバウンスしたいのであれば、210 + abs(105 * pow(e、-0.025 * t)* cos(0.12 * t))となります。 –

関連する問題