您的位置:首页 > 产品设计 > UI/UE

UIImageView范围内放大,缩小,移动 -- 官方文档代码

2017-05-04 10:54 393 查看
UIImageView范围内放大,缩小,移动 -- 官方文档代码



我们都知道,使用UIImagePickerController选择完图片时,设置属性allowsEditing=YES;,那么选中的图片就会跳转到一个新的控制器,在这个控制器里可以在指定的范围内放大、缩小和移动,但是这个界面是CocoaTouch决定的

如果我们自己单独新建一个控制器要写这样的代码,直接用UIPinchGestureRecognizer来做还是有点麻烦的,所以本人在Google上搜了搜,得到如下的完美答案,官方推荐的写法:

@interface ImageViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation ImageViewController

- (instancetype)initWithImage:(UIImage *)image {
if (self = [super init]) {
_originalImage = image;
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];
[self toggleNavigationBar];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doneWithRecognization)];
self.navigationItem.leftBarButtonItem = [[BarButtonItem alloc] initWithTitle:nil position:kBackPosition style:UIBarButtonItemStylePlain target:self action:@selector(dismissPresentVC)];

/*
* As the Apple Documentation said, it is the easiest way to implement pinch-in and pinch-out on an UIImageView, see below links.
* http://stackoverflow.com/questions/500027/how-to-zoom-in-out-an-uiimage-object-when-user-pinches-screen * https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html *
*/
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
_scrollView.minimumZoomScale = 1.0;
_scrollView.maximumZoomScale = 6.0;
_scrollView.contentSize = CGSizeMake(1280, 960);
_scrollView.delegate = self;
[self.view addSubview:_scrollView];

CGRect rect = [UIScreen mainScreen].bounds;
UIImage *scaledImg = [self.originalImage aspectScaleToSize:rect.size];
UIImageView *imgView = [[UIImageView alloc] initWithImage:scaledImg];
CGFloat Y = (rect.size.height - scaledImg.size.height) / 2;
imgView.frame = CGRectMake(0, Y, rect.size.width, rect.size.height);
imgView.userInteractionEnabled = YES;
[imgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleNavigationBar)]];
[imgView setMultipleTouchEnabled:YES];
_imageView = imgView;
[_scrollView addSubview:imgView];

}

#pragma mark UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}

- (void)doneWithRecognization {
if ([self.imageExhibitionDelegate respondsToSelector:@selector(imageViewExhibitionDidClickDone:)]) {
[self.imageExhibitionDelegate imageViewExhibitionDidClickDone:self];
}
}

- (void)toggleNavigationBar {
if (self.navigationController.navigationBar.hidden) {
self.navigationController.navigationBar.hidden = NO;
[[UIApplication sharedApplication] setStatusBarHidden:NO];
} else {
self.navigationController.navigationBar.hidden = YES;
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}

self.navigationController.navigationBar.alpha = 0.7;
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
CGRect rect = [UIScreen mainScreen].bounds;
self.view.frame = CGRectMake(0, 0, rect.size.width, rect.size.height + rect.origin.y);
self.view.backgroundColor = [UIColor blackColor];
}

- (void)dismissPresentVC {
self.navigationController.navigationBar.alpha = 1.0;
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[self.navigationController popViewControllerAnimated:YES];
}

@end



此处原文链接

http://stackoverflow.com/questions/500027/how-to-zoom-in-out-an-uiimage-object-when-user-pinches-screen

https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html




其中重点就是:

1.在当前控制器添加一个UIScrollView,设置参数如下,并且实现代理方法viewForZoomingInScrollView:
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView.minimumZoomScale = 0.5;
self.scrollView.maximumZoomScale = 6.0;
self.scrollView.contentSize = self.imageView.frame.size;
self.scrollView.delegate = self;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: