UIScrollview for comic App

时间:2011-03-08 11:17:31

标签: cocoa-touch uiscrollview uiimageview zooming autorotate

我正在使用ipad应用程序播放漫画,但我无法使用我的滚动视图。我需要这样做:

  • 缩放缩放
  • 轮换
  • 双击并按漫画移动

以下是我想要获得http://vimeo.com/16073699(第二次35 + - )

的示例

现在我有捏缩放但我可以让滚动视图旋转并居中显示图像。

这是我的控制器代码:

#define ZOOM_VIEW_TAG 100
#define ZOOM_STEP 2
#define PAGE_TIME 10

- (void)viewDidLoad{
    [super loadView];

    self.myImage.image = [UIImage imageNamed:@"Tira01.jpg"];

    [self.myImage setTag:ZOOM_VIEW_TAG];

    UISwipeGestureRecognizer *recognizer;


    recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Tap)];
    [(UITapGestureRecognizer *)recognizer setNumberOfTouchesRequired:1];
    [(UITapGestureRecognizer *)drecognizer setNumberOfTapsRequired:1];
    [[self myImage] addGestureRecognizer:recognizer];
    [recognizer release];

    self.drecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    [(UITapGestureRecognizer *)drecognizer setNumberOfTouchesRequired:1];
    [(UITapGestureRecognizer *)drecognizer setNumberOfTapsRequired:2];
    [[self myImage] addGestureRecognizer:drecognizer];
    [drecognizer release];


}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [myScrollView viewWithTag:ZOOM_VIEW_TAG];
}

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
    UIInterfaceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
    if(!self.zoomed)
    {
        // double tap zooms in
        float newScale = [self.myScrollView zoomScale] * ZOOM_STEP;
        CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
        [self.myScrollView zoomToRect:zoomRect animated:YES];
        self.zoomed = YES;


    }
    else {
        float newScale = [self.myScrollView zoomScale] / ZOOM_STEP;
        CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
        [self.myScrollView zoomToRect:zoomRect animated:YES];
        self.zoomed = NO;        
    }

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return YES;
}


- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {

    CGRect zoomRect;

    // the zoom rect is in the content view's coordinates. 
    //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [self.myScrollView frame].size.height / scale;
    zoomRect.size.width  = [self.myScrollView frame].size.width  / scale;

    // choose an origin so as to get the right center.
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);

    return zoomRect;
}

在界面构建器中,我有:

- > UIView(ScaleToFill模式)

- > UIScrollView(ScaleToFill模式)

---> UIImageView(AspectFit模式)

我不知道我做错了什么,但我对此感到疯狂:(

1 个答案:

答案 0 :(得分:0)

关于以内容为中心的UIScrollView,我可能在这里错了,但是UIScrollView不会自动执行此操作。

在我的应用程序中,我通过实际移动scrollView中的内容以使其居中来手动执行此操作 正如下面代码的快速解释,如果内容(图像)框架小于滚动视图的边界,则水平和垂直调整内容的位置。

下面的代码来自我从UIScrollView派生的类。如果您使用的是默认的UIScrollView,则可以在父UIView中使用此代码。



- (void)layoutSubviews 
{
    [super layoutSubviews];

    if (![self centersContent]) 
    {
        return;
    }

    // center the image as it becomes smaller than the size of the screen       
    CGSize boundsSize = self.bounds.size;
    CGRect frameToCenter = imageView.frame;

    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
        frameToCenter.origin.x = 0;

    // center vertically
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    else
        frameToCenter.origin.y = 0;

    imageView.frame = frameToCenter;  
}