仅在触摸屏幕的一半时才移动对象

时间:2014-08-18 17:52:09

标签: ios objective-c uitouch

在我目前的目标C项目中,我正在编写一个机制,当你在屏幕的一半上拖动手时,一个物体直接相关移动,当你拖动另一半屏幕时,另一个物体直接移动相关但第一个没有。当我测试我的项目时,第一个对象在半屏幕上完美移动,但是当触摸另一半屏幕时第二个对象不会移动

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    if (location.x <= 259 ) 
        [Person setCenter:CGPointMake(location.x, Person.center.y)];

    if (location.y >289) 
        [Person1 setCenter:CGPointMake(location.x, Person1.center.y)];
}

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

    if (location.x <= 259 ) 
        [Person setCenter:CGPointMake(location.x, Person.center.y)];

    if (location.y >289) 
        [Person1 setCenter:CGPointMake(location.x, Person1.center.y)];
}

1 个答案:

答案 0 :(得分:0)

你的物体应该移动得很好。但是,如果您尝试将屏幕分成两半(正如您在问题中所述),则触摸委托方法中的if语句相当奇怪。这是一个“人物”对象将根据您触摸屏幕的位置移动的图像(假设4英寸屏幕为纵向)

enter image description here

正如您所看到的,屏幕的一部分不会移动任何一个对象,而另一个(相当大的)部分会移动对象的两个。只有一个非常小的区域会自行移动Person1

如果您想将屏幕的一半分配给每个对象,我建议您做这样的事情来分割屏幕的上半部分和下半部分:

if(location.y <= self.view.frame.size.height / 2)
{
    // move Person
}
else
{
    // move Person1
}

你可以明显地修改它来分割屏幕的左/右半部分。


如果您在移动这两个对象时仍然遇到问题,请确保它们已连接到视图,如果它们是IBOutlet s