头:
#import <UIKit/UIKit.h>
@interface ImageBrowserViewController : UIViewController
@property (nonatomic,retain) NSArray *images;
@property (nonatomic) int currentIndex;
@end
实现:
#import "ImageBrowserViewController.h"
@interface ImageBrowserViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView; //xib 文件中的控件
- (IBAction)backAction:(id)sender; //退出事件
@end
@implementation ImageBrowserViewController
- (void)viewDidLoad {
[super viewDidLoad];
//_images = @[@"https://ss3.bdstatic.com/lPoZeXSm1A5BphGlnYG/skin/482.jpg",
// @"https://ss2.bdstatic.com/kfoZeXSm1A5BphGlnYG/skin/273.jpg",
// @"https://ss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin_zoom/492.jpg"];
//_currentIndex=0;
[self initScrollView];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)initScrollView{
int imagecount = [_images count];
_scrollView.pagingEnabled =true;
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width*imagecount, _scrollView.frame.size.height);
for (int i=0; i<imagecount; i++) {
UIScrollView *subscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(i*_scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
subscrollview.tag = i+100;
//设置实现缩放 //设置代理scrollview的代理对象
subscrollview.delegate=self;
//设置最大伸缩比例
subscrollview.maximumZoomScale=10.0;
//设置最小伸缩比例
subscrollview.minimumZoomScale=1;
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, _scrollView.frame.size.width-10, _scrollView.frame.size.height)];
imageview.tag = 1;
imageview.contentMode = UIViewContentModeScaleAspectFit;
[imageview sd_setImageWithURL:_images[i] placeholderImage:[UIImage imageNamed:@"photo"]];
//[imageview setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[ NSURL URLWithString:_images[i]]]]];
[subscrollview addSubview:imageview];
[_scrollView addSubview:subscrollview];
}
if (_currentIndex>0) {
[_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width*_currentIndex, 0)];
}
}
-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
if(scrollView.zoomScale == 1){
_scrollView.scrollEnabled = true;
}else{
_scrollView.scrollEnabled = false;
};
NSLog(@"%f", scrollView.zoomScale);
}
//告诉scrollview要缩放的是哪个子控件
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView viewWithTag:1];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)backAction:(id)sender {
[self dismissViewControllerAnimated:true completion:nil];
}
@end
调用:
ImageBrowserViewController *vc = [[ImageBrowserViewController alloc] initWithNibName:@"ImageBrowserViewController" bundle:nil];
vc.images = picArray ;
vc.currentIndex = 0;
[self presentViewController:vc animated:true completion:nil];
Post Views: 654