我想对某些UITableView动画的结尾做一系列反应。例如,我希望表格视图单元格通过scrollToRowAtIndexPath滚动后突出显示(通过selectRowAtIndexPath)。如何知道UITableView动画何时完成?
这怎么办?
我想对某些UITableView动画的结尾做一系列反应。例如,我希望表格视图单元格通过scrollToRowAtIndexPath滚动后突出显示(通过selectRowAtIndexPath)。如何知道UITableView动画何时完成?
这怎么办?
那么如果你想在scrollToRowAtIndexPath被触发时执行一个动作。
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
你需要创建一个CAAnimation指针像
CAAnimation *myAnimation;
然后delgate设置为自
myAnimation.delegate = self;
一旦你这样做,这些下面的委托方法应该启动,你可以把您的代码:
- (void)animationDidStart:(CAAnimation *)theAnimation
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
基本模板:
[UIView animateWithDuration:0.2 animations:^{
//do some animations, call them with animated:NO
} completion:^(BOOL finished){
//Do something after animations finished
}];
示例:滚动到行100完成后,在该行获得的细胞,并与标签单元格内容视图= 1至firstResponder:
[UIView animateWithDuration:0.2 animations:^{
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:100 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
} completion:^(BOOL finished){
//Do something after scrollToRowAtIndexPath finished, e.g.:
UITableViewCell *nextCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:100 inSection:0]];
[[nextCell.contentView viewWithTag:1] becomeFirstResponder];
}];
这是迄今为止对于“在scrollToRowAtIndexPath完成后如何执行X”的最清晰的答案?谢谢! – 2013-02-26 10:25:00
我不知道这个解决方案如何在iOS 4/5上工作,但现在在iOS 6.1.4上,我注意到contentOffset被scrollToRowAtIndexPath更改后,这个位置的单元格不可见,所以对于第二个tableview的分数是完全空白的,然后绘制单元格并按预期显示键盘。我猜UITableView并不是用于这种方式,或者至少不是在这个版本的iOS中。使用setContentOffset而不是scrollToRowAtIndexPath可以得到相同的结果。 – ggould75 2013-05-23 10:38:43
我不认为这是可行的 – shim 2013-07-03 03:37:25
我意识到这是一个旧帖子,但我遇到了类似的问题,并创建了一个适用于我的解决方案。我将NSCookBook上使用的技术用于创建带有块的UIAlertView。我之所以这样做是因为我想使用内置的动画而不是UIView的+ animateWithDuration:动画:completion :.这些动画与iOS 7的更改有较大的区别。
您为UITableView创建一个类别,并在实现文件中创建一个内部私有类,该类将通过将其分配为您的tableview委托来回调该块。可以肯定的是,在块被调用之前,原来的委托将会“丢失”,因为新委托是调用块的对象。这就是为什么当调用块重新分配原始UITableViewDelegate时,我会通知发送消息。这段代码已经过测试,并且正在处理我的工作。
// Header file
@interface UITableView (ScrollDelegateBlock)
-(void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath
atScrollPosition:(UITableViewScrollPosition)scrollPosition
animated:(BOOL)animated
scrollFinished:(void (^)())scrollFinished;
@end
// Implementation file
#import "UITableView+ScrollDelegateBlock.h"
#import <objc/runtime.h>
NSString *const BLOCK_CALLED_NOTIFICATION = @"BlockCalled";
@interface ScrollDelegateWrapper : NSObject <UITableViewDelegate>
@property (copy) void(^scrollFinishedBlock)();
@end
@implementation ScrollDelegateWrapper
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
if (self.scrollFinishedBlock) {
[[NSNotificationCenter defaultCenter] postNotificationName:BLOCK_CALLED_NOTIFICATION object:nil];
self.scrollFinishedBlock();
}
}
@end
static const char kScrollDelegateWrapper;
static id<UITableViewDelegate>previousDelegate;
@implementation UITableView (ScrollDelegateBlock)
-(void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath
atScrollPosition:(UITableViewScrollPosition)scrollPosition
animated:(BOOL)animated
scrollFinished:(void (^)())scrollFinished {
previousDelegate = self.delegate;
ScrollDelegateWrapper *scrollDelegateWrapper = [[ScrollDelegateWrapper alloc] init];
scrollDelegateWrapper.scrollFinishedBlock = scrollFinished;
self.delegate = scrollDelegateWrapper;
objc_setAssociatedObject(self, &kScrollDelegateWrapper, scrollDelegateWrapper, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(blockCalled:)
name:BLOCK_CALLED_NOTIFICATION
object:nil];
}
/*
* Assigns delegate back to the original delegate
*/
-(void) blockCalled:(NSNotification *)notification {
self.delegate = previousDelegate;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:BLOCK_CALLED_NOTIFICATION
object:nil];
}
@end
然后,您可以调用像任何其他与块的方法:
[self.tableView scrollToRowAtIndexPath:self.currentPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES
scrollFinished:^{
NSLog(@"scrollFinished");
}
];
你是如何初始化你的'myAnimation'指针? – 2011-09-01 22:11:25