首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIAttachmentBehavior不工作。UIDynamicAnimator

UIAttachmentBehavior不工作。UIDynamicAnimator
EN

Stack Overflow用户
提问于 2014-11-12 07:13:42
回答 1查看 457关注 0票数 1

我在使用initWithItem:attachedToItem:初始化连接动态项的中心点到另一个动态项的中心点的附件行为时遇到了问题。但是当我用方法pan改变视图的中心点时,只有上面的视图在移动,我不能移动另一个视图。它不是应该一起移动吗?(顺便说一句,我正在尝试实现一堆卡片,当我将卡片移到顶部时,它们会一起移动。)

代码语言:javascript
复制
-(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];
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-04 01:51:03

setCenter对UIDynamicAnimator不友好。与其更改顶部视图的中心坐标,不如使用附加到触摸上的另一个UIAttachmentBehavior。用以下方法替换pan手势处理程序:

代码语言:javascript
复制
//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;
  }
}

确保添加"touchAttachment“变量。希望它有帮助:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26881242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档