Trying to set UILabel to move to random points on screen
So I'm trying to set a UILabel, or a piece of mutable text really, so that it will move to different points on the screen at set intervals. I'm going to use a timer for the intervals but I don't rea开发者_JAVA技巧lly have any idea how to move the label around. Looking for someone to point me in the right direction. All help is very much appreciated.
Depends, do you want animation?
If you don't want to animate the movement, it is as simple as changing its center point
UILabel* label; //Previously initialized UILabel
float newX = 90.0f;
float newY = 101.0f;
label.center = CGPointMake(newX, newY);
If you wanted to animate the movement, it's cake to add an animation block:
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:
As of iOS 4, the recommended approach for animations are the block-based methods. For example:
transitionFromView:toView:duration:options:completion:
and transitionWithView:duration:options:animations:completion:
These methods are only available in iOS 4+, so if you have a need to target anything earlier, you will have to use the other methods outlined in the UIView Class Reference.
Just from personal experience, using blocks
based animations greatly simplifies your code, make it less spaghetti-like with all the delegate methods that would otherwise have to be implemented for callbacks, etc. Blocks are really, really powerful and are very much worth your time to use.
You can drag label by a defining a class for dragging label and by setting that class property to UILabel you can easily drag that label anywhere on screen.
- (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];
}
After setting property of dragLabel class type when you touches the label drag it then all respective delegate methods will call.
Just do:
[myLabel setFrame:CGRectMake(/*x location*/, /*y location*/, /*width*/, /*height*/)];
and animation can be done like so:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
// whatever you want animated
[UIView commitAnimations];
精彩评论