Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 尝试将UILabel设置为移动到屏幕上的随机点_Iphone_Objective C_Sdk_Ios4 - Fatal编程技术网

Iphone 尝试将UILabel设置为移动到屏幕上的随机点

Iphone 尝试将UILabel设置为移动到屏幕上的随机点,iphone,objective-c,sdk,ios4,Iphone,Objective C,Sdk,Ios4,所以我试图设置一个UILabel,或者一段可变文本,这样它就会以设定的间隔移动到屏幕上的不同点。我打算用计时器来计时,但我真的不知道如何移动标签。正在找人给我指出正确的方向。非常感谢所有帮助。视情况而定,您想要动画吗 如果不想设置运动的动画,只需更改其中心点即可 UILabel* label; //Previously initialized UILabel float newX = 90.0f; float newY = 101.0f; label.center = CGPointMake(

所以我试图设置一个UILabel,或者一段可变文本,这样它就会以设定的间隔移动到屏幕上的不同点。我打算用计时器来计时,但我真的不知道如何移动标签。正在找人给我指出正确的方向。非常感谢所有帮助。

视情况而定,您想要动画吗

如果不想设置运动的动画,只需更改其中心点即可

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开始,推荐的动画制作方法。例如:

transitionFromView:toView:duration:选项:完成:
transitionWithView:duration:选项:动画:完成:

这些方法仅在iOS 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开始,推荐的动画制作方法。例如:

transitionFromView:toView:duration:选项:完成:
transitionWithView:duration:选项:动画:完成:

这些方法仅在iOS 4+中可用,因此如果您需要更早地确定目标,则必须使用中概述的其他方法

根据个人经验,使用基于
块的动画可以大大简化您的代码,使其与所有必须为回调实现的委托方法不那么像意大利面条一样。块非常非常强大,非常值得您花时间使用。

只需执行以下操作:

[myLabel setFrame:CGRectMake(/*x location*/, /*y location*/, /*width*/, /*height*/)];
动画可以这样做:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
// whatever you want animated
[UIView commitAnimations];
只要做:

[myLabel setFrame:CGRectMake(/*x location*/, /*y location*/, /*width*/, /*height*/)];
动画可以这样做:

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

您可以通过定义用于拖动标签的类来拖动标签,通过将该类属性设置为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];

    }   
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event
{
NSLog(@“触摸开始”);
//检索接触点
CGPoint pt=[[触摸任何对象]位置查看:self];
位置=pt;
[[self superview]将子视图带到前面:self];
CGRect f1=[自帧];
//顶线
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];
//底线
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];
//前线
line3=[[UIView alloc]initWithFrame:CGRectMake(f1.origin.x,-50011300)];
line3.backgroundColor=[UIColor colorWithRed:0 green:0 blue:1.0f alpha:.30f];
[[self superview]添加子视图:第3行];
//后防线
line4=[[UIView alloc]initWithFrame:CGRectMake(f1.origin.x+f1.size.width,-500,11300)];
line4.backgroundColor=[UIColor colorWithRed:0 green:0 blue:1.0f alpha:.30f];
[[self superview]添加子视图:第4行];
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
NSLog(@“移动”);
//相对于原始接触点移动
CGPoint pt=[[触摸任何对象]位置查看:self];
CGRect帧=[自帧];
frame.origin.x+=pt.x-startLocation.x;
frame.origin.y+=pt.y-startLocation.y;
if(frame.origin.x<0){
frame.origin.x=0;
}
如果((frame.origin.x+frame.size.width)>380){
frame.origin.x=380-frame.size.width;
}
if(帧原点y<0){
frame.origin.y=0;
}
如果((frame.origin.y+frame.size.height)>280){
frame.origin.y=280-frame.size.height;
}
//顶线
CGRect frameLine=[line1 frame];
frameLine.origin.x=-500;
frameLine.origin.y=frame.origin.y;
[line1 setFrame:frameLine];
//底线
frameLine=[line2 frame];
frameLine.origin.x=-500;
frameLine.origin.y=frame.origin.y+frame.size.height;
[line2 setFrame:frameLine];
//前线
frameLine=[line3 frame];
frameLine.origin.x=frame.origin.x;
frameLine.origin.y=-500;
[line3 setFrame:frameLine];
//后防线
frameLine=[line4 frame];
frameLine.origin.x=frame.origin.x+frame.size.width;
frameLine.origin.y=-500;
[line4 setFrame:frameLine];
[自设置帧:帧];
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
[line1从SuperView中移除];
[line2从SuperView中移除];
[第3行从SuperView中移除];
[第4行从SuperView中移除];
}   

设置dragLabel类类型的属性后,当您触摸标签时,拖动它,然后将调用所有相应的委托方法。

您可以通过定义用于拖动标签的类来拖动标签,通过将该类属性设置为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];

    }   
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event
{
NSLog(@“触摸开始”);
//检索接触点
CGPoint pt=[[触摸任何对象]位置查看:self];
位置=pt;
[[self superview]将子视图带到前面:self];
CGRect f1=[自帧];
//顶线
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];
//底线
line2=[[UIView alloc]initWithFrame:CGRectMake(-500,f1.origin.y+f1.size.height,1300,1)];
line2.backgroundColor=[UIColor颜色带红色:0绿色:0蓝色:1.0f alpha