2

我正在创建一个基于FMMoveTableView的应用程序,我必须在长按时拖动单元格并在同一部分和不同部分更改其位置。单元格拖动良好并设置在相同和不同部分。但问题是当我开始时向上拖动单元格表格也开始向上滚动。所以它的一些单元格是不可见的,因为我们想要保留拖动的单元格的反弹。当我将单元格拖到底部时也会发生同样的事情。

是否与 UITableView 属性相关,或者我必须以编程方式进行?

我为这个功能遵循的应用程序 FMMoveTableView,它在使用 UITableView 类类型的地方工作正常。我在 UIViewController 类中实现了它,在那里我制作了一些其他视图。

UITableView 属性:

 self.GroupedTableView=[[UITableView alloc]initWithFrame:CGRectMake(20, 25, 280, 480) style:UITableViewStylePlain];
    self.GroupedTableView.showsHorizontalScrollIndicator=YES;
    self.GroupedTableView.showsVerticalScrollIndicator=YES;
    self.GroupedTableView.bounces=YES;
    self.GroupedTableView.alwaysBounceHorizontal=NO;
    self.GroupedTableView.alwaysBounceVertical=YES;
    self.GroupedTableView.bouncesZoom=YES;
    self.GroupedTableView.delaysContentTouches=YES;
    self.GroupedTableView.canCancelContentTouches=YES;
    self.GroupedTableView.userInteractionEnabled=YES;
    self.GroupedTableView.dataSource=self;
    self.GroupedTableView.delegate=self;
    self.GroupedTableView.rowHeight=30;
    self.GroupedTableView.backgroundColor=[UIColor clearColor];
    self.GroupedTableView.tag=202;
    [self.view addSubview:self.GroupedTableView];

长按手势:

UILongPressGestureRecognizer *movingGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    [movingGestureRecognizer setDelegate:self];
    [self.GroupedTableView addGestureRecognizer:movingGestureRecognizer];

自动滚动方法:

- (void)legalizeAutoscrollDistance
{
    float minimumLegalDistance = [self.GroupedTableView contentOffset].y * -1;

    float maximumLegalDistance = [self.GroupedTableView contentSize].height - ([self.GroupedTableView frame].size.height + [self.GroupedTableView contentOffset].y);

    [self setAutoscrollDistance:MAX([self autoscrollDistance], minimumLegalDistance)];
    [self setAutoscrollDistance:MIN([self autoscrollDistance], maximumLegalDistance)];
}

- (void)stopAutoscrolling
{
    [self setAutoscrollDistance:0];
    [[self autoscrollTimer] invalidate];
    [self setAutoscrollTimer:nil];
}


- (void)maybeAutoscrollForSnapShotImageView:(FMSnapShotImageView *)snapShot
{
    [self setAutoscrollDistance:0];

    NSLog(@"Height====%f",[self.GroupedTableView frame].size.height);
    NSLog(@"Height====%f",[self.GroupedTableView contentSize].height);
    NSLog(@"Frame====%@",NSStringFromCGRect([snapShot frame]));
    NSLog(@"Frame====%@",NSStringFromCGRect([self.GroupedTableView bounds]));

    // Check for autoscrolling
    // 1. The content size is bigger than the frame's
    // 2. The snap shot is still inside the table view's bounds

    if ([self.GroupedTableView frame].size.height < [self.GroupedTableView contentSize].height && CGRectIntersectsRect([snapShot frame], [self.GroupedTableView bounds]))
    {
        CGPoint touchLocation = [[self movingGestureRecognizer] locationInView:self.GroupedTableView];
        touchLocation.y += [self touchOffset].y;

        float distanceToTopEdge  = touchLocation.y - CGRectGetMinY([self.GroupedTableView bounds]);
        float distanceToBottomEdge = CGRectGetMaxY([self.GroupedTableView bounds]) - touchLocation.y;

        if (distanceToTopEdge < [self autoscrollThreshold])
        {
            [self setAutoscrollDistance:[self autoscrollDistanceForProximityToEdge:distanceToTopEdge] * -1];
        }
        else if (distanceToBottomEdge < [self autoscrollThreshold])
        {
            [self setAutoscrollDistance:[self autoscrollDistanceForProximityToEdge:distanceToBottomEdge]];
        }
    }

    if ([self autoscrollDistance] == 0)
    {
        [[self autoscrollTimer] invalidate];
        [self setAutoscrollTimer:nil];
    }
    else if (![self autoscrollTimer])
    {
        NSTimer *autoscrollTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / 60.0) target:self selector:@selector(autoscrollTimerFired:) userInfo:snapShot repeats:YES];
        [self setAutoscrollTimer:autoscrollTimer];
    }
}

- (void)autoscrollTimerFired:(NSTimer *)timer
{
    [self legalizeAutoscrollDistance];

    CGPoint contentOffset = [self.GroupedTableView contentOffset];
    contentOffset.y += [self autoscrollDistance];
    [self.GroupedTableView setContentOffset:contentOffset];

    // Move the snap shot appropriately
    FMSnapShotImageView *snapShot = (FMSnapShotImageView *)[timer userInfo];
    [snapShot moveByOffset:CGPointMake(0, [self autoscrollDistance])];

    // Even if we autoscroll we need to update the moved cell's index path
    CGPoint touchLocation = [[self movingGestureRecognizer] locationInView:self.GroupedTableView];
    [self moveRowToLocation:touchLocation];
}



- (float)autoscrollDistanceForProximityToEdge:(float)proximity
{
    return ceilf(([self autoscrollThreshold] - proximity) / 5.0);
}

当我拖动一个单元格时,我无法停止 tableview 滚动。我需要该表不应该移动,直到拖动的单元格没有到达顶部或底部,然后它应该滚动以显示隐藏的单元格。

4

2 回答 2

0

我得到了解决方案。实际上我在我的表格视图单元格上使用了一些手势。所以要启用它以及我使用的其他手势:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
     return YES;

}

所以这实际上也是在不需要时激活 UITableView 手势。所以当我拖动单元格图像时,我的表格也开始与图像一起滚动。我误解了它,因为我的行滑动实现有一些问题。所以使用的代码如果将来有人需要,有问题的工作很好。我所做的是在上述方法中添加了一些条件并在需要时激活它。

于 2013-11-15T13:49:28.247 回答
0
// //  ViewController.h //  testingApp
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    UILongPressGestureRecognizer *reco; }

@property (nonatomic, weak) IBOutlet UITableView *table;

@end



//
//  ViewController.m
//  testingApp

#import "ViewController.h"

@implementation ViewController

@synthesize table;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    reco = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(recognize:)];
    [self.table addGestureRecognizer:reco];
}


-(void)recognize:(id)sender
{
    NSLog(@"recognize");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    return cell;
}

@end
于 2013-11-14T10:28:32.430 回答