简体   繁体   English

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

[英]Trying to move UIImageView, moving whole Screen View

I want my UIViewImages to be movable with touch. 我希望我的UIViewImages可以通过触摸移动。 I'm trying to use code implemented in my ViewController: 我正在尝试使用在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;
    }
}

When I try to drag an UIImageView, I'm dragging whole screen, which is incorrect. 当我尝试拖动UIImageView时,正在拖动整个屏幕,这是不正确的。 Need help! 需要帮忙!

Karol 卡罗尔

You create a gesture recognizer and add it to a view like this: 您创建一个手势识别器,并将其添加到这样的视图中:

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

you can adjust the position of the image view 您可以调整图像视图的位置

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

read this article. 阅读这篇文章。

If I read your code right self.view actually IS the whole screen. 如果我正确阅读了您的代码,则self.view实际上是整个屏幕。 Maybe you mean self.yourImageView instead? 也许您是说self.yourImageView吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM