ios - 同时移动2个物体

标签 ios objective-c cocoa-touch uitouch

在我当前的 ios 项目中,我将屏幕的一侧专用于一个对象,将屏幕的另一侧专用于另一个对象,我已经做到了,如果您在屏幕的一侧滑动手指指定的对象会移动。但是,我想这样做,以便您可以同时以不同的运动移动两个对象,但我不知道该怎么做。以下是我当前的代码。

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

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

    }
    else  {
        [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 <= 270 ) {
        [Person setCenter:CGPointMake(location.x, Person.center.y)];
    }
    else  {
        [Person1 setCenter:CGPointMake(location.x, Person1.center.y)];
    }
}

最佳答案

您应该开始处理在 touches 集中传递的多个触摸 - 遍历所有 UITouch 对象并进行处理。

编辑: 这是您的代码:

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

        if (location.x <= 270 ) {
            [Person setCenter:CGPointMake(location.x, Person.center.y)];
        }
        else  {
            [Person1 setCenter:CGPointMake(location.x, Person1.center.y)];
        }
    }
}

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

        if (location.x <= 270 ) {
            [Person setCenter:CGPointMake(location.x, Person.center.y)];
        }
        else  {
            [Person1 setCenter:CGPointMake(location.x, Person1.center.y)];
        }
    }
}

关于ios - 同时移动2个物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26088870/

相关文章:

objective-c - 你能帮我理解指针吗?

objective-c - Swift:使用 strftime 和本地时间格式化 NSDate

ios - 使用 unwind segue 将数据发送回之前的 Controller ?

ios - UITextView 字体反转回系统默认

iPhone - 直接从数据库中提取数据到 tableview 单元格?

ios - 如何创建选中中心单元格的 UICollectionView?

ios - 如何在 MoreTabBarController 中将 UIStatusBar 颜色更改为 .lightContent

ios - 如何在单个表中使用两个不同的自定义 UITableViewCell 子类?

ios - 有没有办法从可本地化的字符串文件中获取所有 key ?

ios - UICollectionView 首先显示最旧的帖子,我希望它显示最新的帖子