【问题标题】:moving 2 objects at same time同时移动 2 个物体
【发布时间】:2014-09-28 19:40:25
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c cocoa-touch uitouch


    【解决方案1】:

    您应该开始处理 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)];
            }
        }
    }
    

    【讨论】:

    • 我在使用此代码时遇到了与使用原始代码时相同的问题。我试图重新设计我的机制来编码我的机制,但我不太清楚如何让物体同时移动,但是谢谢你的输入
    • @user3795419 那么当您开始进行多次触摸时会发生什么?你能描述一下吗?代码应该可以工作...谢谢
    • 我一次只能移动一个对象。当我试图通过在指定区域上滑动手指同时移动两个对象时,其中只有一个会移动
    • @user3795419 我发现与其循环遍历touches set,不如遍历[event allTouches] set。我已经编辑了我的代码。现在它应该工作了。总帐
    • @user3795419 有什么消息吗?它现在工作了吗?如果这回答了您的问题,请将帖子标记为答案。谢谢。
    【解决方案2】:

    如果您将 -touchesBegan 和 touchesMoved 代码移动到 Person 视图类而不是当前所在的视图/或 viewController 类中,那么这些视图可以彼此独立地同时处理触摸。

    *编辑:更多信息: 目前,您正在使用上面粘贴的代码处理触摸事件(我猜是 UIViewController),如果您将该代码移动到您创建的 Person 类中,您可以使代码更简单并实现您想要的结果。 这将产生这样的效果,即 Person 将决定它是否被触摸以及在哪里被触摸,并将相应地移动自己。

    在您的 Person.m 文件中添加此代码,

     -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:self.superview];
        [self moveToLocation:location];
    }
    
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:self.superview];
        [self moveToLocation:location];
    }
    
    -(void)moveToLocation:(CGPoint)location{
        CGFloat halfOfPersonWidth = self.bounds.size.width /2.0f;
        CGFloat halfOfSuperviewWidth = self.superview.bounds.size.width/2.0f;
    
        // Stop Person from going off left screen edge
        if ((location.x - halfOfPersonWidth) <= 0.0f){
            return;
        } // Stop Person from going off right screen edge
        else if ((location.x + halfOfPersonWidth) >= self.superview.bounds.size.width){
            return;
        }
    
        if (self.center.x < halfOfSuperviewWidth) {
            // Person is on the left of the screen and should not move to right side
            if ((location.x + halfOfPersonWidth) > halfOfSuperviewWidth) {
                 return;
            }
        } else{
            // Person is on the right of the screen and should not move to left side
            if ((location.x - halfOfPersonWidth) < halfOfSuperviewWidth) {
                return;
            }
        }
        // If we have made it this far then we can move the Person
        // move to touch location on x axis only
        [self setCenter:CGPointMake(location.x, self.center.y)];
        }
    

    现在在您的 View Controller 类中,您可以删除您在此处粘贴的原始 -touchesBegan 和 -touchesMoved 代码,或者如果您要谨慎,只需将其注释掉

     /* put slash asterisks above the code and asterisks slash below the code to comment out */
    

    如果您构建并运行,您应该能够像以前一样移动每个 Person 视图,但如果您同时将手指放在每个 Person 视图上,您​​应该能够同时移动它们。

    【讨论】:

    • 我是一个早期的初学者,如果我听起来很缺乏经验,很抱歉,但你介意给我看一个例子来说明代码方面的表现吗?非常感谢您的意见
    • 没问题,给我5分钟
    • 如果这对你有用,你能接受我的回答吗 - 我需要增加我的代表才能发表评论。
    • @KristianFox 我也在考虑建议将触摸处理移至 Person 类,但随后他将不得不移动 x 位置限制(一个人被控制在屏幕的一侧,另一个另一方面)到这个类,这会使事情变得更复杂一些(该类中需要其他属性和方法)。
    • @KristianFox 同样,我不确定您的代码是否可以工作,因为[touch locationInView:touch.view] 您在人的坐标系中获得了触摸的位置。 center 属性在 superview 的坐标系中,所以你应该这样做:[touch locationInView:touch.view.superview]; 或者之后使用covertPoint:toView: 方法。
    猜你喜欢
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    相关资源
    最近更新 更多