2010-06-02 8 views
0

J'ai joué avec l'un des exemples d'iPhone du site Web d'Apple (ScrollViewSuite). J'essaye de l'ajuster un peu de sorte que quand je tourne l'iPad l'image rentrera dans l'écran en mode paysage vertical. J'ai réussi à faire pivoter l'image, mais l'image est plus grande que la hauteur de l'écran paysage, donc le bas est en dessous de l'écran. Je voudrais faire une image à l'échelle de la hauteur de l'écran de paysage.Ajuster une image à l'écran sur la rotation iPhone/iPad?

J'ai joué avec divers attributs autoSizingMask sans succès. L'imageView s'appelle "zoomView". C'est l'image réelle qui se charge dans un scrollView appelé imageScrollView.

Je suis en train de réaliser l'écran pour faire pivoter et ressembler à ceci .... désolé seulement 1 lien autorisé nouvel utilisateur :(

olsonvox.com/photos/correct.png~~V~~3rd

Cependant, cette est ce que mon écran est à la recherche comme.

http://www.olsonvox.com/photos/incorrect.png

J'apprécierais vraiment des conseils ou des conseils. Ci-dessous figure la RootViewController.m pour le projet.

  • Lame

    #

    import "RootViewController.h" 
    
        #define ZOOM_VIEW_TAG 100 
        #define ZOOM_STEP 1.5 
    
        #define THUMB_HEIGHT 150 
        #define THUMB_V_PADDING 25 
        #define THUMB_H_PADDING 25 
        #define CREDIT_LABEL_HEIGHT 25 
    
        #define AUTOSCROLL_THRESHOLD 30 
    
        @interface RootViewController (ViewHandlingMethods) 
        - (void)toggleThumbView; 
        - (void)pickImageNamed:(NSString *)name; 
        - (NSArray *)imageNames; 
        - (void)createThumbScrollViewIfNecessary; 
        - (void)createSlideUpViewIfNecessary; 
        @end 
    
        @interface RootViewController (AutoscrollingMethods) 
        - (void)maybeAutoscrollForThumb:(ThumbImageView *)thumb; 
        - (void)autoscrollTimerFired:(NSTimer *)timer; 
        - (void)legalizeAutoscrollDistance; 
        - (float)autoscrollDistanceForProximityToEdge:(float)proximity; 
        @end 
    
        @interface RootViewController (UtilityMethods) 
        - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center; 
        @end 
    
    
        @implementation RootViewController 
    
        - (void)loadView { 
         [super loadView]; 
    
         imageScrollView = [[UIScrollView alloc] initWithFrame:[[self view]bounds]]; 
    
        // this code makes the image resize to the width and height properly. 
        imageScrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin| UIViewAutoresizingFlexibleBottomMargin| UIViewAutoresizingFlexibleBottomMargin; 
    
        // TRY SETTNG CENTER HERE SOMEHOW>.... 
    
    
        [imageScrollView setBackgroundColor:[UIColor blackColor]]; 
         [imageScrollView setDelegate:self]; 
         [imageScrollView setBouncesZoom:YES]; 
         [[self view] addSubview:imageScrollView]; 
        [self toggleThumbView]; 
    
    
        // intitializes with the first image. 
    
         [self pickImageNamed:@"lookbook1"]; 
        } 
    
        - (void)dealloc { 
         [imageScrollView release]; 
         [slideUpView release]; 
         [thumbScrollView release]; 
         [super dealloc]; 
        } 
    
        #pragma mark UIScrollViewDelegate methods 
    
        - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 
         UIView *view = nil; 
         if (scrollView == imageScrollView) { 
          view = [imageScrollView viewWithTag:ZOOM_VIEW_TAG]; 
         } 
         return view; 
        } 
    
        /************************************** NOTE **************************************/ 
        /* The following delegate method works around a known bug in zoomToRect:animated: */ 
        /* In the next release after 3.0 this workaround will no longer be necessary  */ 
        /**********************************************************************************/ 
        - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale { 
         [scrollView setZoomScale:scale+0.01 animated:NO]; 
         [scrollView setZoomScale:scale animated:NO]; 
        } 
    
        #pragma mark TapDetectingImageViewDelegate methods 
    
        - (void)tapDetectingImageView:(TapDetectingImageView *)view gotSingleTapAtPoint:(CGPoint)tapPoint { 
         // Single tap shows or hides drawer of thumbnails. 
         [self toggleThumbView]; 
        } 
    
        - (void)tapDetectingImageView:(TapDetectingImageView *)view gotDoubleTapAtPoint:(CGPoint)tapPoint { 
         // double tap zooms in 
         float newScale = [imageScrollView zoomScale] * ZOOM_STEP; 
         CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint]; 
         [imageScrollView zoomToRect:zoomRect animated:YES]; 
        } 
    
        - (void)tapDetectingImageView:(TapDetectingImageView *)view gotTwoFingerTapAtPoint:(CGPoint)tapPoint { 
         // two-finger tap zooms out 
         float newScale = [imageScrollView zoomScale]/ZOOM_STEP; 
         CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint]; 
         [imageScrollView zoomToRect:zoomRect animated:YES]; 
        } 
    
        #pragma mark ThumbImageViewDelegate methods 
    
        - (void)thumbImageViewWasTapped:(ThumbImageView *)tiv { 
         [self pickImageNamed:[tiv imageName]]; 
         [self toggleThumbView]; 
        } 
    
        - (void)thumbImageViewStartedTracking:(ThumbImageView *)tiv { 
         [thumbScrollView bringSubviewToFront:tiv]; 
        } 
    
    
        // CONTROLS DRAGGING AND DROPPING THUMBNAILS... 
    
        - (void)thumbImageViewMoved:(ThumbImageView *)draggingThumb { 
    
         // check if we've moved close enough to an edge to autoscroll, or far enough away to stop autoscrolling 
         [self maybeAutoscrollForThumb:draggingThumb]; 
    
         /* The rest of this method handles the reordering of thumbnails in the thumbScrollView. See */ 
         /* ThumbImageView.h and ThumbImageView.m for more information about how this works.   */ 
    
         // we'll reorder only if the thumb is overlapping the scroll view 
         if (CGRectIntersectsRect([draggingThumb frame], [thumbScrollView bounds])) {   
    
          BOOL draggingRight = [draggingThumb frame].origin.x > [draggingThumb home].origin.x ? YES : NO; 
    
          /* we're going to shift over all the thumbs who live between the home of the moving thumb */ 
          /* and the current touch location. A thumb counts as living in this area if the midpoint */ 
          /* of its home is contained in the area.             */ 
          NSMutableArray *thumbsToShift = [[NSMutableArray alloc] init]; 
    
          // get the touch location in the coordinate system of the scroll view 
          CGPoint touchLocation = [draggingThumb convertPoint:[draggingThumb touchLocation] toView:thumbScrollView]; 
    
          // calculate minimum and maximum boundaries of the affected area 
          float minX = draggingRight ? CGRectGetMaxX([draggingThumb home]) : touchLocation.x; 
          float maxX = draggingRight ? touchLocation.x : CGRectGetMinX([draggingThumb home]); 
    
          // iterate through thumbnails and see which ones need to move over 
          for (ThumbImageView *thumb in [thumbScrollView subviews]) { 
    
           // skip the thumb being dragged 
           if (thumb == draggingThumb) continue; 
    
           // skip non-thumb subviews of the scroll view (such as the scroll indicators) 
           if (! [thumb isMemberOfClass:[ThumbImageView class]]) continue; 
    
           float thumbMidpoint = CGRectGetMidX([thumb home]); 
           if (thumbMidpoint >= minX && thumbMidpoint <= maxX) { 
            [thumbsToShift addObject:thumb]; 
           } 
          } 
    
          // shift over the other thumbs to make room for the dragging thumb. (if we're dragging right, they shift to the left) 
          float otherThumbShift = ([draggingThumb home].size.width + THUMB_H_PADDING) * (draggingRight ? -1 : 1); 
    
          // as we shift over the other thumbs, we'll calculate how much the dragging thumb's home is going to move 
          float draggingThumbShift = 0.0; 
    
          // send each of the shifting thumbs to its new home 
          for (ThumbImageView *otherThumb in thumbsToShift) { 
           CGRect home = [otherThumb home]; 
           home.origin.x += otherThumbShift; 
           [otherThumb setHome:home]; 
           [otherThumb goHome]; 
           draggingThumbShift += ([otherThumb frame].size.width + THUMB_H_PADDING) * (draggingRight ? 1 : -1); 
          } 
    
          // change the home of the dragging thumb, but don't send it there because it's still being dragged 
          CGRect home = [draggingThumb home]; 
          home.origin.x += draggingThumbShift; 
          [draggingThumb setHome:home]; 
         } 
        } 
    
        - (void)thumbImageViewStoppedTracking:(ThumbImageView *)tiv { 
         // if the user lets go of the thumb image view, stop autoscrolling 
         [autoscrollTimer invalidate]; 
         autoscrollTimer = nil; 
        } 
    
        #pragma mark Autoscrolling methods 
    
        - (void)maybeAutoscrollForThumb:(ThumbImageView *)thumb { 
    
         autoscrollDistance = 0; 
    
         // only autoscroll if the thumb is overlapping the thumbScrollView 
         if (CGRectIntersectsRect([thumb frame], [thumbScrollView bounds])) { 
    
          CGPoint touchLocation = [thumb convertPoint:[thumb touchLocation] toView:thumbScrollView]; 
          float distanceFromLeftEdge = touchLocation.x - CGRectGetMinX([thumbScrollView bounds]); 
          float distanceFromRightEdge = CGRectGetMaxX([thumbScrollView bounds]) - touchLocation.x; 
    
          if (distanceFromLeftEdge < AUTOSCROLL_THRESHOLD) { 
           autoscrollDistance = [self autoscrollDistanceForProximityToEdge:distanceFromLeftEdge] * -1; // if scrolling left, distance is negative 
          } else if (distanceFromRightEdge < AUTOSCROLL_THRESHOLD) { 
           autoscrollDistance = [self autoscrollDistanceForProximityToEdge:distanceFromRightEdge]; 
          }   
         } 
    
         // if no autoscrolling, stop and clear timer 
         if (autoscrollDistance == 0) { 
          [autoscrollTimer invalidate]; 
          autoscrollTimer = nil; 
         } 
    
         // otherwise create and start timer (if we don't already have a timer going) 
         else if (autoscrollTimer == nil) { 
          autoscrollTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0/60.0) 
                       target:self 
                      selector:@selector(autoscrollTimerFired:) 
                      userInfo:thumb 
                       repeats:YES]; 
         } 
        } 
    
        - (float)autoscrollDistanceForProximityToEdge:(float)proximity { 
         // the scroll distance grows as the proximity to the edge decreases, so that moving the thumb 
         // further over results in faster scrolling. 
         return ceilf((AUTOSCROLL_THRESHOLD - proximity)/5.0); 
        } 
    
        - (void)legalizeAutoscrollDistance { 
         // makes sure the autoscroll distance won't result in scrolling past the content of the scroll view 
         float minimumLegalDistance = [thumbScrollView contentOffset].x * -1; 
         float maximumLegalDistance = [thumbScrollView contentSize].width - ([thumbScrollView frame].size.width + [thumbScrollView contentOffset].x); 
         autoscrollDistance = MAX(autoscrollDistance, minimumLegalDistance); 
         autoscrollDistance = MIN(autoscrollDistance, maximumLegalDistance); 
        } 
    
        - (void)autoscrollTimerFired:(NSTimer*)timer { 
         [self legalizeAutoscrollDistance]; 
    
         // autoscroll by changing content offset 
         CGPoint contentOffset = [thumbScrollView contentOffset]; 
         contentOffset.x += autoscrollDistance; 
         [thumbScrollView setContentOffset:contentOffset]; 
    
         // adjust thumb position so it appears to stay still 
         ThumbImageView *thumb = (ThumbImageView *)[timer userInfo]; 
         [thumb moveByOffset:CGPointMake(autoscrollDistance, 0)]; 
        } 
    
        #pragma mark View handling methods 
    
        - (void)toggleThumbView { 
         [self createSlideUpViewIfNecessary]; // no-op if slideUpView has already been created 
         CGRect frame = [slideUpView frame]; 
         if (thumbViewShowing) { 
          frame.origin.y = 0; 
         } else { 
          frame.origin.y = -225; 
         } 
    
         [UIView beginAnimations:nil context:nil]; 
         [UIView setAnimationDuration:0.3]; 
         [slideUpView setFrame:frame]; 
         [UIView commitAnimations]; 
    
         thumbViewShowing = !thumbViewShowing; 
        } 
    
        - (void)pickImageNamed:(NSString *)name { 
    
         // first remove previous image view, if any 
         [[imageScrollView viewWithTag:ZOOM_VIEW_TAG] removeFromSuperview]; 
    
         UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg", name]]; 
         TapDetectingImageView *zoomView = [[TapDetectingImageView alloc] initWithImage:image]; 
    
    
        zoomView.autoresizingMask = UIViewAutoresizingFlexibleWidth ; 
    
    
         [zoomView setDelegate:self]; 
         [zoomView setTag:ZOOM_VIEW_TAG]; 
         [imageScrollView addSubview:zoomView]; 
         [imageScrollView setContentSize:[zoomView frame].size]; 
         [zoomView release]; 
    
         // choose minimum scale so image width fits screen 
         float minScale = [imageScrollView frame].size.width/[zoomView frame].size.width; 
         [imageScrollView setMinimumZoomScale:minScale]; 
         [imageScrollView setZoomScale:minScale]; 
         [imageScrollView setContentOffset:CGPointZero]; 
        } 
    
        - (NSArray *)imageNames { 
    
         // the filenames are stored in a plist in the app bundle, so create array by reading this plist 
         NSString *path = [[NSBundle mainBundle] pathForResource:@"Images" ofType:@"plist"]; 
         NSData *plistData = [NSData dataWithContentsOfFile:path]; 
         NSString *error; NSPropertyListFormat format; 
         NSArray *imageNames = [NSPropertyListSerialization propertyListFromData:plistData 
                       mutabilityOption:NSPropertyListImmutable 
                         format:&format 
                       errorDescription:&error]; 
         if (!imageNames) { 
          NSLog(@"Failed to read image names. Error: %@", error); 
          [error release]; 
         } 
    
         return imageNames; 
        } 
    
    
    
    
        - (void)createSlideUpViewIfNecessary { 
    
         if (!slideUpView) { 
    
          [self createThumbScrollViewIfNecessary]; 
    
          CGRect bounds = [[self view] bounds]; 
          float thumbHeight = [thumbScrollView frame].size.height; 
          float labelHeight = CREDIT_LABEL_HEIGHT; 
    
          // create label giving credit for images 
          UILabel *creditLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, thumbHeight, bounds.size.width, labelHeight)]; 
    
          [creditLabel setBackgroundColor:[UIColor clearColor]]; 
          [creditLabel setTextColor:[UIColor whiteColor]]; 
    
        // [creditLabel setFont:[UIFont fontWithName:@"Helvetica" size:16]]; 
        // [creditLabel setText:@"SAMPLE TEXT"]; 
    
          [creditLabel setTextAlignment:UITextAlignmentCenter];   
    
    
          // create container view that will hold scroll view and label 
          CGRect frame = CGRectMake(0.0, -225.00, bounds.size.width+256, thumbHeight + labelHeight); 
         slideUpView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; 
          slideUpView = [[UIView alloc] initWithFrame:frame]; 
          [slideUpView setBackgroundColor:[UIColor blackColor]]; 
          [slideUpView setOpaque:NO]; 
          [slideUpView setAlpha:.75]; 
          [[self view] addSubview:slideUpView]; 
    
          // add subviews to container view 
          [slideUpView addSubview:thumbScrollView]; 
          [slideUpView addSubview:creditLabel]; 
          [creditLabel release]; 
    
    
    
         }  
        } 
    
        - (void)createThumbScrollViewIfNecessary { 
    
         if (!thumbScrollView) {   
    
          float scrollViewHeight = THUMB_HEIGHT + THUMB_V_PADDING; 
          float scrollViewWidth = [[self view] bounds].size.width; 
          thumbScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth, scrollViewHeight)]; 
          [thumbScrollView setCanCancelContentTouches:NO]; 
          [thumbScrollView setClipsToBounds:NO]; 
    
          // now place all the thumb views as subviews of the scroll view 
          // and in the course of doing so calculate the content width 
          float xPosition = THUMB_H_PADDING; 
          for (NSString *name in [self imageNames]) { 
           UIImage *thumbImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_thumb.jpg", name]]; 
           if (thumbImage) { 
            ThumbImageView *thumbView = [[ThumbImageView alloc] initWithImage:thumbImage]; 
            [thumbView setDelegate:self]; 
            [thumbView setImageName:name]; 
            CGRect frame = [thumbView frame]; 
            frame.origin.y = THUMB_V_PADDING; 
            frame.origin.x = xPosition; 
            [thumbView setFrame:frame]; 
            [thumbView setHome:frame]; 
            [thumbScrollView addSubview:thumbView]; 
            [thumbView release]; 
            xPosition += (frame.size.width + THUMB_H_PADDING); 
           } 
          } 
          [thumbScrollView setContentSize:CGSizeMake(xPosition, scrollViewHeight)]; 
         }  
        } 
    
        #pragma mark Utility methods 
    
        - (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 = [imageScrollView frame].size.height/scale; 
         zoomRect.size.width = [imageScrollView 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; 
        } 
    
    
        #pragma mark - 
        #pragma mark Rotation support 
    
        // Ensure that the view controller supports rotation and that the split view can therefore show in both portrait and landscape. 
        - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
        return YES; 
        } 
    
    
    
    
    
    
        @end 
    

Répondre

0

Si vous avez configuré votre UIScrollView dans un fichier Nib, assurez-vous qu'il est correctement redimensionne lors d'une rotation (Utilisez les commandes de la Dimensionnement automatique Taille Inspecteur dans Interface Builder: les deux ensembles de flèches à l'intérieur de la boîte doivent être rouges)

Utilisez ensuite cette option pour redimensionner l'image lorsque l'iPad change d'orientation:

- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{ 
//if your UIImageView is called zoomView use this 
CGRect zoomRect=CGRectMake(0,0,zoomView.frame.size.width, zoomView.frame.size.height); 
[scrollView zoomToRect:zoomRect animated:YES];} 

(Désolé pour le mauvais placement du {}, mais il n'a pas été correctement coller code pour une raison quelconque) Hope this helps!

Questions connexes