2010-12-20 60 views
14

我正在尝试移动与用户触摸相关的UIView。移动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; 
} 

的观点不断闪烁的从一个地方到另一个,我不知道自己还能做些什么。

任何帮助表示赞赏。

回答

12

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

窗口是你拖动的对象吗? – EmptyStack 2010-12-20 11:50:27

+0

是的,它是在Interface Builder中创建的视图。 – 2010-12-20 11:54:51

+0

它好多了,但每当它被触碰时都会奇怪地缩到一边。 – 2010-12-20 12:47:18

62

你想要的是使用iOS 3.2中引入的UIPanGestureRecognizer

-(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]; 
    } 
} 
+1

更平滑的滚动! – user523234 2011-11-29 02:00:24

+3

这个答案比接受的答案要好! – Rick 2013-03-25 06:39:33

+1

如果您查看的是UIImageView,请确保将userInteractionEnabled设置为YES,如下所示:_imageView.userInteractionEnabled = YES; – ddiego 2014-03-18 18:55:44

0

这里是斯威夫特代码,与已经在屏幕上创建的ImageView的工作稍微通才版:你的东西,因为这容易(从UIViewController子类)中使用它。

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