2011-02-05 60 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 
       }]; 

編輯:

作爲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 2011-02-06 00:12:55

+0

感謝您的信息! – Jumhyn 2011-02-06 00:30:41

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]; 
2

您可以拖動標籤,並通過設置類屬性的UILabel您通過定義一個類拖動標籤可以輕鬆地將該標籤拖放到屏幕上

- (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]; 

    } 

當你接觸到標籤拖動,就會將所有相應的委託方法將調用設置dragLabel類類型的屬性後。