gpt4 book ai didi

ios - 试图移动 UIImageView,移动整个屏幕 View

转载 作者:行者123 更新时间:2023-11-29 02:47:31 26 4
gpt4 key购买 nike

我希望我的 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 时,我正在拖动整个屏幕,这是不正确的。需要帮助!

卡罗尔

最佳答案

您创建一个手势识别器并将其添加到 View 中,如下所示:

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

你可以调整 ImageView 的位置

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

read这篇文章。

关于ios - 试图移动 UIImageView,移动整个屏幕 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24918817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com