2

我是非常新的圖形編程和學習方式。我正在研究一個類似iBooks的應用程序。 the code from github在ibook like應用中放大和縮小功能

它工作得很好。如果任何機構可以給我正確的指針/方法,我怎樣才能讓它放大/縮小啓用。我應該關注/處理Layer類(創建所有圖層的位置)來實現放大/縮小功能。到目前爲止,我一直在嘗試一些像這樣的事情在UIViewController類

#import "LeavesView.h" 

@interface LeavesViewController : UIViewController <LeavesViewDataSource, LeavesViewDelegate,UIScrollViewDelegate,UIGestureRecognizerDelegate> { 
    LeavesView *leavesView;  
    UIScrollView *theScrollView;  
} 


-(void) zoomStuff; 
// added by Lnkd.com?24 - use designated initializer to avoid continuous loop when loaded from NIB 
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle; 

-(id)init; 

@end 

    #define ZOOM_AMOUNT 0.25f 
    #define NO_ZOOM_SCALE 1.0f 
    #define MINIMUM_ZOOM_SCALE 1.0f 
    #define MAXIMUM_ZOOM_SCALE 5.0f 
    #define NAV_AREA_SIZE 48.0f  
    - (void)loadView { 
    [super loadView]; 
    leavesView.frame = self.view.bounds; 
    leavesView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 
    [self zoomStuff];  
} 

- (void) viewDidLoad { 
    [super viewDidLoad]; 

    leavesView.dataSource = self; 
    leavesView.delegate = self; 
    leavesView.backgroundColor=[UIColor redColor]; 
    [leavesView reloadData]; 
} 
-(void) zoomStuff 
{ 
    UITapGestureRecognizer *tapGesture = nil; 
    UISwipeGestureRecognizer *swipeGesture = nil; 

    theScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; 

    theScrollView.scrollsToTop = NO; 
    theScrollView.directionalLockEnabled = YES; 
    theScrollView.showsVerticalScrollIndicator = NO; 
    theScrollView.showsHorizontalScrollIndicator = NO; 
    theScrollView.contentMode = UIViewContentModeRedraw; 
    theScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    theScrollView.minimumZoomScale = MINIMUM_ZOOM_SCALE; 
theScrollView.maximumZoomScale = MAXIMUM_ZOOM_SCALE; 
    theScrollView.contentSize = theScrollView.bounds.size; 
    theScrollView.backgroundColor = [UIColor yellowColor]; 
    theScrollView.delegate = self; 

    [self.view addSubview:theScrollView]; 
    [theScrollView addSubview:leavesView]; 

    tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesOne:)]; 
    tapGesture.cancelsTouchesInView = NO; 

tapGesture.delaysTouchesEnded = NO; 
//tapGesture.delegate = self; 
    tapGesture.numberOfTouchesRequired = 1; 
tapGesture.numberOfTapsRequired = 1; 
// One finger single tap 
    [self.view addGestureRecognizer:tapGesture]; 
    [tapGesture release]; 

    tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesOne:)]; 

    tapGesture.cancelsTouchesInView = NO; 
    tapGesture.delaysTouchesEnded = NO; 
//tapGesture.delegate = self;  
    tapGesture.numberOfTouchesRequired = 1; 
    tapGesture.numberOfTapsRequired = 2; 
// One finger double tap 
    [self.view addGestureRecognizer:tapGesture]; 
[tapGesture release]; 

    tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesTwo:)]; 

    tapGesture.cancelsTouchesInView = NO; 
    tapGesture.delaysTouchesEnded = NO; 
//tapGesture.delegate = self; 

    tapGesture.numberOfTouchesRequired = 2; 

    tapGesture.numberOfTapsRequired = 2; 
// Two finger double tap 

    [leavesView addGestureRecognizer:tapGesture]; 
[tapGesture release]; 



} 

- (void)handleTouchesOne:(UITapGestureRecognizer *)recognizer 
{ 
    CGRect tapAreaRect = CGRectZero; 
    CGRect viewBounds = recognizer.view.bounds; 
    CGPoint tapLocation = [recognizer locationInView:recognizer.view]; 
    NSInteger numberOfTaps = recognizer.numberOfTapsRequired; 

    // Page increment (single or double tap) 

    tapAreaRect.size.width = NAV_AREA_SIZE; 
    tapAreaRect.origin.y = (viewBounds.origin.y + NAV_AREA_SIZE); 
    tapAreaRect.origin.x = (viewBounds.size.width - NAV_AREA_SIZE); 
    tapAreaRect.size.height = (viewBounds.size.height - NAV_AREA_SIZE); 


    // Page decrement (single or double tap) 

    tapAreaRect.size.width = NAV_AREA_SIZE; 
    tapAreaRect.origin.x = viewBounds.origin.x; 
    tapAreaRect.origin.y = (viewBounds.origin.y + NAV_AREA_SIZE); 
    tapAreaRect.size.height = (viewBounds.size.height - NAV_AREA_SIZE); 



    if (numberOfTaps == 1) // Reader toolbar (single tap) 
    { 
     tapAreaRect.size.height = NAV_AREA_SIZE; 
     tapAreaRect.origin.x = viewBounds.origin.x; 
     tapAreaRect.origin.y = viewBounds.origin.y; 
     tapAreaRect.size.width = viewBounds.size.width; 


    } 

    if (numberOfTaps == 2) // Zoom area handling (double tap) 
    { 
     tapAreaRect = CGRectInset(viewBounds, NAV_AREA_SIZE, NAV_AREA_SIZE); 

     if (CGRectContainsPoint(tapAreaRect, tapLocation)) 
     { 
      CGFloat zoomScale = theScrollView.zoomScale; 

      if (zoomScale < MAXIMUM_ZOOM_SCALE) // Zoom in if below maximum zoom scale 
      { 
       zoomScale = ((zoomScale += ZOOM_AMOUNT) > MAXIMUM_ZOOM_SCALE) ? MAXIMUM_ZOOM_SCALE : zoomScale; 

       [theScrollView setZoomScale:zoomScale animated:YES]; 
      } 
     } 
    } 
} 

- (void)handleTouchesTwo:(UITapGestureRecognizer *)recognizer 
{ 
    CGRect viewBounds = recognizer.view.bounds; 
    CGPoint tapLocation = [recognizer locationInView:recognizer.view]; 

    CGRect tapAreaRect = CGRectInset(viewBounds, NAV_AREA_SIZE, NAV_AREA_SIZE); 

    if (CGRectContainsPoint(tapAreaRect, tapLocation)) 
    { 
     CGFloat zoomScale = theScrollView.zoomScale; 

     if (zoomScale > MINIMUM_ZOOM_SCALE) // Zoom out if above minimum zoom scale 
     { 
      zoomScale = ((zoomScale -= ZOOM_AMOUNT) < MINIMUM_ZOOM_SCALE) ? MINIMUM_ZOOM_SCALE : zoomScale; 

      [theScrollView setZoomScale:zoomScale animated:YES]; 
     } 
    } 
} 



-(BOOL)gestureRecognizer:(UIGestureRecognizer *)this shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)that 
{ 
    if ([this isMemberOfClass:[UISwipeGestureRecognizer class]]) 
     return YES; 
    else 
     return NO; 
} 
+0

是leafview的scrollview的孩子嗎? –

+0

yes leavesview是scrollview的子節點 – Alok

回答

0

在LeavesViewController

設置scrollView.delegate = self; 然後添加以下

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scroll 
{ 
    return leavesView; 
} 

這應該允許leavesView放大