什么可能导致UIScrollView丢失部分scrollview委托?

时间:2011-10-24 03:13:49

标签: ios uiscrollview uikit

我有一个奇怪的滚动视图。它非常“不敏感地”滚动,没有加速和减速,没有反弹也起作用。这可能是造成这种情况的原因之一?

我怀疑它与我试图制作的视频录制有关。这个问题似乎只有在我尝试用AVFoundation拍摄视频后才会发生......

由于我使用相同的委托有多个scrollview,以下是我的代表的代码......

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
        //switch if reachs halfway
        CGFloat pageWidth = scrollView.frame.size.width;
        int page = floor ((scrollView.contentOffset.x - pageWidth/2) / pageWidth) +1;
        if(self.pageControl.currentPage != page){
            [self performSelector:@selector(switchingTitle)];
            NSLog(@"scroll to change view");
        }
        NSLog(@"scrollviewdidscroll.");
        self.pageControl.currentPage = page;
    }

    -(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
        NSLog(@"scrollviewwillbegindragging");
    }

    -(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
        if(decelerate == NO){
             NSLog(@"no deceleration.");
        }
        NSLog(@"scrollviewdidenddragging");
    }

    -(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
        [self.content setDecelerationRate:UIScrollViewDecelerationRateNormal];
        NSLog(@"scrollviewwillbegindecelerating %f", self.content.decelerationRate);
    }

    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        NSLog(@"scrollviewdidenddeceleration");
    }

以下是当我进行拖动时打印输出的样子...当我再次尝试拖动时,它将重复此模式。

    2011-10-25 13:03:45.503  scrollviewwillbegindragging
    2011-10-25 13:03:45.507  scrollviewdidscroll.
    2011-10-25 13:03:45.536  scrollviewdidscroll.
    2011-10-25 13:03:45.560  scrollviewdidscroll.
    2011-10-25 13:03:45.565  scrollviewdidscroll.
    2011-10-25 13:03:45.580  scrollviewdidenddragging
    2011-10-25 13:03:45.582  scrollviewwillbegindecelerating 0.998000

编辑:在下面添加了我的视频录制代码...并非所有都在这里,但这是照片录制的主要批量,我怀疑是滚动视图停止正常工作的原因

-(void) startPreview {
     //preview layer
    if(previewLayer){ previewLayer = nil; }
    previewLayer =  [AVCaptureVideoPreviewLayer layerWithSession:session];  

    if( UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]))
        self.previewLayer.orientation = [[UIDevice currentDevice] orientation];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatepreview) name:UIDeviceOrientationDidChangeNotification object:nil]; 

    self.previewLayer.frame = CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height);
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [imageView.layer addSublayer:previewLayer]; 
}


-(void) captureSession{ 
    NSLog(@"captureSession");
    videoCaptureCompleted = NO;
    if (interrupted) {
        interrupted = NO;
    }
    //capture session
    if([session isRunning]){
        [session stopRunning];
        NSLog(@"session stopRunning");
        [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
        session =nil;
    }

    session = [[AVCaptureSession alloc]init];

    [self startPreview];
    NSLog(@"start Preview");
    //capture device
    cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];  

    //capture device input
    NSError *error=nil;  
    AVCaptureDeviceInput* cameraInputTemp = [[AVCaptureDeviceInput alloc] initWithDevice:cameraDevice error:&error]; 
    self.cameraInput = [cameraInputTemp retain];
    [cameraInputTemp release];
    if(!self.cameraInput){
        NSLog(@"camera input not found");
        return;
    }

    movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

    CMTime maxDuration = CMTimeMake(10, 1);
    movieFileOutput.maxRecordedDuration = maxDuration;
    [session setSessionPreset:AVCaptureSessionPresetMedium];

    // Add the input and output  
    [session addInput:self.cameraInput];  

    if([session canAddOutput:movieFileOutput]){
        [session addOutput:movieFileOutput];
    }else{
        NSLog(@"session refuses to add output");
    }

    [session startRunning]; 

    [self performSelector:@selector(prepareToRecording:)  withObject:[NSNumber numberWithInt:takeCount] afterDelay:5.0];
}

-(void) prepareToRecording{
        [cameraDevice unlockForConfiguration]; 
        NSError* lockingError;
        if([cameraDevice lockForConfiguration:&lockingError]){
            [cameraDevice setFocusMode:AVCaptureFocusModeAutoFocus];
        }
        while(cameraDevice.adjustingFocus){
            [self performSelector:@selector(waitForFetch)];
        } 
        if (!cameraDevice.adjustingFocus){
            [self performSelector:@selector(startVideoRecording:) withObject:takeCt afterDelay:3.0];
        }
    }
}

 -(void)startRecording{
        [[self movieFileOutput] startRecordingToOutputFileURL:URLForVideo recordingDelegate:self];
 }

-(void) stopRecording{
    videoCaptureCompleted = YES;
    while ([movieFileOutput isRecording]) {
        [self.movieFileOutput stopRecording];
        NSLog(@"movieFileOutput stopRecording");
        [self performSelector:@selector(waitForFetch)];
    }

    //unlock configuration of autofocusing only once
    [cameraDevice unlockForConfiguration];

    if(![movieFileOutput isRecording]){
        [session performSelector:@selector(stopRunning) withObject:nil afterDelay:1.0];
    }
}

-(void) captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)URLForVideo fromConnections:(NSArray *)connections{
    NSLog(@"start recording video");

}

-(void) captureOutput:(AVCaptureFileOutput*) movieFileOutput didFinishRecordingToOutputFileAtURL: (NSURL*)URLForVideo fromConnections:(NSArray*)connections error:(NSError *)error {
    NSLog(@"recorded video");
}

-(void) startVideoRecording{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
    if(!timerOn){
        timerLabel.text = @"Recording is starting!";
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:takeCt repeats:YES];
    }
}

还有其他帮助吗?

2 个答案:

答案 0 :(得分:0)

您可能已在InterfaceBuilder中或以编程方式打开/关闭滚动视图的某些功能(属性)。要自定义这些功能属性(根据您的需要进行调整):

scrollView.pagingEnabled = NO;
scrollView.bounces = YES;
scrollView.bouncesZoom = YES;

UIScrollView Reference Docs中的更多属性。您也可以将decelerationRate调整为您需要的内容。

答案 1 :(得分:0)

如果精确点击滚动视图,则不会调用这些功能(减速)。然而,DidScroll将被调用。但是,如果您快速轻按(快速移动),则以常规方式滚动查看呼叫will begin/end decelerating。某些人很难在模拟器中轻弹。在设备上试试。