2012-07-20 59 views
0

我想在主視圖上的每個水龍頭上動態創建UIImageView,並且還想通過手指移動來移動所有創建的UIImageView。我已成功在每次觸摸我的代碼時動態創建低於:如何在iPhone中管理多個UIImageView

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    CGPoint pt = [touch locationInView:self.view]; 

    imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(pt.x,pt.y, 100, 100)] autorelease]; 
    imgFilepath = [[NSBundle mainBundle] pathForResource:@"img" ofType:@"png"]; 
    img = [[UIImage alloc] initWithContentsOfFile:imgFilepath]; 
    imgView.tag=i; 
    [imgView setImage:img]; 
    [img release]; 
    [self.view addSubview:imgView]; 
} 

現在我想通過手指移動它來移動任何動態創建的UIImageView的。 謝謝。

回答

4

設置的UIImageView的userInteractionEnabled屬性是在創建它的方法(即你貼法):

imgView.userInteractionEnabled = YES; 

,那麼你將最有可能需要繼承的UIImageView,並實現以下方法來移動圖像周圍景觀:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 

提示:在touchesMoved:等方法,你要跟蹤這些用戶的手指移動 - 閱讀一些文檔,你會最有可能最終使用-[UITouch locationInView:]後 - - 你需要t o將座標轉換爲圖像視圖的超視圖,然後在這種情況下移動圖像視圖。

1

touchesBegan檢測imageView你有所觸動。在touchesMoved通過設置它的中心到觸摸點移動移動選定imageView