在我的应用程序中,我应该实现imageView的拖放;问题是我的imageView在scrollview中;这是我的代码
- (void) viewDidLoad{
[super viewDidLoad];
UILongPressGestureRecognizer *downwardGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dragGestureChanged:)];
[scrollViewAlfabeto addGestureRecognizer:downwardGesture];
for (UIGestureRecognizer *gestureRecognizer in myscrollView.gestureRecognizers)
{
[gestureRecognizer requireGestureRecognizerToFail:downwardGesture];
}
}
- (void) dragGestureChanged:(UIPanGestureRecognizer*)gesture
{
CGPoint point = [gesture locationInView:scrollViewAlfabeto];
if (gesture.state == UIGestureRecognizerStateBegan)
{
[imageViewToMove removeFromSuperview];
[self.view addSubview:imageViewToMove];
UIView *draggedView = [myscrollView hitTest:point withEvent:nil];
if ([draggedView isKindOfClass:[UIImageView class]])
{
imageViewToMove = (UIImageView*)draggedView;
}
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
imageToMove.center = point;
}
else if (gesture.state == UIGestureRecognizerStateEnded ||
gesture.state == UIGestureRecognizerStateCancelled ||
gesture.state == UIGestureRecognizerStateFailed)
{
// Determine if dragged view is in an OK drop zone
// If so, then do the drop action, if not, return it to original location
NSLog(@"point.x final:%f", point.x);
NSLog(@"point.y final:%f", point.y);
if (CGRectContainsPoint(goal.frame, point)){
imageToMove.frame = CGRectMake(167, 159, 100, 100);
}
else{
[imageToMove removeFromSuperview];
[myscrollView addSubview:imageToMove];
[imageToMove setFrame:CGRectMake(12, 38, 100, 100)];
imageToMove = nil;
}
}
}
然后使用我的代码,我可以使用longpress从scrollView中获取imageView,将其拖到“self.view”中;另外,如果我将这个imageView放在self.view中的另一个imageView上,它会放在它上面。它工作正常。但我有两个问题:
1-当我拖动此图像时,我执行
[imageViewToMove removeFromSuperview];
[self.view addSubview:imageViewToMove];
图像视图不会出现在我的手指下但在其他位置,我希望它仍然在我的手指下
2-这段代码仅在我第一次启动viewcontroller时工作,因为如果我不将imageview放在self.view内的其他imageview上,itr会返回scrollview,但如果我想再次拖动它,它不起作用。
你能帮助我吗?
答案 0 :(得分:0)
我没有在objc中取得进步,但是......
[self.view userInteractionEnabled:YES];
[self.view setMultipleTouchEnabled:YES];
取自UIView与 userInteractionEnabled 相关的文档:
讨论
设置为NO时,用户事件 - 例如触摸和 用于视图的键盘将被忽略并从事件中删除 队列。设置为YES时,事件将正常传递到视图。该 此属性的默认值为YES。在动画期间,暂时禁用用户交互 动画中涉及的所有视图,无论其中的值如何 属性。您可以通过指定禁用此行为 配置时的UIViewAnimationOptionAllowUserInteraction选项 动画。
希望它有效