2010-12-28 4 views
0

によりアニメーションを制御....iはコードのこのタイプを有するタッチ

// create a UIImageView 
UIImageView *rollDiceImageMainTemp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rollDiceAnimationImage1.png"]]; 

// position and size the UIImageView 
rollDiceImageMainTemp.frame = CGRectMake(0, 0, 100, 100); 

// create an array of images that will represent your animation (in this case the array contains 2 images but you will want more) 
NSArray *savingHighScoreAnimationImages = [NSArray arrayWithObjects: 
               [UIImage imageNamed:@"rollDiceAnimationImage1.png"], 
               [UIImage imageNamed:@"rollDiceAnimationImage2.png"], 
               nil]; 

// set the new UIImageView to a property in your view controller 
self.viewController.rollDiceImage = rollDiceImageMainTemp; 

// release the UIImageView that you created with alloc and init to avoid memory leak 
[rollDiceImageMainTemp release]; 

// set the animation images and duration, and repeat count on your UIImageView 
[self.viewController.rollDiceImageMain setAnimationImages:savingHighScoreAnimationImages]; 
[self.viewController.rollDiceImageMain setAnimationDuration:2.0]; 
[self.viewController.rollDiceImageMain.animationRepeatCount:3]; 

// start the animation 
[self.viewController.rollDiceImageMain startAnimating]; 

// show the new UIImageView 
[self.viewController.view addSubview:self.rollDiceImageMain]; 

代わりのstartAnimationはtouchesMovedを使用してこのコードを制御する方法をdirectly..there?

答えて

0

[self.viewController.rollDiceImageMain startAnimating];メソッドを呼び出すことができるという点で、- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)eventなどのUIResponderメソッドを使用してタッチを検出できます。

そして、それは、あなたはいくつかの時間後に停止し、タッチに

よろしくを制限することができ始めると、

サティヤ

0

は実際、何がしたいことは- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)eventです。以下は、UIViewをx軸だけで動かせるコードのサンプルです。あなたのコードが必要なものに適応してください。

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    CGPoint original = self.center; 
    UITouch* touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:self.superview]; 


    // Taking the delta will give us a nice, smooth movement that will 
    // track with the touch. 
    float delta = location.x - original.x; 

    // We need to substract half of the width of the view 
    // because we are using the view's center to reposition it. 
    float maxPos = self.superview.bounds.size.width 
        - (self.frame.size.width * 0.5f); 
    float minPos = self.frame.size.width * 0.5f; 

    float intendedPos = delta + original.x; 

    // Make sure they can't move the view off-screen 
    if (intendedPos > maxPos) 
    { 
     intendedPos = maxPos; 
    } 

    // Make sure they can't move the view off-screen   
    if (intendedPos < minPos) 
    { 
     intendedPos = minPos; 
    } 

    self.center = CGPointMake(intendedPos, original.y); 


    // We want to cancel all other touches for the view 
    // because we don't want the touchInside event firing. 
    [self touchesCancelled:touches withEvent:event]; 

    // Pass on the touches to the super 
    [super touchesMoved:touches withEvent:event]; 
} 

私は指で動きのデルタとないを取っていることをここに注意してください。あなたが指で追跡する場合、非常に望ましくない非常に不規則な振る舞いをします。デルタを適用すると、タッチ入力で完全に追跡されるビューのすばらしく流動的な動きが得られます。

アップデート:また、2で割るのではなく、0.5fで乗算することを選んだ理由については、ARMプロセッサはハードウェアでの除算をサポートしていないため、乗算を行うことでパフォーマンスが大幅に低下します。このパフォーマンスの最適化は、プログラムの使用中に数回しか呼び出されないため、差は出ないかもしれませんが、この特定のメッセージはドラッグすると何度も何度も呼び出されます。この場合には、乗算の価値があるかもしれません。

+0

私は配列内に一連の画像を持つ画像ビューを持っています... どうすれば使用できますか? m初心者です。私のばかげた質問に申し訳ありません。 – Hisenberg

+0

@ user462533あなたは配列内のそれぞれに対して使用しているUIImageまたはUIImageViewをサブクラス化し、このメソッドを追加します。デフォルトでは、このメソッドではUIViewクラスは何も行いません。そのため、UIViewクラスはそれをオーバーライドする必要があります。 –

関連する問題