试图移动UIImageView,移动整个屏幕视图

时间:2014-07-23 19:03:59

标签: ios objective-c uiimageview

我希望我的UIViewImages可以通过触摸移动。我正在尝试使用在我的ViewController中实现的代码:

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

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([touches count]==1) {
        UITouch *touch = [touches anyObject];
        CGPoint p0 = [touch previousLocationInView:self.view];
        CGPoint p1 = [touch locationInView:self.view];
        CGPoint center = self.view.center;
        center.x += p1.x - p0.x;
        center.y += p1.y - p0.y;
        self.view.center = center;
    }
}

当我尝试拖动UIImageView时,我拖动整个屏幕,这是不正确的。 需要帮助!

Karol

2 个答案:

答案 0 :(得分:1)

您可以创建一个手势识别器并将其添加到如下视图中:

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(gestureRecognizerMethod:)];
[self.imageView addGestureRecognizer:panGestureRecognizer];

您可以调整图像视图的位置

- (void)gestureRecognizerMethod:(UIPanGestureRecognizer *)recogniser
{
    if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged)
    {
        CGPoint touchLocation = [recognizer locationInView:self.view];
        self.imageView.center = touchLocation;
    }
}

read这篇文章。

答案 1 :(得分:0)

如果我正确地阅读你的代码,那么自我。实际上就是整个屏幕。 也许你的意思是self.yourImageView?