UITableView 以一定的速度平滑滚动? [英] UITableView scroll smooth with certain speed?

查看:30
本文介绍了UITableView 以一定的速度平滑滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a custom slot machine with a column that exists of a uitableview.

当用户拉动杠杆时,tableview 应该滚动到带有索引的特定位置.我用的方法:

When the user pulls a lever the tableview should scroll to a certain position with an index. I used the method:

- scrollToRowAtIndexPath:atScrollPosition:animated:

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

But this method will make the table scroll with a constant duration. So you will not really recognize a long or short spin.

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

I'm looking for a way to: A) Slow down the scroll animation. Or, B) Change the duration for the scroll animation to a self defined value.

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

The normal scroll animation (with the finger) does show this effect. Maybe it is a stupid idea, but is it an idea to invoke a touchesBegan and touchesDidEnd method on my tableView?

已经谢谢了

推荐答案

因为 UITableView 继承自 UIScrollView 你也可以使用 setContentOffset:animated:通过这种方式,您可以让您的 tableview 将您选择的一定数量的像素滚动"到您喜欢的任何一侧.

Because a UITableView inherits from UIScrollView you might also use setContentOffset:animated: This way you can make your tableview "scroll" a certain amount of pixels of your choosing to any side you like.

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

This can be done the same with the scrollToRowAtIndexPath:atScrollPosition:animated:

我制作了一个原型只是为了向您展示它是如何工作的.

I made a prototype just to show you how it works.

因为这是通过计时器和其他东西完成的,您可以设置自动滚动的持续时间以及动画的速度(如果您使用的是内容偏移量).

Because this is done with timers and stuff you can set how long the autoScroll will last and how fast (and how far if you're using the contentoffset) the animation will go.

这是.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

这是 .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

如果您有任何问题,请随时发表评论,我会详细说明.

If you have any question feel free to leave a comment and I will elaborate.

这篇关于UITableView 以一定的速度平滑滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆