0

我遇到了碰撞行为问题。我有 2 种类型的对象从屏幕底部落下并与屏幕底部的图像发生碰撞。碰撞效果很好,但是当我移动图像时,它会重新缩放并经常闪烁。感谢您的建议。

移动图像的代码。

-(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];
4

2 回答 2

1

虽然我不知道确切的问题出在哪里,但我会告诉你:UIKitDynamics通过更改视图frametransform. 因此,问题可能是您transform通过显式修改frame. 尝试CGAffineTransformTranslate改用。

于 2013-11-24T18:38:49.930 回答
1

如果你想移动一个已经添加到动画师的对象,我想你想给 playerMove 添加一个附件行为,并在你的 touchesMoved 方法中拖动锚点。因此,首先,创建附件行为,并将其添加到动画师:

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

然后在您的 touchesBegan 和 touchesMoves 中,执行以下操作:

-(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;
}
于 2013-11-25T00:17:28.833 回答