2010-12-20 72 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) 
    } 
}