2013-11-04 65 views
1

我已將一個子視圖添加到UIScrollView。當我放大滾動視圖時,我想平移子視圖。放大後平移UIScrollView的子視圖

touchesBegan:我得到的觸摸的初始位置,然後touchesMoved:我能夠確定多少移動子視圖。當zoomscale爲1.0時,它可以很好地工作。但是,當它縮放指針要移動的子視圖的指針時(此處插圖 - 指針位置被選爲選取框工具)。

視圖的中心應該位於指針位置,而不是位於當前位置! px和py變量確保在子視圖上的任何位置點擊,同時拖動指針的位置始終保持不變。 illustration

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:self]; 
    location.x = location.x * self.zoomScale; 
    location.y = location.y * self.zoomScale; 
    px = location.x; 
    py = location.y; 
    if ([touch view] == rotateView) { 
     self.scrollEnabled = NO; 
     return; 
    } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:self]; 
    location.x = location.x * self.zoomScale; 
    location.y = location.y * self.zoomScale; 
    if ([touch view] == rotateView) { 
     rotateView.center = CGPointMake(rotateView.center.x + (location.x - px), rotateView.center.y + (location.y - py)); 
     px = location.x; 
     py = location.y; 
     return; 
    } 

}

回答

1

相反,你正在採取的辦法,使子視圖另一個的UIScrollView並讓它處理聲像。

(您可能希望設置scrollEnabled = NO你的子視圖直到發生縮放)。

+0

我不知道,我完全你在說什麼我明白。我應該使我的所有子視圖都是另一個滾動視圖?層次結構就像這樣:一個滾動視圖(self)包含另一個uiview,它包含可以在該uivew中移動的元素(並且scrollview也包含scrollview包含uivew)。 (元素是很多uiimageviews或uiviews)。當scrollview沒有放大時,左右移動效果非常好。 –

+0

不,你應該讓你提到的單個視圖(「我已經添加了一個子視圖到UIScrollView」)。或者你已經有另一個滾動視圖作爲你在原始問題中提到的scrollview的子視圖? – hatfinch

+0

不,因爲我已經在評論中說過:我有UIScrollView。其中,我有容器uiview,其中有很多uiimageviews或uiviews作爲子視圖。我試圖在容器視圖中移動這些子視圖(UIImageViews或UIviews)。現在更容易理解嗎? 你是否認爲我的「容器」UIView應該是UIScrollView?如果我這樣做,不會觸及它的子視圖 - 對於我的父級滾動視圖,我創建了customscrollview類,並且覆蓋觸摸開始,移動並結束識別子視圖觸摸的方法。 –

相關問題