加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

[转载]scrollview里面多张图片,每张都能放大缩小

(2016-01-13 10:24:57)
标签:

转载

分类: 计算机相关
虽然写的代码有些乱,但是我的确靠它转换成了多张图片随意放大缩小的功能。
(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 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 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];
}

0

前一篇:#穆帅下课了
后一篇:桔梗花 花语
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有