根据触摸移动UIView

17

我正在尝试根据用户的触摸移动一个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;
}

我的视图不停地在两个位置之间闪烁,我不知道还能做什么。

非常感谢任何帮助。

5个回答

66
您想要使用的是在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];
   }
}

2
如果您的视图是UIImageview,请确保将userInteractionEnabled设置为YES,如下所示:_imageView.userInteractionEnabled = YES; - ddiego

15

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

你正在拖动的对象是窗口吗? - EmptyStack
是的,这是在Interface Builder中创建的视图。 - Jack Greenhill
它好多了,但是每次被触摸时奇怪地向一侧缩放。 - Jack Greenhill
1
我将oldX和oldY更改为float,一切都正常了...感谢您的帮助! - Jack Greenhill

4
这是用 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)
    }
}

1

//上面的答案适用于Swift 4.0

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
}

你能解释一下你做了什么或者改了什么,或者指出错误吗? - Nihal
请解释一下你的代码行,以便其他用户能够理解其功能。 - Ignacio Ara

0

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接