2012-08-01 27 views
0

正在处理通过触摸将图像添加到视图。到目前为止,我的代码允许我在触摸屏幕时添加与我想要的次数相同的图像。我希望能够一次添加一张图像,这样我就可以操作添加的图像(放大/缩小,旋转,移动)。如何通过触摸仅添加一个图像

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

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

    } 
} 

谢谢!

回答

0

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

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

到:

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

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

如果您触摸子类别imageView的某处,仍然会再次添加相同的视图。如果你想压制这个,你必须添加一个属性来跟踪最后挑选的图像,并将其放入一个if子句中,如果它包含该图像,则不添加图像视图。

相关问题