我正在尝试实现带有水平和垂直预览的图像幻灯片,并且我希望能够在用户停止滚动并在 5 秒后做这些事情时做一些事情。我使用 2 个计时器来处理这个问题,但我需要一些同步,或者我可能走错了路!为什么我使用 2 个计时器:因为我想处理主视图(-(void)touchesBegan
和-(void)touchesEnded
)中的滚动以及滚动视图()上的滚动UIScrollView delegate
。我的代码在下面,我希望有人可以提供帮助:)。谢谢。
// This is the slideShow view controller
// It display all the images with preview (right, left, up and down)
@interface ImageSlideShowViewController : UIViewController<UIScrollViewDelegate>
{
UIScrollView *scrollViewForSlideShow;
// Timers
NSTimer *autoscrollTimer;
NSTimer *mainViewTimer;
}
@end
@implementation ImageSlideShowViewController
#pragma mark Touch handling
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
....
// Invalidate all timers
[autoscrollTimer invalidate];
autoscrollTimer = nil;
[mainViewTimer invalidate];
mainViewTimer = nil;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
....
assert(mainViewTimer == nil);
mainViewTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:@selector(allViewTimerFireMethod:)
userInfo:nil
repeats:NO];
// prev image horizontal
...
// next image horizontal
....
// prev image vertical
...
// next image vertical
....
}
#pragma mark -
#pragma mark UIScrollView delegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// Invalidate all timers
[autoscrollTimer invalidate];
autoscrollTimer = nil;
[mainViewTimer invalidate];
mainViewTimer = nil;
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
//Doing some management stuff here
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if (!scrollView.dragging)
{
//Doing some management stuff here
assert(autoscrollTimer == nil);
autoscrollTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:@selector(timerFireMethod:)
userInfo:nil
repeats:NO];
}
}
#pragma mark -
#pragma mark Timers methods
- (void)timerFireMethod:(NSTimer*)theTimer
{
autoscrollTimer = nil;
// Call a method that dispaly the image
}
- (void)allViewTimerFireMethod:(NSTimer *)theTimer
{
mainViewTimer = nil;
// Call a method that dispaly the image (same as the method called above)
}
@end