I have a simple game that I'm starting to make that has a ball bouncing around and I want the user to be able to drag a paddle around and hit the ball. The ball is bouncing around perfectly well, but the paddle behavior isn't working properly.
In the function start
the paddle is created and then an attachment behavior is setup and a PanGestureRecogniser
is declared:
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];
The panGestureRecognizer
looks like this:
- (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];
}
}
I can move the paddle around but it doesn't follow my finger, and the collisions with the ball don't look like they did before I added the ability to move the paddle. I'd be grateful for some help with this.
Don't add and remove the behavior with each gesture, just add it once, then move the anchorPoint
. The animation will need some time to run after your gesture recognizer hits UIGestureRecognizerStateEnded
.
If you really want to remove the behavior, implement UIDynamicAnimatorDelegate
and wait for the dynamicAnimatorDidPause
message.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.