[iOS]图组全屏可缩放和旋转屏幕来展示
Demo:http://download.csdn.net/detail/u012881779/9775105
因为要加载图片,所以先导入了工具SDWebImage;
#import "GraphicDetailsViewController.h" #import "UIImageView+WebCache.h" @interface GraphicDetailsViewController () <UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @property (strong, nonatomic) NSArray *picArr; @property (assign,nonatomic) NSInteger pageIndex; @end @implementation GraphicDetailsViewController @synthesize dataMDict = _dataMDict; - (void)dealloc { _dataMDict = nil; self.picArr = nil; } - (void)viewDidLoad { [super viewDidLoad]; self.pageIndex = [[_dataMDict objectForKey:@"index"] integerValue]; self.picArr = [_dataMDict objectForKey:@"array"]; [self settingAction:YES]; [self creatPictureAction]; // 设备旋转通知 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDeviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; } - (BOOL)prefersStatusBarHidden { return YES ; } - (void)settingAction:(BOOL)result { self.title = @"图片详情"; [self.navigationController setNavigationBarHidden:result]; // iOS7之后 if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) { [self prefersStatusBarHidden]; [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)]; } // iOS9之后 [[UIApplication sharedApplication] setStatusBarHidden:result]; } - (IBAction)tapBackViewAction:(id)sender { [self settingAction:NO]; [self.navigationController popViewControllerAnimated:NO]; [self.navigationController setNavigationBarHidden:NO]; [[UIApplication sharedApplication] setStatusBarHidden:NO]; } - (void)creatPictureAction { for (int i = 0; i < self.picArr.count ; i ++) { NSString *imgUrl = [self.picArr objectAtIndex:i]; UIScrollView *backScrollView = [[UIScrollView alloc] init]; backScrollView.backgroundColor = [UIColor clearColor]; [backScrollView setShowsVerticalScrollIndicator:NO]; [backScrollView setShowsHorizontalScrollIndicator:NO]; [backScrollView.layer setMasksToBounds:YES]; backScrollView.delegate = self; backScrollView.maximumZoomScale = 3.0; backScrollView.minimumZoomScale = 1.0; [_scrollView addSubview:backScrollView]; UIImageView *tempIV = [[UIImageView alloc] init]; [tempIV sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"qspic.png"]]; [tempIV setContentMode:UIViewContentModeScaleAspectFit]; [backScrollView addSubview:tempIV]; } [self adaptiveImageViewFrame]; } #pragma mark UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.tag == 2300) { CGPoint osPoint = scrollView.contentOffset; osPoint.y = 0; scrollView.contentOffset = osPoint; } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (scrollView.tag == 2300) { CGPoint osPoint = scrollView.contentOffset; NSInteger index = (osPoint.x + [UIScreen mainScreen].bounds.size.width/2.0) / [UIScreen mainScreen].bounds.size.width; osPoint.x = [UIScreen mainScreen].bounds.size.width * index; [UIView animateWithDuration:0.2 animations:^{ scrollView.contentOffset = osPoint; } completion:nil]; self.pageIndex = index; } } // 通过ScrollView的这个代理方法来实现图片的缩放 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { if (scrollView.tag != 2300) { NSArray *subViews = [scrollView subviews]; UIImageView *imageView; for (int i = 0; i < subViews.count ; i ++) { UIView *tempView = [subViews objectAtIndex:i]; if ([[tempView class] isSubclassOfClass:[UIImageView class]]) { imageView = (UIImageView *)tempView; } } return imageView; } else { return nil; } } // 监控屏幕旋转 - (void)handleDeviceOrientationDidChange:(UIInterfaceOrientation)interfaceOrientation{ UIDevice *device = [UIDevice currentDevice] ; switch (device.orientation) { case UIDeviceOrientationFaceUp: NSLog(@"屏幕朝上平躺"); break; case UIDeviceOrientationFaceDown: NSLog(@"屏幕朝下平躺"); break; case UIDeviceOrientationUnknown: NSLog(@"未知方向"); break; case UIDeviceOrientationLandscapeLeft: NSLog(@"home键在右"); break; case UIDeviceOrientationLandscapeRight: NSLog(@"home键在左"); break; case UIDeviceOrientationPortrait: NSLog(@"home键在下"); break; case UIDeviceOrientationPortraitUpsideDown: NSLog(@"home键在上"); break; default: NSLog(@"无法辨识"); break; } [self adaptiveImageViewFrame]; } // 设置scrollView和imageView的frame - (void)adaptiveImageViewFrame { float sWith = [UIScreen mainScreen].bounds.size.width; float sHigh = [UIScreen mainScreen].bounds.size.height; NSMutableArray *subSVArr = [[NSMutableArray alloc] init]; NSArray *subArr = _scrollView.subviews; for (int i = 0; i < subArr.count ; i ++) { UIView *tempView = [subArr objectAtIndex:i]; if ([[tempView class] isSubclassOfClass:[UIScrollView class]]) { [subSVArr addObject:tempView]; } } for (int i = 0; i < subSVArr.count ; i ++) { UIScrollView *tempView = [subSVArr objectAtIndex:i]; [tempView setFrame:CGRectMake(sWith*i, 0, sWith, sHigh)]; [[[tempView subviews] firstObject] setFrame:tempView.bounds]; } [_scrollView setContentSize:CGSizeMake(sWith*subSVArr.count, sHigh)]; NSInteger index = self.pageIndex; CGPoint osPoint = _scrollView.contentOffset; osPoint.y = 0; osPoint.x = sWith * index; _scrollView.contentOffset = osPoint; } @end示意图: