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

查看:213
本文介绍了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.

因为这是用定时器和东西完成的,你可以设置autoScroll将持续多长时间动画会有多快(以及你使用内容偏移的距离)。

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天全站免登陆