![](/img/trans.png)
[英]Moving a view and changing the image of uiimageview is not performing the move of the view
[英]Trying to move UIImageView, moving whole Screen View
我希望我的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时,正在拖动整个屏幕,这是不正确的。 需要帮忙!
卡罗尔
您创建一个手势识别器,并将其添加到这样的视图中:
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;
}
}
阅读这篇文章。
如果我正确阅读了您的代码,则self.view实际上是整个屏幕。 也许您是说self.yourImageView吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.