2010-12-20 36 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

+0

はい、Interface Builderで作成されたビューです。 –

+0

これははるかに優れていますが、触ったときはいつでも奇妙に横にズームします。 –

62

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

スムーズなスクロール!!! – user523234

+3

この回答は受け入れられた回答よりも優れています! – Rick

+1

あなたがUIImageviewを表示している場合は、次のようにuserInteractionEnabledをYESにしてください:_imageView.userInteractionEnabled = YES; – ddiego

0

ここでコードがスウィフト、画面上で作成された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) 
    } 
} 
関連する問題