zoukankan      html  css  js  c++  java
  • scrollview里面多张图片,每张都能放大缩小

    http://blog.sina.com.cn/s/blog_5d68044001018s1n.html

    scrollview里面多张图片,每张都能放大缩小

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
        
        UITapGestureRecognizer *doubleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
        [doubleTap setNumberOfTapsRequired:2];

        self.imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 980)];
        self.imageScrollView.backgroundColor = [UIColor clearColor];
        self.imageScrollView.scrollEnabled = YES;
        self.imageScrollView.pagingEnabled = YES;
        self.imageScrollView.delegate = self;
        self.imageScrollView.contentSize = CGSizeMake(768*array.count, 980);
        for (int i = 0; i
            UITapGestureRecognizer *doubleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
            [doubleTap setNumberOfTapsRequired:2];
            
            UIScrollView *s = [[UIScrollView alloc] initWithFrame:CGRectMake(768*i, 0, 768, 980)];
            s.backgroundColor = [UIColor clearColor];
            s.contentSize = CGSizeMake(768, 980);
            s.delegate = self;
            s.minimumZoomScale = 1.0;
            s.maximumZoomScale = 3.0;
            //        s.tag = i+1;
            [s setZoomScale:1.0];
            
            UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 768, 980)];
            NSString *imageName = [array objectAtIndex:i];
            imageview.image = [UIImage imageNamed:imageName];
            imageview.userInteractionEnabled = YES;
            imageview.tag = i+1;
            [imageview addGestureRecognizer:doubleTap];
            [s addSubview:imageview];
            [self.imageScrollView addSubview:s];
            
            [doubleTap release];
            [imageview release];
            [s release];
        }
        
        [self.view addSubview:self.imageScrollView];
        
        
    }
    #pragma mark - ScrollView delegate
    -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
        
        for (UIView *v in scrollView.subviews){
            return v;
        }
        return nil;
    }

    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        imageScrollView=nil;
        //nameArray =nil;
    }
    - (void)dealloc
    {
        [imageScrollView release];
        //  [nameArray release];
        [super dealloc];
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    if(interfaceOrientation ==UIInterfaceOrientationPortrait||interfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown)
        {
            return YES;
        }
        return NO;
    }

    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        
        if (scrollView == self.imageScrollView){
            CGFloat x = scrollView.contentOffset.x;
            if (x==offset){
                
            }
            else {
                offset = x;
                for (UIScrollView *s in scrollView.subviews){
                    if ([s isKindOfClass:[UIScrollView class]]){
                        [s setZoomScale:1.0];
                    }
                }
            }
        }
    }

    //- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    //    [scrollView setZoomScale:scale+1.0 animated:YES];
    //    [scrollView setZoomScale:scale animated:YES];
    //}
    #pragma mark -
    -(void)handleDoubleTap:(UIGestureRecognizer *)gesture{
        
        float newScale = [(UIScrollView*)gesture.view.superview zoomScale] * 1.5;
        CGRect zoomRect = [self zoomRectForScale:newScale  inView:(UIScrollView*)gesture.view.superview withCenter:[gesture locationInView:gesture.view]];
        [(UIScrollView*)gesture.view.superview zoomToRect:zoomRect animated:YES];
    }

  • 相关阅读:
    Django REST Framework之版本控制
    Django REST Framework之频率限制
    webpack4进阶配置
    高阶函数map,filter,reduce的用法
    Vue+elementui 实现复杂表头和动态增加列的二维表格
    Pika源码学习--pika和rocksdb的对接
    Pika源码学习--pika的命令执行框架
    Pika源码学习--pika的PubSub机制
    Pika源码学习--pika的通信和线程模型
    pika源码学习--开篇
  • 原文地址:https://www.cnblogs.com/apem/p/4126732.html
Copyright © 2011-2022 走看看