ios - 移动图像后如何固定图像的位置?

标签 ios objective-c uitouch

我正在使用 - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 移动图像。如何在移动图像后固定它的位置。我的意思是我会就像当我选择正确的位置时,图像应该不再移动。

最佳答案

将 PanGesture 添加到包含图像的 imageView。

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
[imageView addGestureRecognizer:panRecognizer];
//method to move imageView whereever you want in the view,you can also write conditions to restrict the imageView not to go beyond bounds.
- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
{
    CGPoint translation = [panRecognizer translationInView:self.view];
    CGPoint imageViewPosition = imageView.center;
    imageViewPosition.x += translation.x;
    imageViewPosition.y += translation.y;


    imageView.center = imageViewPosition;

    [panRecognizer setTranslation:CGPointZero inView:self.view];

//you can use if(panRecognizer.state==UIGestureRecognizerStateEnded){} condition to fix the position of the imageView.
}

关于ios - 移动图像后如何固定图像的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19948322/

相关文章:

iphone - 仅当通过 iPhone 中的 UIActivityViewController 发布成功时才想执行一些操作

ios - UICollectionView 单元格的 "Lazy Drawing"

iphone - 添加 subview 后未收到touchesEnded/Moved/Cancelled

ios - UITouch Force on touchesBegan

ios - 我应该如何在 CoreData 中建模键/值对?

ios - Xcode 9.0 和 Firebase 5.0.4 上的位码版本无效

ios - 一个函数中的 NSURLConnectionDelegate 回调

ios - 检测用户触摸移动方向?

ios - 如何传递对 UIView 实例的引用

iphone - DynamoDB - 新放置的项目未反射(reflect)在扫描中