水平方向と垂直方向のプレビューを使用して画像のスライドショーを実装しようとしています。ユーザーがスクロールを停止して 5 秒後にこの操作を実行したときに、いくつかの操作を実行できるようにしたいと考えています。これを処理するために2つのタイマーを使用していますが、同期が必要か、間違った方法をとっている可能性があります! 2 つのタイマーを使用する理由: メイン ビュー (-(void)touchesBegan
および-(void)touchesEnded
) でのスクロールと scrollView( 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