在ibook中放大和缩小功能,如应用程序

时间:2012-06-01 13:48:59

标签: iphone zoom uitapgesturerecognizer ibooks uiswipegesturerecognizer

我是图形编程和学习方法的新手。我正在开发一个类似iBooks的应用程序。 the code from github

它运作良好。如果任何身体可以给我正确的指针/方法如何使其放大/缩小启用。我应该关注/接近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;  
} 

1 个答案:

答案 0 :(得分:0)

在LeavesViewController中

设置scrollView.delegate = self; 然后添加以下

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

这应该允许leavesView缩放