移动UIView与触摸有关

Jac*_*ill 16 iphone objective-c uiview ios

我试图移动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;
}
Run Code Online (Sandbox Code Playgroud)

视图从一个位置闪烁到另一个位置,我不知道还能做什么.

任何帮助赞赏.

Pey*_*loW 66

你想要的是使用UIPanGestureRecognizeriOS 3.2中引入的.你可以使用它(就像你的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];
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 这个答案比接受的答案要好! (3认同)
  • 如果您查看的是UIImageview,请确保将userInteractionEnabled设置为YES,例如:_imageView.userInteractionEnabled = YES; (2认同)

Emp*_*ack 14

touchesBegantouchesMoved方法修改为如下所示.

float oldX, oldY;
BOOL dragging;
Run Code Online (Sandbox Code Playgroud)

的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

touchesEnded:withEvent:方法方法.

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

    dragging = NO;
}
Run Code Online (Sandbox Code Playgroud)