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;
}
我们都知道,使用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;
}
相关文章推荐
- Android 在代码中设置imageview的大小和位置(满足单方向的放大缩小和任意位置的移动)
- android中使用Baidu Map遇到的问题之:移动Mapview上的放大缩小控件
- 24行代码,让你的网页元素任意放大、缩小、拖拽、移动
- IOS代码添加控件,控件移动,放大,缩小,旋转
- ViewPager图片预览之图片的放大缩小,移动,切换(第四课)结束了
- Android 自定义View可拖动移动位置及边缘拉伸放大缩小
- andriod 利用WebView实现 图片放大缩小以及移动[转]
- IOS代码添加控件,控件移动,放大,缩小,旋转
- UIImageView实现图片移动,缩放、旋转的代码片段
- IOS代码添加控件,控件移动,放大,缩小,旋转
- ViewPager图片预览之图片的放大缩小,移动,切换(第二课)连载
- UIImageView属性---iOS-Apple苹果官方文档翻译
- 自定义View之图片放大、缩小、移动
- 安卓 单指移动,双指缩放,双击放大缩小的 iamgeview
- UIImageView实现图片移动,缩放、旋转的代码片段
- IOS代码添加控件,控件移动,放大,缩小,旋转
- #iOS开发笔记#UIImageView实现拖动,放大/缩小,旋转
- #iOS开发笔记#如何限制UIImageView放大缩小的比例
- 24行代码,让你的网页元素任意放大、缩小、拖拽、移动
- android自定义view实现可自由放大缩小和移动的imageView