2012-08-01 38 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子句中,如果它包含該圖像,則不添加圖像視圖。

相關問題