iOS:UIView动画块方法在多次火灾中奇怪地制作动画

时间:2014-05-05 12:28:13

标签: ios uiwebview uiswipegesturerecognizer

我正在使用2 UIWebViews进行页面滑动动画以呈现书页效果。 下次和上次点击时会触发滑动。

动画正常工作,直到连续多次点击下一个或上一个按钮为止。

这就是我在做的事情:

  • 默认显示webview1
  • 右侧滑动
  • ,使webview1帧x原点为负值以隐藏它。并将webview2 frame x origin设为0以显示它。
  • 同样适用于左侧滑动。

这是我要刷的代码:

-(void)pageChange
{
    @try{


        [UIView commitAnimations];
        NSString *finalPath;

        finalPath = [NSString stringWithFormat:@"%@/%@",self.strBookPath, [arrHtmlPages objectAtIndex:currentPage]];


        NSData *htmlData = [NSData dataWithContentsOfFile:finalPath];
        __block CGRect basketTopFrame = webViewPage.frame;



        [UIView animateWithDuration:DURATION delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{

             if (_webview2.frame.origin.x == self.view.frame.size.width) {
                basketTopFrame.origin.x = -(self.view.frame.size.width);
                [webViewPage setFrame:basketTopFrame];
                basketTopFrame.origin.x = self.view.frame.size.width;
                _webview2.frame = basketTopFrame;



                [_webview2 loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:self.strBookPath isDirectory:YES]];

                [UIView animateWithDuration:DURATION delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
                    //basketTopFrame = webViewPage.frame;

                    basketTopFrame.origin.x = 0;
                    _webview2.frame = basketTopFrame;
                    //NSLog(@"web1: %f",webViewPage.frame.origin.x);
                    //NSLog(@"web2 : %f", _webview2.frame.origin.x);

                } completion:^(BOOL finished){
                    basketTopFrame.origin.x = self.view.frame.size.width;
                    webViewPage.frame=basketTopFrame;
                }];
            } else {
                basketTopFrame.origin.x = -(self.view.frame.size.width);
                [_webview2 setFrame:basketTopFrame];
                basketTopFrame.origin.x = self.view.frame.size.width;
                webViewPage.frame = basketTopFrame;


                [webViewPage loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:self.strBookPath isDirectory:YES]];


                [UIView animateWithDuration:DURATION delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
                    //basketTopFrame = webViewPage.frame;

                    basketTopFrame.origin.x = 0;
                    webViewPage.frame = basketTopFrame;
                    //NSLog(@"web1 in: %f",webViewPage.frame.origin.x);
                    //NSLog(@"web2 in: %f", _webview2.frame.origin.x);

                } completion:^(BOOL finished){
                    basketTopFrame.origin.x = self.view.frame.size.width;
                    _webview2.frame=basketTopFrame;
                }];
            }

        } completion:^(BOOL finished){
            NSLog(@"web1 in: %f",webViewPage.frame.origin.x);
            NSLog(@"web2 in: %f", _webview2.frame.origin.x);
        }];
    }@catch(NSException *e){
        NSLog(@"exception : %@",e.description);
    }

}

此代码仅适用于右键滑动。左侧滑动也有类似的代码。

- (IBAction)btnNext_click:(id)sender {
    [self swipeRight:nil]; //this calls pageChange()
}

当上述方法以非常短的间隔(用户严格点击它)多次触发时,两个webview都将frame x origin作为设备的宽度(在我的情况下为768),并且两者都被隐藏。

我哪里出错了?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

有一个简单的解决方案:在动画期间禁用按钮。查找按钮的userInteractionEnabled属性,并在动画期间将其设置为NO。动画完成后,再次启用它。使用基于块的动画非常方便,因为您将在完成处理程序中启用它。希望这会有所帮助。