ibook 之类的应用程序中的放大和缩小功能

Posted

技术标签:

【中文标题】ibook 之类的应用程序中的放大和缩小功能【英文标题】:Zoom in and Zoom out functionality in ibook like app 【发布时间】:2012-06-01 13:48:58 【问题描述】:

我是图形编程的新手,并且正在学习它。我正在开发一个类似于 iBooks 的应用程序。 the code from github

效果很好。如果有任何机构可以给我正确的指示/方法,我怎样才能使它放大/缩小。我是否应该关注/接近图层类(所有图层创建所在的位置)来实现放大/缩小功能。到目前为止,我一直在 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】:

在 LeavesViewController 中

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

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scroll

    return leavesView;

这应该允许叶子视图缩放

【讨论】:

以上是关于ibook 之类的应用程序中的放大和缩小功能的主要内容,如果未能解决你的问题,请参考以下文章

iOS 中 UtableviewCell 中的放大缩小功能

电脑应用拖到上边放大

旋转,拖动,放大,缩小Android中的视频和图像

高德地图自定义放大缩小功能(好用)

如何使用在单击按钮上创建的用户触发放大和缩小绘图图表?

图像的尺寸调整