简体   繁体   English

移动UIView与触摸有关

[英]Move UIView with relation to touch

i'm trying to move a UIView with relation to the user's touches. 我试图移动UIView与用户的触摸有关。

Here's what I have at the moment: 这就是我现在所拥有的:

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;
}

The view keeps flickering from one location to another, and I don't know what else to do. 视图从一个位置闪烁到另一个位置,我不知道还能做什么。

Any help appreciated. 任何帮助赞赏。

What you want is to use a UIPanGestureRecognizer , introduced in iOS 3.2. 你想要的是使用iOS 3.2中引入的UIPanGestureRecognizer You use it with something as easy as this (from your UIViewController subclass): 你可以使用它(就像你的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];
   }
}

Modify the touchesBegan and touchesMoved methods to be like the following. touchesBegantouchesMoved方法修改为如下所示。

float oldX, oldY;
BOOL dragging;

The touchesBegan:withEvent: method. 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;
    }
}

The touchesMoved:withEvent: method. 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;
    }
}

The touchesEnded:withEvent: method. touchesEnded:withEvent:方法。

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

    dragging = NO;
}

Here is code in Swift, a slightly more generalist version that works with an ImageView that has been created on the screen. 这是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)
    }
}

//Above answer in Swift 4.0 //在Swift 4.0中回答以上问题

var dragging: Bool = false var dragging: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
}

@PeyLoW answer is the answer I suggest and add limit to parent boundaries @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];
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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