簡體   English   中英

UITableView以一定的速度滾動?

[英]UITableView scroll smooth with certain speed?

我正在構建一個自定義老虎機,其中包含一個uitableview列。

當用戶拉動杠桿時,工作台視圖應滾動到具有索引的特定位置。 我用的方法是:

- scrollToRowAtIndexPath:atScrollPosition:animated:

但是這種方法會使表格以恆定的持續時間滾動。 所以你不會真正認識到長期或短期的旋轉。

我正在尋找一種方法:A)減慢滾動動畫。 或者,B)將滾動動畫的持續時間更改為自定義值。

正常的滾動動畫(用手指)確實顯示了這種效果。 也許這是一個愚蠢的想法,但是在我的tableView上調用touchesBegan和touchesDidEnd方法是一個想法嗎?

謝謝

可能需要朝那個方向看?

 [UIView animateWithDuration: 1.0
                  animations: ^{
                      [tableViewExercises scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:previousSelectedExerciseCell inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
                  }completion: ^(BOOL finished){
                  }
    ];

僅使用動畫:NO。

因為UITableView繼承自UIScrollView,所以你也可以使用setContentOffset:animated:這樣你就可以讓你的tableview將你選擇的一定數量的像素“滾動”到你喜歡的任何一面。

這可以與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

如果您有任何問題可以自由發表評論,我會詳細說明。

如果你可以要求iOS 5,你可以使用UIScrollViewDelegate方法scrollViewWillEndDragging:withVelocity:targetContentOffset: . 這使您可以查看用戶移動手指的速度,減速動畫以默認速度結束的位置,並允許您覆蓋動畫速度,以便在不同的點結束。

我搜索了很多這個答案,但最終還是得出了我自己的答案。 您可以使用起始行號調用方法scrollAutomatically,如:

[self scrollAutomatically:0];

所以這就是函數的樣子。 我正在處理一個總共有3000行的表,我打算滾動到底部。

- (void) scrollAutomatically:(int) i
{
    __block int j = i;
    [UIView animateWithDuration: 0//Change this to something more for slower scrolls
                     animations: ^{
                         [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:j inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
                     }
                     completion: ^(BOOL finished){
                         j = j + 10;//Changing this number affects speed.
                         if(j<=2999)//here you could provide the index of destination row
                             [self scrollAutomatically:j];
                         else
                         {
                             //I had some code here that popped up a UIAlertView.
                         }
                     }];
}

現在來到滾動的速度。

真正的快速滾動:

如果我設置值,我將函數的每次調用的行索引(j)增加到10,即如果我寫j = j+10; 然后我的3000行花了大約9秒來滾動。 (3000 *意味着我可以集合的FPS)。 如果我把它設置為j = j+20; 然后3000行花了大約4.5秒。 所以你明白了。 要使其滾動較慢,請減小增量值。

對於慢,可讀的滾動:

[UIView animateWithDuration: 1.5//this will directly set your speed. n rows scrolled every 1.5 seconds.

注意:如果您更改CALayers或Views的框架(例如,您可能已添加到tableViewCell的contentView中的customView),那么這些動畫將開始打擾您。 對於大型動畫持續時間,它們將非常明顯,您可能會看到奇怪的細胞行為。

在這種情況下,無論你改變你的幀等等,看看像這樣的東西:

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
[myView.myCALayer.frame = (CGRect){ { 10, 10 }, { 100, 100 } ];
[CATransaction commit];

此處找到上述解決方案。

您可能還必須為圖層設置操作字典,以便為所有操作返回空值。

NSMutableDictionary *newActions = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSNull null], @"sublayers", nil];
superlayer.actions = newActions;

這似乎為時已晚,但對於面臨同樣問題的人,我希望這個答案會有所幫助。 如果我犯了一些明顯(或沒有那么多)的錯誤,請隨時指導我。

編輯:哎呀,我看到我的答案上面的確切事情:(不管怎樣,這是一個更詳細,我只是一個初學者:)

制作app-slot-machines的常用方法是用UIPickerView,也許你應該檢查一下..

如何設置計時器,然后在計時器觸發時調用滾動:

start_index = 0;
dest_index = 20;
timer = [NSTimer scheduledTimerWithTimeInterval:(0.1) target:self selector:@selector(rolling) userInfo:nil repeats:YES];                


- (void)rolling {
start_index++;
if (start_index < dest_index) {
    NSIndexPath *Index = [NSIndexPath indexPathForRow:start_index inSection:0];
    [self.tableView scrollToRowAtIndexPath:Index atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
} else {
    [timer invalidate];
}
}

我一直在使用Sparrow框架的補間功能來做類似的動畫。

上面的鏈接有一個如何設置它的例子。 您可以為任何Objective C對象的任何數字屬性設置動畫,並且可以使用“easy in”,“ease out”,“easy in elastic”等轉換,或者只是使用舊的線性動畫。

屬性contentSize雖然是CGPoint,但是您需要在其中一個類上實際設置不同屬性的動畫,然后為屬性setter函數實現一個實際方法,以便更新contentOffset。

- (void) setTableScrollPosition:(CGFloat)position
{
    CGPoint newOffset = CGPointMake(0.0, position);
    scrollView.contentOffset = newOffset;
}

你不能(據我所知,我一直在尋找一種方法來做到這一點)制造一個不恆定的速度

- scrollToRowAtIndexPath:atScrollPosition:animated:

所以我建議......如果你真的需要它,可以用一些動畫或其他東西制作你自己的東西,或做一些比較容易的東西可以節省你一些時間,使用UIPickerView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM