Ios 使用UIDynamicMator的平移手势拖动UIView的正确方法

Ios 使用UIDynamicMator的平移手势拖动UIView的正确方法,ios,objective-c,uiview,Ios,Objective C,Uiview,我有一个简单的游戏,我开始做,有一个球反弹周围,我希望用户能够拖动一个桨周围,击中球。球弹得很好,但桨的动作不正常 在函数start中,创建挡板,然后设置附件行为,并声明PangestureRecognitor: self.attach = [[UIAttachmentBehavior alloc] initWithItem:self.paddle attachedToAnchor:self.paddle.center]; self.attach.damping = 1.0; SEL drag

我有一个简单的游戏,我开始做,有一个球反弹周围,我希望用户能够拖动一个桨周围,击中球。球弹得很好,但桨的动作不正常

在函数start中,创建挡板,然后设置附件行为,并声明PangestureRecognitor:

self.attach = [[UIAttachmentBehavior alloc] initWithItem:self.paddle attachedToAnchor:self.paddle.center];
self.attach.damping = 1.0;

SEL drag_sel = NSSelectorFromString(@"drag:");

self.paddle.userInteractionEnabled = YES;
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:drag_sel];

[self.paddle addGestureRecognizer:panGestureRecognizer];
PangestureRecognitor如下所示:

- (void)drag :(UIPanGestureRecognizer*)pan {

    CGPoint p = [pan translationInView:self.hockeyView];
    NSLog(@"%f, %f", p.x, p.y);

    UIView *targetView = pan.view;
    self.attach.anchorPoint = p;

    if (pan.state == UIGestureRecognizerStateBegan){
        [self.animator addBehavior:self.attach];
    }
    else if (pan.state == UIGestureRecognizerStateEnded){
        [self.animator removeBehavior:self.attach];
    } 
}

我可以移动球拍,但它没有跟随我的手指,与球的碰撞看起来不像我添加移动球拍的能力之前那样。我非常感谢您的帮助。

不要在每个手势中添加或删除行为,只需添加一次,然后移动主播点。在手势识别器点击UIgestureRecognitzerStateEnded后,动画将需要一些时间运行

如果确实要删除该行为,请实现UIDynamicMatorDelegate并等待DynamicMatorDelegate消息