我通过将 UIPanGestureRecognizer 分配给数组中的每个 UIView 来“解决”这个问题。
这可能不是最好的方法,但我现在可以在屏幕上移动它们。
这是代码:
for (int x = 0; x < [keys count]; x++) {
UIPanGestureRecognizer *pGr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragging:)];
UIView *sca = [self.studentCellArray objectAtIndex:x];
[sca addGestureRecognizer:pGr];
[pGr release];
}
这是我使用的“拖动”方法。我将屏幕分成三部分,如果 UIViews 碰巧越过阈值,则会有一个动画将其捕捉到一个点。我希望这能给某人一些好主意。如果您能找到更好的方法,请提供帮助。
- (void) dragging:(UIPanGestureRecognizer *)p{
UIView *v = p.view;
if (p.state == UIGestureRecognizerStateBegan || p.state == UIGestureRecognizerStateChanged) {
CGPoint delta = [p translationInView:studentListScrollView];
CGPoint c = v.center;
c.x += delta.x;
//c.y += delta.x;
v.center = c;
[p setTranslation:CGPointZero inView:studentListScrollView];
}
if (p.state == UIGestureRecognizerStateEnded) {
CGPoint pcenter = v.center;
//CGRect frame = v.frame;
CGRect scrollFrame = studentListScrollView.frame;
CGFloat third = scrollFrame.size.width/3.0;
if (pcenter.x < third) {
pcenter = CGPointMake(third/2.0, pcenter.y);
//pop the view
[self showModalDialog:YES perfMode:YES andControlTag:[studentCellArray indexOfObjectIdenticalTo:p.view]];
}
else if (pcenter.x >= third && pcenter.x < 2.0*third) {
pcenter = CGPointMake(3.0*third/2.0, pcenter.y);
}
else
{
pcenter = CGPointMake(5.0 * third/2.0, pcenter.y);
//pop the view
[self showModalDialog:YES perfMode:YES andControlTag:[studentCellArray indexOfObjectIdenticalTo:p.view]];
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
v.center = pcenter;
[UIView commitAnimations];
}
}
编辑:将 [studentCellArray indexOfObjectIdenticalTo:p.view] 添加到 andControlTag 为我提供了所触摸视图数组中的位置,因此我可以将其传递到我的模态对话框以呈现适当的信息。