UITableView以一定的速度滚动?

Mat*_*art 18 iphone xcode uitableview ipad

我正在构建一个自定义老虎机,其中包含一个uitableview列.

当用户拉动杠杆时,工作台视图应滚动到具有索引的特定位置.我用的方法是:

- scrollToRowAtIndexPath:atScrollPosition:animated:
Run Code Online (Sandbox Code Playgroud)

但是这种方法会使表格以恒定的持续时间滚动.所以你不会真正认识到长期或短期的旋转.

我正在寻找一种方法:A)减慢滚动动画.或者,B)将滚动动画的持续时间更改为自定义值.

正常的滚动动画(用手指)确实显示了这种效果.也许这是一个愚蠢的想法,但是在我的tableView上调用touchesBegan和touchesDidEnd方法是一个想法吗?

谢谢

Rub*_*con 17

可能需要朝那个方向看?

 [UIView animateWithDuration: 1.0
                  animations: ^{
                      [tableViewExercises scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:previousSelectedExerciseCell inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
                  }completion: ^(BOOL finished){
                  }
    ];
Run Code Online (Sandbox Code Playgroud)

仅使用动画:NO.

  • 对我来说,这只适用于目标单元格在屏幕上的情况.当它不是.通过屏幕的所有细胞都不会被初始化......我有100个细胞.并从第一个滚动到第80个.当它开始时,每个都变成空白,然后80和更高的数字出现在屏幕上,它停止滚动. (11认同)
  • 这很完美.除了一个小设计问题.给定`indexPath`上单元格上方的单元格在动画开始之前消失. (8认同)

Tot*_*mus 13

因为UITableView继承自UIScrollView,所以你也可以使用setContentOffset:animated:这样你就可以让你的tableview将你选择的一定数量的像素"滚动"到你喜欢的任何一面.

这可以与scrollToRowAtIndexPath相同:atScrollPosition:animated:

我做了一个原型只是为了告诉你它是如何工作的.

因为这是通过计时器和东西来完成的,你可以设置autoScroll将持续多长时间以及动画将会有多快(以及你使用contentoffset的距离).

这是.h文件:

#import <UIKit/UIKit.h>

@interface AutomaticTableViewScrollViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
    UITableView *slotMachine;
    NSMutableArray *listOfItems;
    NSTimer *tableTimer;
}

@property (nonatomic,retain) UITableView *slotmachine;
@property (nonatomic,retain) NSMutableArray *listOfItems;
@property (nonatomic,retain) NSTimer *tableTimer;

-(void)automaticScroll;
-(void)stopscroll;

@end
Run Code Online (Sandbox Code Playgroud)

这是.m文件:

#import "AutomaticTableViewScrollViewController.h"

@implementation AutomaticTableViewScrollViewController

@synthesize slotmachine;
@synthesize listOfItems;
@synthesize tableTimer;

-(void)loadView
{
    [super loadView];

    slotmachine = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    slotmachine.delegate = self;
    slotmachine.dataSource = self;
    [self.view addSubview:slotmachine];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    static NSString *CellIdentifier = @"Cell";

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

    // Set up the cell...
    if (indexPath.row % 2 == 0) 
    {
        cell.textLabel.text = @"blalala";
    }

    return cell;
}

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //you might want to do this action in ur buttonTargetMethod
    //start timers
    tableTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 //this value arranges the speed of the autoScroll
                                                   target:self
                                                 selector:@selector(automaticScroll)
                                                 userInfo:nil
                                                  repeats:YES];

    [NSTimer scheduledTimerWithTimeInterval:5 //this arranges the duration of the scroll
                                     target:self
                                   selector:@selector(stopscroll)
                                   userInfo:nil
                                    repeats:NO]; 
}

-(void)automaticScroll
{
    [slotmachine setContentOffset:CGPointMake(slotmachine.contentOffset.x, slotmachine.contentOffset.y + 50) animated:YES]; //the 50 stands for the amount of movement every tick the timer will make
}

-(void)stopscroll
{
    //stop tabletimer again
    [tableTimer invalidate];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}

@end
Run Code Online (Sandbox Code Playgroud)

如果您有任何问题可以自由发表评论,我会详细说明.