我有一个 ScrollView ,其中添加了固定数量的缩略图作为 subview 。我想将这些图像沿着触摸移动到另一个 View 中的矩形。我能够沿着 ScrollView (即沿着同一 View )移动图像,但无法跨另一个 View 移动。 现在正在根据触摸位置更改图像的中心。当触摸点增加超出 ScrollView 的框架时,图像消失。这是我的问题
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: self];
NSLog(@"%f,%f",location.x,location.y);
touch.view.center = location;
}
任何解决这个问题的方法都会对我有很大帮助!!!
请引用图片了解更多详情
最佳答案
这就是我要做的:
向每个图像添加一个 panGestureRecognizer,并使用以下 handlePan:
方法作为其操作。您仍然需要弄清楚如何获取正确的 imageView (myImageView
),但这将使您的图像跟随您的手指。
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
// Find the correct image you're dragging
// UIImageView *myImageview = ...
CGPoint translation = [recognizer translationInView:self.view];
if (recognizer.state == UIGestureRecognizerStateEnded) {
// What to do when you start the gesture
// You may also define myImageView here
// (if so, make sure you put it in @interface,
// because this method will be called multiple times,
// but you will enter this "if" only when you start the touch)
}
// Keep your image in the screen
if (myImageView.frame.origin.x + translation.x >= 0.0f &&
myImageView.frame.origin.x + myImageView.frame.size.width + translation.x <= 320.0f &&
myImageView.frame.origin.y + translation.y >= 0.0f &&
myImageView.frame.origin.y + myImageView.frame.size.height + translation.y <= 480.0f) {
myImageView.center = CGPointMake(myImageView.center.x + translation.x,
myImageView.center.y + translation.y);
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
// What to do when you remove your finger from the screen
}
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
关于iphone - 通过触摸移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13119360/