2012-12-11 39 views

回答

0

因此,除了使用第三方庫,我解決了使用這個問題:

iOS. How do I restrict UIScrollview scrolling to a limited extent?,適應回答下面的方法,這是我希望的自我解釋:

- (CGRect) methodThatGetsImageSizeOnScreen 
{ 

float frameHeight; 
float frameWidth; 
float frameXOrigin; 
float frameYOrigin; 

float threshold; 
BOOL thisImageTouchesLeftAndRight; 

UIInterfaceOrientation thisOrientation = self.interfaceOrientation; 

if (UIInterfaceOrientationIsLandscape(thisOrientation)){ 

    threshold = 748.0/1024.0; 

    if ((self.imageToPresent.size.height == self.imageToPresent.size.width) | ((self.imageToPresent.size.height/self.imageToPresent.size.width) > threshold)){ 

     thisImageTouchesLeftAndRight = NO; 

     frameWidth = (748/self.imageToPresent.size.height)*self.imageToPresent.size.width; 
     frameHeight = 748; 
     frameXOrigin = (1024-frameWidth)/2; 
     frameYOrigin = 0; 
    } 
    else 
    { 
     thisImageTouchesLeftAndRight = YES; 

     frameWidth = 1024; 
     frameHeight = (1024/self.imageToPresent.size.width)*self.imageToPresent.size.height; 
     frameXOrigin = 0; 
     frameYOrigin = (748-frameHeight)/2; 
    } 
} 
else { 

    threshold = 768.0/1004.0; 

    if ((self.imageToPresent.size.height == self.imageToPresent.size.width) | ((self.imageToPresent.size.width/self.imageToPresent.size.height) > threshold)){ 

     thisImageTouchesLeftAndRight = YES; 

     frameWidth = 768; 
     frameHeight = (768/self.imageToPresent.size.width)*self.imageToPresent.size.height; 
     frameXOrigin = 0; 
     frameYOrigin = (1004-frameHeight)/2; 
    } 
    else 
    { 
     thisImageTouchesLeftAndRight = NO; 

     frameWidth = (1004/self.imageToPresent.size.height)*self.imageToPresent.size.width; 
     frameHeight = 1004; 
     frameXOrigin = (768-frameWidth)/2; 
     frameYOrigin = 0; 
    } 


} 

CGRect theRect = CGRectMake(frameXOrigin, frameYOrigin, frameWidth, frameHeight); 

return theRect; 
} 

#pragma mark - UIScrollViewDelegate 

- (void) scrollViewDidScroll:(UIScrollView*)scroll{ 

UIInterfaceOrientation thisOrientation = self.interfaceOrientation; 

float largeDimension; 
float smallDimension; 

if (UIInterfaceOrientationIsLandscape(thisOrientation)){ 

    largeDimension = 1024; 
    smallDimension = 748; 
} 
else{ 

    largeDimension = 1004; 
    smallDimension = 768; 

} 

CGPoint offset = scroll.contentOffset; 

CGRect results = [self methodThatGetsImageSizeOnScreen]; 


float frameHeight = results.size.height; 
float frameYOrigin = results.origin.y; 


float frameWidth = results.size.width; 
float frameXOrigin = results.origin.x; 


//So, we start the limiting of a landscape image in portrait (in the y direction) when we exceed the following criteria: 

if((frameHeight*self.scrollView.zoomScale) > largeDimension){ 

    if(offset.y < self.scrollView.zoomScale*frameYOrigin) offset.y = self.scrollView.zoomScale*frameYOrigin; 

    if(offset.y > ((self.scrollView.zoomScale*frameYOrigin)+(frameHeight*self.scrollView.zoomScale)-largeDimension)) offset.y = ((self.scrollView.zoomScale*frameYOrigin)+(frameHeight*self.scrollView.zoomScale)-largeDimension); 

} 

if((frameWidth*self.scrollView.zoomScale) > largeDimension){ 

    if(offset.x < self.scrollView.zoomScale*frameXOrigin) offset.x = self.scrollView.zoomScale*frameXOrigin; 

    if(offset.x > ((self.scrollView.zoomScale*frameXOrigin)+(frameWidth*self.scrollView.zoomScale)-largeDimension)) offset.x = ((self.scrollView.zoomScale*frameXOrigin)+(frameWidth*self.scrollView.zoomScale)-largeDimension); 

} 

    // Set offset to adjusted value 
    scroll.contentOffset = offset; 

//Remember you may want your minimum zoomScale set in viewDidLoad or viewWillAppear 

} 
0

你可以使用這樣做的庫。嘗試MWPhotoBrowser

+0

感謝。我會檢查出來,讓你知道如果這對我有用。 –