iOSアプリで写真画像を拡大縮小操作できるようにする2つの方法
iOSアプリで写真画像を拡大縮小操作できるようにする2つの方法
iOSで画像を拡大縮小したい場合がある。
そのような場合、2つ方法が用意されている。
・拡大縮小機能が実装されているUIScrollViewを使う方法
・UIViewすべてにある機能のアフィン変換を使う方法
である。
使用する用途によって使い分けると良いだろう。
アフィン変換により写真を拡大縮小する方法
アフィン変換(CGAffinexxx()関数群)によって、手軽にUIImageViewを変形できるが、使いやすさを考慮して写真画像の拡大縮小機能を実装する場合には考慮すべきことがたくさんあり、結局のところ実装が難しくなってしまう。
単に拡大できればいいということであれば、とても簡単に実装できる。
UIImageView#userInteractionEnabled = YES;
// gestureRecognizerをセット
[UIImageView#addGestureRecognizer:[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handler:)]];
- (void)handler:(UIPinchGestureRecognizer)gesture {
CGAffineTransform transform = CGAffineTransformIdentity;
CGFloat scale = gesture.scale;
switch (gesture.state) {
case UIGestureRecognizerStateChanged: // 2
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(scale, scale);
CGAffineTransform transitionTransform = CGAffineTransformMakeTranslation(prePoint.x, prePoint.y);
transform = CGAffineTransformConcat(scaleTransform, transitionTransform);
break;
case UIGestureRecognizerStateEnded: // 3
case UIGestureRecognizerStateCancelled:
break;
}
UIImageView#transform = transform;
}
ScrollViewにより写真を拡大縮小する方法
ある機能を実装する場合にはUIViewControllerにおいてインスタンスを生成して属性をセットしていく方法と、その機能を実装するためのクラスを定義する方法がある。
どちらでも良いのだが、再利用を考慮した場合には後者をお勧めする。
UIScrollViewを継承してカスタムクラスを定義する。
@interface ImageScrollView : UIScrollView {
}
@implementation ImageScrollView {
UIImageView *mImageInScrollView;
CGRect mPageRect;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.showsVerticalScrollIndicator = YES;
self.showsHorizontalScrollIndicator = YES;
self.bouncesZoom = YES;
self.decelerationRate = UIScrollViewDecelerationRateFast;
self.delegate = self;
[self setBackgroundColor:[UIColor whiteColor]];
self.maximumZoomScale = 6.0;
self.minimumZoomScale = 1.0;
}
return self;
}
- (void)setUpMe:(UIImage *)image {
mPageRect = self.view.bounds;
self.contentSize = mPageRect.size;
mImageInScrollView = [[UIImageView alloc] initWithImage:image];
mImageInScrollView.frame = mPageRect;
mImageInScrollView.contentMode = UIViewContentModeScaleAspectFit;
mImageInScrollView.userInteractionEnabled = true;
[self addSubview:mImageInScrollView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return mImageInScrollView;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
self.contentSize = CGRectMake(0., 0., mPageRect.size.width * scale, mPageRect.size.height * scale);
}
// 以降のデリゲートメソッドは使用しない
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
}
2023年12月 | ||||||
日 | 月 | 火 | 水 | 木 | 金 | 土 |
  |   |   |   |   | 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |   |   |   |   |   |   |
iOS
web
アプリの著作権
ブロックチェーン/暗号技術
新しい社会
禅・大乗仏教
日本のなりたち