DX・デジタルトランスフォーメーション

AppStoreにて無料公開中! ”システム手帳”で検索! - AppStoreへ iPad版 - iPhone版

ブロックチェーン・暗号技術 | android | iOS | Web | アプリの著作権 | 日本のなりたち | 禅・大乗仏教 | 新しい社会

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月
     12
3456789
10111213141516
17181920212223
24252627282930
31      
android
iOS
web
アプリの著作権
ブロックチェーン/暗号技術
新しい社会
禅・大乗仏教
日本のなりたち

AppStoreにて無料公開中! ”システム手帳”で検索! - AppStoreへ iPad版 - iPhone版
デジタルトランスフォーメーションで新しい生活を提案!