【问题标题】:Move UIView with relation to touch相对于触摸移动 UIView
【发布时间】:2011-05-28 04:36:45
【问题描述】:

我正在尝试根据用户的触摸移动 UIView。

这是我目前拥有的:

int oldX, oldY;
BOOL dragging;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if (CGRectContainsPoint(window.frame, touchLocation)) {
        dragging = YES;
        oldX = touchLocation.x;
        oldY = touchLocation.y;
    }

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if (CGRectContainsPoint(window.frame, touchLocation) && dragging) {
        CGRect frame;
        frame.origin.x = (window.frame.origin.x + touchLocation.x - oldX);
        frame.origin.y = (window.frame.origin.y + touchLocation.y - oldY);
        window.frame = frame;

    }

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    dragging = NO;
}

视图不断从一个位置闪烁到另一个位置,我不知道还能做什么。

任何帮助表示赞赏。

【问题讨论】:

    标签: iphone objective-c ios uiview


    【解决方案1】:

    您想要的是使用 iOS 3.2 中引入的UIPanGestureRecognizer。您可以像这样简单地使用它(来自您的 UIViewController 子类):

    -(void)viewDidLoad;
    {
       [super viewDidLoad];
       UIPanGestureRecognizer* pgr = [[UIPanGestureRecognizer alloc] 
                                           initWithTarget:self
                                                   action:@selector(handlePan:)];
       [self.panningView addGestureRecognizer:pgr];
       [pgr release];
    }
    
    -(void)handlePan:(UIPanGestureRecognizer*)pgr;
    {
       if (pgr.state == UIGestureRecognizerStateChanged) {
          CGPoint center = pgr.view.center;
          CGPoint translation = [pgr translationInView:pgr.view];
          center = CGPointMake(center.x + translation.x, 
                               center.y + translation.y);
          pgr.view.center = center;
          [pgr setTranslation:CGPointZero inView:pgr.view];
       }
    }
    

    【讨论】:

    • 如果你的view是一个UIImageview,确保userInteractionEnabled 为YES 像:_imageView.userInteractionEnabled = YES;
    【解决方案2】:

    修改 touchesBegantouchesMoved 方法,如下所示。

    float oldX, oldY;
    BOOL dragging;
    

    touchesBegan:withEvent: 方法。

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint touchLocation = [touch locationInView:self.view];
    
        if (CGRectContainsPoint(window.frame, touchLocation)) {
    
            dragging = YES;
            oldX = touchLocation.x;
            oldY = touchLocation.y;
        }
    }
    

    touchesMoved:withEvent: 方法。

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint touchLocation = [touch locationInView:self.view];
    
        if (dragging) {
    
            CGRect frame = window.frame;
            frame.origin.x = window.frame.origin.x + touchLocation.x - oldX;
            frame.origin.y =  window.frame.origin.y + touchLocation.y - oldY;
            window.frame = frame;
        }
    }
    

    touchesEnded:withEvent: 方法。

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
        dragging = NO;
    }
    

    【讨论】:

    • window是你拖动的对象吗?
    • 是的,它是在 Interface Builder 中创建的视图。
    • 它好多了,但奇怪的是,一碰到它就会缩小到一边。
    • 我将 oldX 和 oldY 更改为浮动,一切正常...感谢您的帮助!
    【解决方案3】:

    这里是 Swift 中的代码,这是一个稍微通用的版本,可以与在屏幕上创建的 ImageView 一起使用。

    let panGestureRecongnizer = UIPanGestureRecognizer(target:self, action: "handlepan:")
    imageView.addGestureRecognizer(panGestureRecongnizer)
    
    func handlepan(sender: UIPanGestureRecognizer) {
        if (sender.state == UIGestureRecognizerState.Changed) {
            var center = sender.view?.center
            let translation = sender.translationInView(sender.view)
            center = CGPointMake(center!.x + translation.x, center!.y + translation.y)
            sender.view?.center = center!
            sender .setTranslation(CGPointZero, inView: sender.view)
        }
    }
    

    【讨论】:

      【解决方案4】:

      //以上 Swift 4.0 中的答案

      var 拖动:Bool = false

      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
      
          let touch = event?.allTouches?.first
          let touchPoint = touch?.location(in: self.window)
      
          i
          if viewToDrag.frame.contains(touchPoint!){
              dragging = true
          }
      
      }
      
      override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
          let touch = event?.allTouches?.first
          let touchPoint = touch?.location(in: self.window)
      
          if (dragging) {
      
              viewToDrag.center = CGPoint(x: (touchPoint?.x)!, y: (touchPoint?.y)!)
      
          }
      
      }
      
      override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
          dragging = false
      }
      

      【讨论】:

      • 你能解释一下你做了什么或改变了什么或突出错误吗?
      • 请解释您的代码行,以便其他用户了解其功能
      【解决方案5】:

      @PeyLoW 答案是我建议的答案,并对父边界添加限制

      -(void)handlePan:(UIPanGestureRecognizer*)pgr;
      {
          if (pgr.state == UIGestureRecognizerStateChanged) {
              CGPoint center = pgr.view.center;
              CGPoint translation = [pgr translationInView:pgr.view];
              center = CGPointMake(center.x + translation.x,
                                   center.y + translation.y);
      
              if ([self pointInside:center withEvent:nil])
              {
                  pgr.view.center = center;
                  [pgr setTranslation:CGPointZero inView:pgr.view];
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-08
        • 1970-01-01
        相关资源
        最近更新 更多