【问题标题】:How to add only one image by touch如何通过触摸仅添加一张图像
【发布时间】:2012-08-01 02:48:50
【问题描述】:

致力于通过触摸将图像添加到视图中。到目前为止,我的代码允许我在触摸屏幕时添加我想要多次选择的相同图像。我希望能够一次只添加一张图片,这样我就可以操作添加的图片(放大/缩小、旋转、移动)。

我应该如何修改我的代码以允许这样做?

 -(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];

  }
}

谢谢!

【问题讨论】:

    标签: objective-c cocoa-touch uiimageview uiimage touchesbegan


    【解决方案1】:

    如果我了解您想要什么,您需要继承 UIImageView 并在该子类中实现 touchesBegan(和/或手势识别器方法)。在您发布的代码中,您应该更改:

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

    到:

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

    CustomImageView 是您的 UIImageView 子类。如果你这样做,那么自定义视图中的任何触摸都将由该类中的代码处理,而不是包含视图。

    如果您触摸子类 imageView 之外的某个位置,您仍然会再次添加相同的视图。如果你想抑制这种情况,你必须添加一个属性来跟踪最后选择的图像,并放入一个 if 子句,如果它包含该图像,则不会添加图像视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      相关资源
      最近更新 更多