ios - 以编程方式滚动 UIScrollView 但不仅仅在委托(delegate)方法中处理此操作?

标签 ios objective-c animation uiscrollview delegates

在我的类(class)中,我编写了以下委托(delegate)方法:

scrollViewDidScroll:
scrollViewDidEndScrollingAnimation:

然后对于这个 scrollView 我使用下面的代码:

scrollView.frame = ...;
scrollView.contentSize = ...;
[scrollView setContentOffset:... animated:NO];

代码和委托(delegate)在主线程中工作,但彼此异步。

问题是我不想用这段代码调用描述的委托(delegate)方法。如何解决这个问题?我不能简单地写:

BOOL shouldCallDelegate = YES; //or NO

因为我还有其他代码需要通过这些委托(delegate)方法处理。

已更新

我试过这样的:

NSLog(@"isMainThread == %d", [NSThread isMainThread]);
NSLog(@"isScrollViewBusy == %d", self.isScrollViewBusy);
self.isScrollViewBusy = YES;
scrollView.frame = ...;
scrollView.contentSize = ...;
[scrollView setContentOffset:... animated:NO];
self.isScrollViewBusy = NO;

然后在我的委托(delegate)方法中:

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {

   NSLog(@"isMainThread2 == %d", [NSThread isMainThread]);
   NSLog(@"isScrollViewBusy2 == %d", self.isScrollViewBusy);
   ...
}

你认为应用程序必须写这样的东西吗?

isMainThread == 1
isScrollViewBusy == 0
isMainThread2 == 1
isScrollViewBusy2 == 0

在大多数情况下是这样,但是当您尝试旋转屏幕时:

isMainThread == 1
isScrollViewBusy == 0
isMainThread2 == 1
isScrollViewBusy2 == 1

所以所有代码都在主线程中,但突然间代码的第一部分暂停?!当应用程序正在处理旋转并且委托(delegate)方法被意外调用时介于 YES 和 NO 之间

最佳答案

id<UIScrollViewDelegate> delegate = scrollView.delegate;
scrollView.delegate = nil;
// do your stuff
scrollView.delegate = delegate

关于ios - 以编程方式滚动 UIScrollView 但不仅仅在委托(delegate)方法中处理此操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28899309/

相关文章:

带有 AnimationDrawable 源的 Android Imageview 改变大小

javascript - replaceWith & fadeIn/fadeOut - 悬停之后不起作用

ios - 通用 UIPopoverPresentationController 代码

ios - 无论是否在托管上下文 iOS 上调用保存,Core Data 中的实体是否会保留其状态?

ios - 从选项卡 Controller 在当前上下文中呈现 Model View Controller 后出现黑屏

objective-c - 为什么一个空的 UIScrollView 有两个 subview ?

objective-c - 如何更改 NSArray 中的元素?

html - 向右平移一个 div 并让它消失在一条看不见的线后面

javascript - iOS UIWebView 不是 100% 兼容 iOS 上的 Safari?

ios - 不了解如何在 ViewController 之间传递值