我正在构建一个定制的老虎机,它有一个uitableview列。
当用户拉动操纵杆时,表格视图应该滚动到带有索引的某个位置。我使用的方法是:
- scrollToRowAtIndexPath:atScrollPosition:animated:
但此方法将使表格滚动的持续时间恒定。所以你不会真的识别出长旋转或短旋转。
我正在寻找一种方法: A)放慢滚动动画的速度。或者,B)将滚动动画的持续时间更改为自定义的值。
正常的滚动动画(用手指)确实显示了这种效果。也许这是一个愚蠢的想法,但是在我的tableView上调用touchesBegan和touchesDidEnd方法是一个想法吗?
已经谢谢了
发布于 2011-10-15 07:52:59
因为动画继承自UIScrollView,所以您还可以使用setContentOffset: UITableView :这样您就可以让您的表视图根据您的选择将一定数量的像素“滚动”到您喜欢的任何一侧。
对于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
这是.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
如果您有任何问题,请随时留下评论,我将详细说明。
发布于 2011-04-20 15:49:57
可能需要朝那个方向看?
[UIView animateWithDuration: 1.0
animations: ^{
[tableViewExercises scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:previousSelectedExerciseCell inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
}completion: ^(BOOL finished){
}
];
仅使用动画:否。
发布于 2011-10-15 08:24:03
如果可以使用iOS 5,可以使用UIScrollViewDelegate
方法scrollViewWillEndDragging:withVelocity:targetContentOffset:
。这使您可以看到,用户移动手指的速度有多快,减速动画将在何处以默认速度结束,并允许您覆盖动画速度,以便在不同的点处结束。
https://stackoverflow.com/questions/3979119
复制相似问题