【问题标题】:Movement of object only when one half of the screen is touched仅当触摸屏幕的一半时才移动对象
【发布时间】:2014-08-18 17:52:08
【问题描述】:

在我当前的目标 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)];
}

【问题讨论】:

  • 您确实意识到用于决定要移动哪个对象的坐标系不是“屏幕的一半”,不是吗?

标签: ios objective-c uitouch


【解决方案1】:

您的对象应该可以正常移动。但是,如果您试图将屏幕一分为二(正如您在问题中所说),那么您的触摸委托方法中的 if 语句相当奇怪。这是一个图像,其中“Person”对象将根据您触摸屏幕的位置移动(假设纵向为 4 英寸屏幕)

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

如果您想将屏幕的一半分配给每个对象,我建议您执行以下操作来拆分屏幕的上半部分和下半部分:

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

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


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

【讨论】:

  • 感谢您的意见。它极大地帮助我纠正了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多