简体   繁体   中英

UIAttachmentBehavior doesn't work. UIDynamicAnimator

I am having trouble using initWithItem:attachedToItem: Initializes an attachment behavior that connects the center point of a dynamic item to the center point of another dynamic item. But when I changed the center point of topview using method pan,only the topview moved around,I can't get the other view to move.Isn't it should be moving together? (BTW I am trying to implement a pile of cards and move around all together when I pan the card in the top.)

-(void)pinch:(UIPinchGestureRecognizer *)gesture
{
    if(gesture.state ==  UIGestureRecognizerStateChanged){
        CGPoint pinchCen = [gesture locationInView:self.cardArea];
        if (gesture.scale <= 0.5 && !self.pileStat) {
            self.pileStat = !self.pileStat;
            NSUInteger number = [self.cardViews count];
            UIView *topView = [self.cardViews lastObject];
            [topView addGestureRecognizer:[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)]];
            for (int i = 0; i < number;i++) {
                UIView *cardView = self.cardViews[i];
                [UIView animateWithDuration:0.5 animations:^{cardView.center = CGPointMake(pinchCen.x+i%10*0.5, pinchCen.y+i%10*0.5);} completion:^(BOOL finished){
                    if(i != number - 1){
                        UIAttachmentBehavior *attach = [[UIAttachmentBehavior alloc]initWithItem:cardView attachedToItem:topView];
                        [self.animator addBehavior:attach];
                    }
                }];
            }

        }
        else if(gesture.scale > 1.5 && self.pileStat)
        {
            self.pileStat = !self.pileStat;
        }
    }else if (gesture.state == UIGestureRecognizerStateEnded){
        gesture.scale = 1.0f;
    }
}
-(void)pan:(UIPanGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateChanged || UIGestureRecognizerStateEnded) {
    UIView *topView = [self.cardViews lastObject];
        CGPoint trans = [gesture translationInView:self.cardArea];
        topView.center = CGPointMake(trans.x+topView.center.x, trans.y+topView.center.y);
        [gesture setTranslation:CGPointMake(0, 0) inView:self.cardArea];
    }
}

setCenter does not play nice with UIDynamicAnimator. Instead of changing the center coordinate of your top view, you should use another UIAttachmentBehavior that you attach to your touch. Replace your pan gesture handler with this method:

//Add a variable in your interface or header section called "touchAttachment" of type UIAttachmentBehavior
- (void) pan: (UIPanGestureRecognizer *) sender{
  UIView* topCard = [self.cardViews lastObject];
  if (sender.state == UIGestureRecognizerStateBegan){
    _touchAttachment = [[UIAttachmentBehavior alloc] initWithItem:topCard         
      attachedToAnchor: [sender locationInView:self.view]];
    [self.animator addBehavior:_touchAttachment];
  }
  else if (sender.state == UIGestureRecognizerStateChanged){
    [_touchAttachment setAnchorPoint: [sender locationInView: self.view]];
  }
  else if (sender.state == UIGestureRecognizerStateEnded){
    [self.animator removeBehavior: _touchAttachment];
    _touchAttachment = nil;
  }
}

Make sure you add the "touchAttachment" variable. Hope it helps :)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM