0

タッチでビューに画像を追加する作業。これまでのところ、私のコードでは、画面に触れたときに選択した同じ画像を何度でも追加できます。一度に 1 つずつ画像を追加できるようにして、追加した画像を操作できるようにしたい (拡大/縮小、回転、移動)。

これを許可するには、コードをどのように変更すればよいですか?

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
touchPoint = [touch locationInView:imageView];

  if (touchPoint.x > 0 && touchPoint.y > 0)
  {
    stampedImage = _imagePicker.selectedImage;   

    _stampedImageView = [[UIImageView alloc] initWithImage:stampedImage];
    _stampedImageView.multipleTouchEnabled = YES;
    _stampedImageView.userInteractionEnabled = YES;
    [_stampedImageView setFrame:CGRectMake(touchPoint.x, touchPoint.y, 80.0, 80.0)];
    _stampedImageView.center = touchPoint;
    [imageView addSubview:_stampedImageView];
    [_stampedImageView release];

  }
}

ありがとう!

4

1 に答える 1

0

私があなたが望むものを理解しているなら、あなたはUIImageViewをサブクラス化し、そのサブクラスにtouchesBegan(および/またはジェスチャ認識メソッド)を実装する必要があります。投稿したコードで、次を変更する必要があります。

_stampedImageView = [[UIImageView alloc] initWithImage:stampedImage];

に:

_stampedImageView = [[CustomImageView alloc] initWithImage:stampedImage];

ここで、CustomImageViewはUIImageViewサブクラスです。これを行うと、カスタムビューでのタッチは、含まれているビューではなく、そのクラスのコードによって処理されます。

サブクラス化されたimageViewのどこかにタッチすると、同じビューが再び追加されます。これを抑制したい場合は、最後に選択した画像を追跡するプロパティを追加し、その画像が含まれている場合は画像ビューを追加しないif句を挿入する必要があります。

于 2012-08-01T04:22:32.120 に答える