与运动图像的碰撞行为

时间:2013-11-24 17:17:15

标签: ios objective-c uikit-dynamics

我遇到了碰撞行为的问题。我有两种类型的物体从屏幕的底部落下并与屏幕底部的图像碰撞。碰撞效果很好,但是当我移动图像时,它会很快重新调整并闪烁。谢谢你的建议。

移动图像的代码。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //CGPoint svpt = [[touches anyObject] locationInView:self.view];
    CGPoint pt = [[touches anyObject] locationInView:self.playerMove];

CGRect frame = [self.playerMove frame];
frame.origin.x += (pt.x - self.startLocation.x);
frame.origin.y += (pt.y - self.startLocation.y);
frame.origin.x = MIN(MAX(frame.origin.x, -10), 240);
frame.origin.y = MIN(MAX(frame.origin.y, 430), 430);

self.playerMove.frame = frame;
[_animator updateItemUsingCurrentState:self.playerMove];

}

碰撞代码。

_collision = [[UICollisionBehavior alloc]initWithItems:@[self.hammerImage,self.playerMove]];
[_animator addBehavior:_collision];

2 个答案:

答案 0 :(得分:1)

虽然我不知道具体问题在哪里,但我会告诉您:UIKitDynamics通过更改视图的frametransform来实现。所以,问题可能是你通过明确修改transform来搞乱frame。请尝试使用CGAffineTransformTranslate

答案 1 :(得分:1)

如果要移动已添加到动画制作者的对象,我认为您要向playerMove添加附件行为,并在touchesMoved方法中拖动锚点。首先,创建附件行为,并将其添加到动画师:

    self.ab = [[UIAttachmentBehavior alloc] initWithItem:self.playerMove attachedToAnchor:self.playerMove.center];
    [_animator addBehavior:self.ab];

然后在你的触摸开始和触摸动作中,做这样的事情:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint pt = [[touches anyObject] locationInView:self];
    self.currentLocation = pt;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        CGPoint pt = [[touches anyObject] locationInView:self];
        self.ab.anchorPoint = CGPointMake(self.ab.anchorPoint.x + (pt.x - self.currentLocation.x), self.ab.anchorPoint.y + (pt.y - self.currentLocation.y));
        self.currentLocation = pt;
}