首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >UITableView滚动是否以一定的速度平滑?

UITableView滚动是否以一定的速度平滑?
EN

Stack Overflow用户
提问于 2010-10-20 22:46:31
回答 7查看 16.4K关注 0票数 19

我正在构建一个定制的老虎机,它有一个uitableview列。

当用户拉动操纵杆时,表格视图应该滚动到带有索引的某个位置。我使用的方法是:

代码语言:javascript
复制
- scrollToRowAtIndexPath:atScrollPosition:animated:

但此方法将使表格滚动的持续时间恒定。所以你不会真的识别出长旋转或短旋转。

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

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

已经谢谢了

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2011-10-15 07:52:59

因为动画继承自UIScrollView,所以您还可以使用setContentOffset: UITableView :这样您就可以让您的表视图根据您的选择将一定数量的像素“滚动”到您喜欢的任何一侧。

对于scrollToRowAtIndexPath:atScrollPosition:animated:,也可以这样做

我做了一个原型只是为了向你展示它是如何工作的。

因为这是通过计时器和其他东西完成的,所以您可以设置autoScroll将持续多长时间和多快(如果使用contentoffset,还可以设置动画播放的距离)。

这是.h文件:

代码语言:javascript
复制
#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

这是.m文件:

代码语言:javascript
复制
#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

如果您有任何问题,请随时留下评论,我将详细说明。

票数 14
EN

Stack Overflow用户

发布于 2011-04-20 15:49:57

可能需要朝那个方向看?

代码语言:javascript
复制
 [UIView animateWithDuration: 1.0
                  animations: ^{
                      [tableViewExercises scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:previousSelectedExerciseCell inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
                  }completion: ^(BOOL finished){
                  }
    ];

仅使用动画:否。

票数 16
EN

Stack Overflow用户

发布于 2011-10-15 08:24:03

如果可以使用iOS 5,可以使用UIScrollViewDelegate方法scrollViewWillEndDragging:withVelocity:targetContentOffset:。这使您可以看到,用户移动手指的速度有多快,减速动画将在何处以默认速度结束,并允许您覆盖动画速度,以便在不同的点处结束。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3979119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档