加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – UITableView滚动光滑一定的速度?

发布时间:2020-12-15 02:00:45 所属栏目:百科 来源:网络整理
导读:我正在建立一个自定义的老虎机,其中列有一个可用的视图。 当用户拉一个杠杆时,桌面应该滚动到具有索引的某个位置。 我用的方法是: - scrollToRowAtIndexPath:atScrollPosition:animated: 但是这种方法会使表格滚动一段持续时间。所以你不会真的认识到一个
我正在建立一个自定义的老虎机,其中列有一个可用的视图。

当用户拉一个杠杆时,桌面应该滚动到具有索引的某个位置。
我用的方法是:

- scrollToRowAtIndexPath:atScrollPosition:animated:

但是这种方法会使表格滚动一段持续时间。所以你不会真的认识到一个漫长的或短的旋转。

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

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

已经感谢

解决方法

因为UITableView继承自UIScrollView,您还可以使用setContentOffset:animated:
这样,您可以将您的桌面视图“滚动”一定数量的您所选择的像素。

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

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

因为这是通过定时器和东西来完成的,您可以设置autoScroll将持续多长时间,以及如何使用contentoffset(以及使用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

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

如果您有任何问题可以随意发表评论,我会详细说明。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读