2011-02-05 13 views
1

UILabelまたは変更可能なテキストを実際に設定しようとしていますので、設定した間隔で画面上の異なる点に移動します。私はインターバルのためにタイマーを使用するつもりですが、ラベルをどのように動かすかは全く分かりません。誰かが私を正しい方向に向けるのを探しています。すべての助けは非常に感謝しています。UILabelを画面上のランダムな点に移動するように設定しようとしています

答えて

4

依存しますか、アニメーションしますか?

:あなたは運動をアニメーション化したくない場合は

は、それがアニメーションブロックを追加するにはケーキだ、あなたは運動をアニメーション化したい場合は、その中心点

UILabel* label; //Previously initialized UILabel 
float newX = 90.0f; 
float newY = 101.0f; 

label.center = CGPointMake(newX, newY); 

を変えるのと同じくらい簡単です

UILabel* label; //Previously initialized UILabel 
float newX = 90.0f; 
float newY = 101.0f; 

[UIView transitionWithView:label 
        duration:0.5f 
        options:UIViewAnimationCurveEaseInOut 
       animations:^(void) { 
        label.center = CGPointMake(newX, newY); 
       } 
       completion:^(BOOL finished) { 
        // Do nothing 
       }]; 

EDIT:iOSの4、アニメーションare the block-based methodsのための推奨されるアプローチのよう

。たとえば:

transitionFromView:toView:duration:options:completion:transitionWithView:duration:options:animations:completion:

これらのメソッドは、iOS 4+でのみ利用可能ですので、あなたが以前のものを対象とする必要がある場合、あなたはUIView Class Referenceに概説されたその他の方法を使用する必要があります。

blocksベースのアニメーションを使用すると、コードを大幅に簡略化し、そうでなければコールバックなどのために実装する必要のあるすべてのデリゲートメソッドでスパゲティのようにすることができます。使用するのに貴重な時間です。

+0

私の答えではなく、あなたがここで説明したようにアニメーションを行う利点はありますか?それらはどう違いますか?私は前にこの方法を見ていないし、私はその違いが何か不思議です。 – Jumhyn

+0

情報ありがとう! – Jumhyn

+0

これは素晴らしいです。ありがとう。今私は、ランダムに5つの特定のポイント間を移動する方法を理解する必要があります。どのような推測が難しいでしょうか。 – HenryGale

0

ちょうど行います

[myLabel setFrame:CGRectMake(/*x location*/, /*y location*/, /*width*/, /*height*/)]; 

とアニメーションがそうのように行うことができます。

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.75]; 
// whatever you want animated 
[UIView commitAnimations]; 
+0

ブロックベースのアニメーショントランジションは、iOS 4の推奨アプローチです。http://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816 -CH3-SW131 –

2

あなたはラベルをドラッグするためとUILabel、あなたにそのクラスのプロパティを設定することによって、定義するクラスでラベルをドラッグすることができますそのラベルを画面のどこにでも簡単にドラッグできます。ラベルはすべて、それぞれのデリゲートメソッドを呼び出しますし、それをドラッグ触れたときdragLabelクラス型のプロパティを設定した後

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event 
    { 
NSLog(@"touches began"); 
// Retrieve the touch point 
CGPoint pt = [[touches anyObject] locationInView:self]; 
startLocation = pt; 

[[self superview] bringSubviewToFront:self]; 
CGRect f1=[self frame]; 

//Top Line 
line1=[[UIView alloc] initWithFrame:CGRectMake(-500, f1.origin.y, 1300, 1)]; 
line1.backgroundColor=[UIColor colorWithRed:0 green:0 blue:1.0f alpha:.30f]; 
[[self superview] addSubview:line1]; 

//Bottom Line 
line2=[[UIView alloc] initWithFrame:CGRectMake(-500, f1.origin.y+f1.size.height, 1300, 1)]; 
line2.backgroundColor=[UIColor colorWithRed:0 green:0 blue:1.0f alpha:.30f]; 
[[self superview] addSubview:line2]; 


//front Line 
line3=[[UIView alloc] initWithFrame:CGRectMake(f1.origin.x, -500, 1,1300)]; 
line3.backgroundColor=[UIColor colorWithRed:0 green:0 blue:1.0f alpha:.30f]; 
[[self superview] addSubview:line3]; 

//Rear Line 
line4=[[UIView alloc] initWithFrame:CGRectMake(f1.origin.x+f1.size.width,-500, 1, 1300)]; 
line4.backgroundColor=[UIColor colorWithRed:0 green:0 blue:1.0f alpha:.30f]; 
[[self superview] addSubview:line4]; 
    } 

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event 
    { 
NSLog(@"touches moved"); 
// Move relative to the original touch point 
CGPoint pt = [[touches anyObject] locationInView:self]; 
CGRect frame = [self frame]; 
frame.origin.x += pt.x - startLocation.x; 
frame.origin.y += pt.y - startLocation.y; 

if(frame.origin.x < 0) { 
    frame.origin.x= 0; 

} 

else if((frame.origin.x+ frame.size.width) > 380) { 

    frame.origin.x = 380-frame.size.width; 
} 

if(frame.origin.y < 0) { 

    frame.origin.y= 0; 
} 

else if((frame.origin.y + frame.size.height) > 280) { 

    frame.origin.y = 280-frame.size.height; 
} 


//Top Line 
CGRect frameLine = [line1 frame]; 
frameLine.origin.x = -500; 
frameLine.origin.y =frame.origin.y; 
[line1 setFrame:frameLine]; 


//Bottom Line 
frameLine = [line2 frame]; 
frameLine.origin.x = -500; 
frameLine.origin.y = frame.origin.y + frame.size.height; 
[line2 setFrame:frameLine]; 


//front Line 
frameLine = [line3 frame]; 
frameLine.origin.x= frame.origin.x; 
frameLine.origin.y= -500; 
[line3 setFrame:frameLine]; 

//Rear Line 
frameLine = [line4 frame]; 
frameLine.origin.x=frame.origin.x+frame.size.width; 
frameLine.origin.y= -500; 
[line4 setFrame:frameLine]; 

[self setFrame:frame]; 
    } 
    -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 

[line1 removeFromSuperview]; 


[line2 removeFromSuperview]; 


[line3 removeFromSuperview]; 


[line4 removeFromSuperview]; 

    } 

関連する問題