我需要用户能够在屏幕上拖动子视图以更改子视图的位置。我知道这两种方式都可以,但首选一种方式吗?为什么?
2 回答
1
纯粹基于 MVC 的概念,负责该视图的任何东西都应该处理平移手势识别器。至少,这就是我所做的。视图本身不应该真正关心或意识到它正在被移动。
现在,对于像缩放或滚动这样的东西,这似乎与视图相关(例如,UIScrollView
具有panGestureRecognizer
属性)。但在这种情况下,它是视图功能的一部分,因此视图控制手势识别器是有意义的。
一般来说,它通常是关于意图是什么以及谁应该在语义上负责处理动作。因此,在您的情况下,我会将其添加UIPanGestureRecognizer
到超级视图并让它管理自己的子视图。
于 2011-09-21T00:01:53.233 回答
0
如果您使用的是 iOS 3.2 或更高版本,请查看 UIPanGestureRecognizer。
// **** other Code ****
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
panGesture.delegate = self;
[dragView addGestureRecognizer:panGesture];
[panGesture release];
}
- (void)panGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint location = [gestureRecognizer locationInView:parentView]; // The view the of the drag item is in
gestureRecognizer.view.center = location;
}
这一切都是凭记忆完成的,但应该是这样的
于 2011-09-20T23:52:39.560 回答