2011-03-08 89 views
0

我正在爲漫畫製作ipad應用程序,但我無法讓我的滾動視圖正常工作。我需要做的:漫畫應用程序的UIScroll視圖

  • 雙指縮放
  • 旋轉
  • 雙擊和連環畫

Here's什麼,我想http://vimeo.com/16073699(二35爲例移動+ - )

現在我有捏縮放,但我可以得到滾動視圖旋轉和居中圖像。

Here's我的控制器的代碼:

#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模式)

我不知道我是什麼我錯了,但我會瘋狂的那個:(

回答

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; 
}