根据拖动时UITableview单元格移动启用和禁用UITableview滚动

时间:2013-11-14 09:15:49

标签: ios uitableview

我正在创建一个基于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滚动。我需要该表不应该移动,直到被拖动的单元格没有到达顶部或底部然后它应该滚动显示隐藏的单元格。 /强>

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我有解决方案。实际上我在我的桌面视图单元格上使用了一些手势。所以我和其他手势一起启用它:

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

}

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