【问题标题】:Pinching in and out from UICollectionView从 UICollectionView 中捏入和捏出
【发布时间】:2013-03-21 00:24:33
【问题描述】:

所以我有一个UICollectionView,我希望用户能够张开或伸入以展开和折叠集合单元格。我用this tutorial to perform the expanding and collapsing bit。哪个有效。然后我将下面的代码添加到我的collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 委托方法中。

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchCollection:)];
[cell addGestureRecognizer:pinchGesture];

然后像这样创建动作pinchCollection:

-(void)pinchCollection:(id)sender {
    UIPinchGestureRecognizer *gesture = (UIPinchGestureRecognizer*)sender;
    if (gesture.state == UIGestureRecognizerStateBegan) {        
        if (gesture.scale <= -1) { // I also changed this to be gesture.scale < 1 but it didn't work. 
            // pinch in
            [self collapseCollection];
            gesture.scale = 1;

        }
        if (gesture.scale >= 1) { // I changed this to be gesture.scale > 1 but it didn't work either. 
            // pinch out 
            [self expandCollection];
            gesture.scale = -1;
        }
    }
}

但只有捏出的代码有效。我已经搜索了有关如何正确执行此操作但没有运气的教程或代码。

扩展集合如下所示:

【问题讨论】:

  • 听起来您正在使用“巧合的编程”来编写代码。这是一个非常糟糕的做法。您需要了解您的代码是如何工作的,而不是随意更改代码并认为它​​可以“修复错误”。 "0
  • 哈哈谢谢@HaiFengKao!实用编程。我之前想通了,但我没有更新帖子。无论如何,谢谢。

标签: ios ipad uicollectionview uipinchgesturerecognizer


【解决方案1】:

这不是答案,但这里缺少一件事:

[gesture setDelegate:self];

我很确定该比例的负值是没有意义的。比例通常用作百分比,归一化为 0 到 1 之间的值。我必须看看你折叠和展开集合视图的方法;但是,如果您使用手势的 scale 属性来调整集合视图的大小,则只需修复错误的数学即可。

如果您使用属性值 -1 作为确定捏合方向的方法,那么正确地执行此操作很简单:

CGPoint p1 = [sender locationOfTouch:0 inView:self];
CGPoint p2 = [sender locationOfTouch:1 inView:self];
    
// Compute the new spread distance.
CGFloat xd = p1.x - p2.x;
CGFloat yd = p1.y - p2.y;
CGFloat distance = sqrt(xd*xd + yd*yd);

if (distance < previousDistance) {
// add collapse method call
} else {
// add expand method call
}

previousDistance = distance;

在此示例代码中,previousDistance 是一个全局或静态变量,用于与新计算的距离进行比较;显然,如果距离大于之前的距离,那么用户正在尝试扩展集合视图(反之亦然)。

如果这些似乎都不是问题,请提供代码到您的展开和折叠方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    相关资源
    最近更新 更多